Exclusive-OR
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 302 Accepted Submission(s): 92
Problem Description
You are
not given
n non-negative integers
X0, X1, ..., Xn-1 less than 2
20 , but they do exist, and their values never change.
I'll gradually provide you some facts about them, and ask you some questions.
There are two kinds of facts, plus one kind of question:
I'll gradually provide you some facts about them, and ask you some questions.
There are two kinds of facts, plus one kind of question:
Input
There will be at most 10 test cases. Each case begins with two integers
n and
Q (1 <=
n <= 20,000, 2 <=
Q <= 40,000). Each of the following lines contains either a fact or a question, formatted as stated above. The
k parameter in the questions will be a positive integer not greater than 15, and the
v parameter in the facts will be a non-negative integer less than 2
20. The last case is followed by
n=Q=0, which should not be processed.
Output
For each test case, print the case number on its own line, then the answers, one on each one. If you can't deduce the answer for a particular question, from the facts I provide you
before that question, print "I don't know.", without quotes. If the
i-th fact (don't count questions)
cannot be consistent with
all the facts before that, print "The first i facts are conflicting.", then keep silence for everything after that (including facts and questions). Print a blank line after the output of each test case.
Sample Input
2 6 I 0 1 3 Q 1 0 Q 2 1 0 I 0 2 Q 1 1 Q 1 0 3 3 I 0 1 6 I 0 2 2 Q 2 1 2 2 4 I 0 1 7 Q 2 0 1 I 0 1 8 Q 2 0 1 0 0
Sample Output
Case 1: I don't know. 3 1 2 Case 2: 4 Case 3: 7 The first 2 facts are conflicting.#include<iostream>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=21000;
int fath[maxn+1],val[maxn+1];//val[x]表示x^fath[x]的值
int n;//(0---n-1) n表示虚节点,若x的值已知,则将其指向n。
int find(int x)
{
int pref=fath[x];
if(fath[x]!=x)
{
fath[x]=find(fath[x]);
val[x]=val[x]^val[pref];
}
return fath[x];
}
//经过路径压缩后,val[x]表示x^祖先节点,x^y则是val[x]^祖先节点^val[y]^祖先节点=val[x]^val[y];
int uion(int x,int y,int v)//返回0表示和前面冲突
{
int fx=find(x),fy=find(y);
if(fx==fy)
{
if((val[x]^val[y])!=v) return 0;//和前面冲突
return 1;
}
if(fx==n) swap(fx,fy);//如果有一个数的值已知,则将另一个数也要指向n
fath[fx]=fy;//fx指向fy
val[fx]=v^val[y]^val[x];//x到n有两条路,x-fx-fy和x-b-fy;所以val[x]^val[fx]=v^val[y];
return 1;
}
int Q;//操作次数
int l,num[20];//需要被查询的数字
int query()//返回-1表示无法求出具体数字
{
int vis[20]={0};
int ans=0;
for(int i=0;i<l;i++)
{
if(vis[i]) continue;
int f=find(num[i]);//i的祖先节点
ans^=val[num[i]];
vis[i]=1;
int size=1;//和i属于同一棵数的数的个数
for(int j=i+1;j<l;j++)
{
if(vis[j]) continue;
int res=find(num[j]);
if(f==res)//i和j属于同一棵数
{
vis[j]=1;
size++;
ans^=val[num[j]];
}
}
if(f!=n && size%2==1) return -1;//如果数不已知并且属于同一个集合的数字有奇数个,则祖先节点不能抵消,求不出来
}
return ans;
}
void solve()
{
int T=1;
int x,y,v;
for(int i=0;i<=n;i++) fath[i]=i;
memset(val,0,sizeof(val));
int flag=0;
char ch;
while(Q--)
{
cin>>ch;
if(ch=='Q')
{
scanf("%d",&l);
for(int i=0;i<l;i++) scanf("%d",&num[i]);
if(!flag)
{
int res=query();
if(res==-1) printf("I don't know./n");
else printf("%d/n",res);
}
}
else
{
string str;
getline(cin,str);
istringstream sin(str);
int total=0;
int a[3];
while(sin>>a[total])
{
total++;
}
if(total==2) x=a[0],y=n,v=a[1];
else x=a[0],y=a[1],v=a[2];
if(!uion(x,y,v))
{
printf("The first %d facts are conflicting./n", T);
flag=1;
}
T++;
}
}
}
int main()
{
int pl=1;
while(scanf("%d%d",&n,&Q)==2&&n)
{
printf("Case %d:/n", pl++);
solve();
printf("/n");}
return 0;
}