题目:
题解:
以前对于开关灯问题的相邻赋值有误解,a[i][j]表示的是对于第i个灯来说,第j个灯会不会影响ta,所以这里a[i][j]的值应该跟读入的link[j][i]是相同的。
其他就很简单了,多解就是自由元的个数x,2^x
代码:
#include <cstdio>
#include <bitset>
#include <cstring>
using namespace std;
bitset <35> a[35];int b[35],n,s[35];
bool link[35][35],flag[35];
void gauss()
{
for (int i=1;i<=n;i++)
{
if (a[i][i]==0)
{
int num=i;
for (int j=i+1;j<=n;j++)
if (a[j][i]) {num=j; break;}
if (num==i) continue;
swap(a[num],a[i]); swap(b[num],b[i]);
}
for (int j=i+1;j<=n;j++)
if (a[j][i]) a[j]^=a[i],b[j]^=b[i];
}
int num=1;
for (int i=n;i>=1;i--)
{
if (!a[i][i])
{
if (b[i]==0) num*=2;
else {printf("Oh,it's impossible~!!\n");return;}
}
for (int j=1;j<i;j++)
if (a[j][i]) b[j]^=b[i];
}
printf("%d\n",num);
}
int main()
{
int T,x,y;scanf("%d",&T);
while (T--)
{
memset(link,0,sizeof(link));
memset(a,0,sizeof(a));
scanf("%d",&n);
for (int i=1;i<=n;i++) scanf("%d",&s[i]);
for (int i=1;i<=n;i++) scanf("%d",&b[i]),b[i]^=s[i];
while (scanf("%d%d",&x,&y) && x!=0) link[x][y]=1;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (link[j][i]) a[i][j]=1;
for (int i=1;i<=n;i++) a[i][i]=1;
gauss();
}
}