Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P5027 Barracuda

P5027 Barracuda

枚举哪一个是方程是错误的,每次都消元即可,注意判断无解。

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
template <class Type>
void read(Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
flag && (x = ~x + 1);
}
template <class Type>
void write(Type x)
{
x < 0 && (putchar('-'), x = ~x + 1);
x > 9 && (write(x / 10), 0);
putchar(x % 10 + '0');
}
const int N = 110;
const double eps = 1e-12;
int n, ans[N];
double w[N][N], A[N][N];
int Gauss()
{
for (int k = 0; k < n; k++)
{
int t = k;
for (int i = k + 1; i < n; i++)
fabs(A[i][k]) > fabs(A[t][k]) && (t = i);
if (fabs(A[t][k]) < eps)
return 0;
for (int i = k; i <= n; i++)
swap(A[t][i], A[k][i]);
for (int i = n; i >= k; i--)
A[k][i] /= A[k][k];
for (int i = k + 1; i < n; i++)
for (int j = n; fabs(A[i][k]) > eps && j >= k; j--)
A[i][j] -= A[k][j] * A[i][k];
}
for (int i = n - 1; ~i; i--)
for (int j = i + 1; j < n; ++j)
A[i][n] -= A[i][j] * A[j][n];
int mx = -1;
for (int i = 0; i < n; i++)
{
ans[i] = A[i][n];
if ((A[i][n] - ans[i]) > eps || ans[i] <= 0)
return 0;
(mx == -1 || ans[i] > ans[mx]) && (mx = i);
}
for (int i = 0; i < n; i++)
if (i ^ mx && ans[i] == ans[mx])
return 0;
return ++mx;
}
int main ()
{
read(n);
for (int i = 0, k; i <= n; i++)
{
read(k);
for (int a; k; k--)
read(a), w[i][--a] = 1;
read(k), w[i][n] = k;
}
int t = 0;
for (int i = 0; i <= n; i++)
{
memcpy(A, w, sizeof w);
swap(A[i], A[n]);
int k = Gauss();
if (k && t)
{
t = 0;
break;
}
k && (t = k);
}
!t ? puts("illegal") : (write(t), 0);
return 0;
}