Exclusive-OR
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3300 Accepted Submission(s): 909
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:
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
Case 1: I don't know. 3 1 2 Case 2: 4 Case 3: 7 The first 2 facts are conflicting.
<span style="font-size:18px;"> #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
const int maxn=20005;
int fa[maxn],d[maxn];
int n;
struct node
{
int a,p;
bool operator<(const node& b)const{
return p<b.p;
}
}quy[30];
int find(int x){
if(x==fa[x]) return x;
int y=find(fa[x]);
d[x]=d[x]^d[fa[x]];
return fa[x]=y;
}
bool Union(int a,int b,int v)
{
int pa=find(a);
int pb=find(b);
if(pa==pb){
return (d[a]^d[b])==v;
}
if(pa==n) swap(pa,pb);
fa[pa]=pb;
d[pa]=d[a]^d[b]^v;
return true;
}
int query(int k)
{
for(int i=0;i<k;i++)
quy[i].p=find(quy[i].a); //cout << quy[i].p << endl;
int l=0;
int ans=0;
sort(quy,quy+k);
while(l<k){
int r=l;
while(r<k-1 && quy[r+1].p==quy[l].p) r++;
int num=r-l+1;
if(quy[l].p!=n && (num&1)) return -1;
for(int i=l; i<=r; i++) ans^=d[quy[i].a];
l=r+1;
//cout << -1 << endl;
}
return ans;
}
int main()
{
char str[100];
int Q,cas=0;
while(~scanf("%d%d",&n,&Q) && n+Q)
{
for(int i=0; i<=n;i++) fa[i]=i,d[i]=0;
printf("Case %d:\n",++cas);
int killid=-1,kk=0;
while(Q--){
scanf("%s",str);
if(str[0]=='Q'){
int k;
scanf("%d",&k);
for(int i=0;i<k;i++) scanf("%d",&quy[i].a);
if(killid !=-1) continue;
int ans=query(k);
if(ans==-1) puts("I don't know.");
else printf("%d\n",ans);
}
else {
kk++;
gets(str);
if(killid!=-1) continue;
int a,b,c;
int t=sscanf(str,"%d%d%d",&a,&b,&c);
int res;
//cout << t << endl;
if(t==2){
res=Union(a,n,b);
}
else{
res=Union(a,b,c);
}
if(!res) killid=kk;
}
}
if(killid != -1){
printf("The first %d facts are conflicting.\n",kk);
}
printf("\n");
}
return 0;
}
</span>
1311



被折叠的 条评论
为什么被折叠?



