HDU 3234 Exclusive-OR(加权并差集)
2016-08-11 22:31
417 查看
Exclusive-OR
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3300 Accepted Submission(s): 909
[align=left]Problem Description[/align]
You are not given
n non-negative integers X0, X1, ..., Xn-1 less than 220 , 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:
[align=left]Input[/align]
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 220. The last case is followed by
n=Q=0, which should not be processed.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
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
[align=left]Sample Output[/align]
Case 1:
I don't know.
3
1
2
Case 2:
4
Case 3:
7
The first 2 facts are conflicting.
[align=left]Source[/align]
2009 Asia Wuhan Regional Contest Hosted
by Wuhan University
题意:
对于n个数a[0]~a[n-1],但你不知道它们的值,通过逐步提供给你的信息,你的任务是根据这些信息回答问题
I P V :告诉你a[P] = V
I P Q V:告诉你a[P] XOR a[Q] = V
Q K P1..PK:询问a[P1]^a[P2]^...a[PK]的值
思路:
很经典的并查集题目
r[a]记录的是a与其父亲节点的异或值
通过并查集合并的过程同时更新路径上的r值
令fa,fb分别是a,b,的父亲节点
我们知道r[a] = a^fa,r[b]=b^fa,a^b=c
那么在合并fa,fb的时候,令fb为fa的父节点
那么就有r[fa]=fa^fb
而r[a]=a^fa,所以fa=a^r[a]
同理fb=b^r[b]
所以r[fa] = r[a]^r[b]^a^b = r[a]^r[b]^c
当然对于一个节点合并问题不好解决,此时可以添加一一个新节点n作为根节点,使得n的值为0,那么任何值与0的异或都是本身
这样一来合并问题就解决了
然后对于查询而言,对于n是根节点的我们不需要考虑,但是对于不是以n为跟节点的,根节点的值被重复计算了,这个时候我们要统计根节点被统计的次数,如果是奇数次,那么必然是无法确定的了
<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>
相关文章推荐
- HDU 3234 | UValive 4487 - Exclusive-OR (加权并查集)
- HDU 3234 - Exclusive-OR(并查集)
- HDU 3234 Exclusive-OR 扩展并查集
- HDU 3234 Exclusive-OR 并查集变形
- hdu 3234 Exclusive-OR 有权并查集
- Hdu 3234 & Uva 12232 Exclusive-OR
- HDU 3234 Exclusive-OR(并查集)
- hdu 3234 Exclusive-OR
- HDU 3234 Exclusive-OR 09年武汉区域赛E题
- HDU 3234 Exclusive-OR 并查集
- hdu 3234 Exclusive-OR
- HDU 3234 Exclusive-OR
- HDU 3234 Exclusive-OR 并查集扩展
- WuHan 2009 / UVa 12232 / HDU 3234 Exclusive-OR (异或的性质&加权并查集&合并时保持根结点不变)
- HDU 3234 Exclusive-OR(并查集偏移向量)
- hdu 3234 Exclusive-OR (并查集)
- HDU 3234 Exclusive-OR Regional的题就是硬啊卧槽
- HDU 3234 Exclusive-OR
- [HDU 3234] Exclusive-OR
- hdu 3234 Exclusive-OR[并查集] 施工中没写完233