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
Input
The first line of the input contains the number
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
output
1 2 3
题目大意
一共有
思路
高斯消元,把所有的减号换成异或。
代码
#include <cstdio>
#include <algorithm>
const int maxn=250;
int n,ans[maxn+1],tot;
struct matrix
{
int a[maxn+1][maxn+2];
inline int gauss()
{
for(register int i=1; i<n; ++i)
{
if(!a[i][i])
{
int j=i;
while(j<=n)
{
if(a[j][i])
{
break;
}
++j;
}
if(j>n)
{
return 1;
}
for(register int k=i; k<=n+1; ++k)
{
std::swap(a[i][k],a[j][k]);
}
}
for(register int j=i+1; j<=n; ++j)
{
if(a[j][i])
{
for(register int k=i; k<=n+1; ++k)
{
a[j][k]^=a[i][k];
}
}
}
}
for(register int i=n; i>1; --i)
{
for(register int j=1; j<i; ++j)
{
a[j][n+1]^=a[j][i]*a[i][n+1];
}
}
return 0;
}
inline int print_ans()
{
for(register int i=1; i<=n; ++i)
{
if(a[i][n+1])
{
++tot;
ans[tot]=i;
}
}
for(register int i=1; i<tot; ++i)
{
printf("%d ",ans[i]);
}
printf("%d",ans[tot]);
return 0;
}
};
matrix t;
int a;
int main()
{
scanf("%d",&n);
for(register int i=1; i<=n; ++i)
{
while(1)
{
scanf("%d",&a);
if(a==-1)
{
break;
}
t.a[a][i]=1;
}
}
for(register int i=1; i<=n; ++i)
{
t.a[i][n+1]=1;
}
if(t.gauss())
{
puts("No solution");
}
else
{
t.print_ans();
}
return 0;
}