Central heating
Time Limit: 1000MS Memory Limit: 65536K
Description
Winter has come, but at the Ural State University heating is not turned on yet. There’s one little problem: the University is heated only if all of the valves are opened. There are some technicians at the University. Each of them is responsible for one or more valves. There may be several technicians responsible for the same valve. When a technician gets an instruction to turn on the heating he goes round all of his valves and turns them. It means that if a valve was opened then he closes it, and if it was closed then he opens it. It is well known that every technician earns his money not in vain so it’s impossible to replace any technician by any combination of other technicians.
Your task is to determine who of the technicians is to get an instruction “to turn on the heating” in order to heat all the Ural State University. Note that there are N technicians and N valves at the University (1 <= N <= 250).
Input
The first line of an input contains the number N. The next N lines contain lists of the valves in charge of each of the technicians. It means that a line number i + 1 contains numbers of the valves that the i-th technician is responsible for. Each list of valves is followed by –1.
Output
An output should contain a list of technicians’ numbers sorted in ascending order. If several lists are possible, you should send to an output the shortest one. If it’s impossible to turn on the heating at the University, you should write “No solution” .
Sample Input
4
1 2 -1
2 3 4 -1
2 -1
4 -1
Sample Output
1 2 3
题意:
就是有n个人和n个阀门,每个人都负责几个阀门,一旦用了这个人,那么他所负责的阀门开的会关,关的会开。
思路:
我们可以将全部的用矩阵表示,列代表人所负责的阀门,行代表着阀门,然后就有了:
1 0 0 0 | 1
1 1 1 0 | 1
0 1 0 0 | 1
0 1 0 1 | 1
然后每一个阀门都需要打开的话,就说明只要一行里面只要有一个1就行了,然后的话通过之前的运算的影响看看最后消元后的1是否需要这个人去开阀门比如最后的一个人,因为之前第二个人已经开过阀门了,所以最后一个人就可以不需要了,然后矩阵最后一列就可以直到最后是否需要这个人了。
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cmath>
using namespace std;
typedef vector<int> vec;
typedef vector<vec > mat;
const double eps = 1e-8;
vec guass(mat A) {
int n = A.size();
mat B(n, vec(n + 1));
B = A;
for (int i = 0; i < n; i++) {
int temp = i;
for (int j = i + 1; j < n; j++) {
if (abs(B[j][i]) > abs(B[temp][i])) temp = j;
}
swap(B[i], B[temp]);
if (B[i][i] < eps) return vec();
for (int j = 0; j < n; j++) {
if (i != j) {
for (int k = i + 1; k <= n; k++) B[j][k] ^= B[j][i] & B[i][k];
}
}
}
vec x;
for (int i = 0; i < n; i++) {
if (B[i][n]) x.push_back(i + 1);
}
return x;
}
int main() {
int n, x;
scanf("%d", &n);
mat A(n, vec(n + 1));
for (int i = 0; i < n; i++) {
while (scanf("%d", &x) != EOF && x != -1) {
x--;
A[x][i] = 1;
}
A[i][n] = 1;
}
vec v = guass(A);
for (int i = 0; i < v.size(); i++) {
if (i != 0) printf(" ");
printf("%d", v[i]);
}
return 0;
}