题意:给你n个数,接着三种操作: 
I p v :告诉你 Xp = v 
I p q v :告诉你 Xp ^ Xq = v 
Q k p1 p2 … pk:问你k个数连续异或的结果 
注意前两类操作可能会出现与之前告诉你的相矛盾,此时输出“The first n(第几个I) facts are conflicting.”接着一直保持沉默,否则不输出。最后一类询问可能得不到值,就输出“I don’t know.”,否则输出结果

  题解:告诉你时使用并查集的合并操作,可以记录权值为此点异或父亲节点的值,祖先节点的权值一定为0(其他值异或0不变),因为:X^X1^X1^X2=X^X2 则可以进行路径压缩。 
  但是我们知道一个集合的关系时,却不一定知道每个元素各自的大小,所以再记录一个权值2,当一个集合祖先的权值2非负时,表示祖先确定大小了,而这个集合就一定可以知道每个元素的大小,否则就不知道。我们合并时就要注意某子树是否知道每个元素的大小。 
  询问是首先将明确其大小的值计算出来。而只知道关系一些数:如果有偶数个在同一集合,那这偶数个就可以运用它们间的关系一同求出。 
  这儿还有一个小麻烦就是当输入I时,后面个数不定,所以可以使用sscanf处理。 
  真不愧是神题啊,我copy文档的“I don’t know.”居然是错的。。。。。。。。。。。。。。。。。。。。。。。。。。。。错的。。。。。。。。。。。。。。。。。。。。。。。。。。

  1. #include<set>
  2. #include<map>
  3. #include<queue>
  4. #include<stack>
  5. #include<cmath>
  6. #include<vector>
  7. #include<string>
  8. #include<cstdio>
  9. #include<cstring>
  10. #include<stdlib.h>
  11. #include<iostream>
  12. #include<algorithm>
  13. using namespace std;
  14. #define eps 1E-8
  15. /*注意可能会有输出-0.000*/
  16. #define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
  17. #define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
  18. #define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
  19. #define mul(a,b) (a<<b)
  20. #define dir(a,b) (a>>b)
  21. typedef long long ll;
  22. typedef unsigned long long ull;
  23. const int Inf=<<;
  24. const double Pi=acos(-1.0);
  25. const int Mod=1e9+;
  26. const int Max=;
  27. int fat[Max],ran[Max],num[Max];
  28. int tem[],tem1;
  29. void Init(int n)
  30. {
  31. for(int i=; i<=n; ++i)
  32. {
  33. fat[i]=i;
  34. ran[i]=;
  35. num[i]=-;
  36. }
  37. return;
  38. }
  39. int Find(int x)
  40. {
  41. if(x==fat[x])
  42. return fat[x];
  43. int y=Find(fat[x]);
  44. ran[x]=(ran[x]^ran[fat[x]]);//异或优先级很低
  45. return fat[x]=y;
  46. }
  47. int Union(int x,int y,int z)
  48. {
  49. int x1=Find(x);
  50. if(y==-)//确定一个值
  51. {
  52. if(num[x1]==-)
  53. {
  54. num[x1]=(ran[x]^z);
  55. return ;
  56. }
  57. if(num[x1]==(ran[x]^z))
  58. return ;
  59. return ;
  60. }
  61. int y1=Find(y);
  62. if(x1==y1)
  63. {
  64. if((ran[x]^ran[y])==z)
  65. return ;
  66. return ;
  67. }
  68. if(num[x1]==-)//x集合不知道每个值得权值
  69. {
  70. fat[x1]=y1;
  71. ran[x1]=(ran[x]^ran[y]^z);
  72. return ;
  73. }
  74. else
  75. {
  76. fat[y1]=x1;
  77. ran[y1]=(ran[x]^ran[y]^z);
  78. if(num[y1]==-)
  79. return ;
  80. else
  81. {
  82. if((num[x1]^num[y1])==ran[y1])
  83. return ;
  84. return ;
  85. }
  86. }
  87. }
  88. int Solve(int n)//询问
  89. {
  90. int ans=,vis[];
  91. for(int i=; i<tem1; ++i)
  92. {
  93. int x1=Find(tem[i]);
  94. if(num[x1]!=-)
  95. {
  96. ans=(ans^ran[tem[i]]^num[x1]);
  97. vis[i]=-;
  98. }
  99. else//不知道这个值
  100. {
  101. ans=(ans^ran[tem[i]]);
  102. vis[i]=x1;
  103. }
  104. }
  105. sort(vis,vis+tem1);
  106. int flag=;
  107. for(int i=; i<tem1; ++i)
  108. {
  109. if(vis[i]!=-)//不知道的值要在同集合出现偶数次
  110. {
  111. if(flag)
  112. {
  113. if(vis[i]!=vis[i-])
  114. return -;
  115. flag=;
  116. }
  117. else
  118. flag=;
  119. }
  120. }
  121. if(flag)
  122. return -;
  123. return ans;
  124. }
  125. int main()
  126. {
  127. int n,m;
  128. int xx1,yy1,val,flag,coun=,tem3;
  129. char str[];
  130. while(~scanf("%d %d",&n,&m))
  131. {
  132. if(!n&&!m)
  133. break;
  134. tem3=;
  135. printf("Case %d:\n",++coun);
  136. flag=;
  137. Init(n);
  138. for(int i=; i<m; ++i)
  139. {
  140. scanf("%s",str);
  141. if(str[]=='I')
  142. {
  143. tem3++;
  144. getchar();
  145. gets(str);
  146. if(flag)
  147. {
  148. if(sscanf(str,"%d%d%d",&xx1,&yy1,&val)==)//转化
  149. {
  150. val=yy1;
  151. yy1=-;
  152. }
  153. int tem2=Union(xx1,yy1,val);
  154. if(!tem2)
  155. {
  156. printf("The first %d facts are conflicting.\n",tem3);
  157. flag=;
  158. }
  159. }
  160. }
  161. else
  162. {
  163. scanf("%d",&tem1);
  164. for(int j=; j<tem1; j++)
  165. scanf("%d",&tem[j]);
  166. if(flag)
  167. {
  168. int tem2=Solve(n);
  169. if(tem2==-)
  170. printf("I don't know.\n");
  171. else
  172. printf("%d\n",tem2);
  173. }
  174. }
  175. }
  176. printf("\n");
  177. }
  178. return ;
  179. }

UVA 12232 Exclusive-OR(并查集+思想)的更多相关文章

  1. UVA 11987 - Almost Union-Find(并查集)

    UVA 11987 - Almost Union-Find 题目链接 题意:给定一些集合,操作1是合并集合,操作2是把集合中一个元素移动到还有一个集合,操作3输出集合的个数和总和 思路:并查集,关键在 ...

  2. PAT甲级1004题解——并查集思想改

    题目分析:本题开始一直在考虑如何将每一个节点通过一种合适的数据结构存储起来(一对多的关系),最后发现借助并查集的思想可以用一个数组p,p[i]存放i节点的父节点,每次查询编号为i的节点属于第几层且判断 ...

  3. CodeForces 828C String Reconstruction(并查集思想)

    题意:给你n个串,给你每个串在总串中开始的每个位置,问你最小字典序总串. 思路:显然这道题有很多重复填涂的地方,那么这里的时间花费就会特别高. 我们维护一个并查集fa,用fa[i]记录从第i位置开始第 ...

  4. UVA - 1197 (简单并查集计数)

    Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized ...

  5. UVA 10158 War(并查集)

    //思路详见课本 P 214 页 思路:直接用并查集,set [ k ]  存 k 的朋友所在集合的代表元素,set [ k + n ] 存 k  的敌人 所在集合的代表元素. #include< ...

  6. UVA - 11987 Almost Union-Find 并查集的删除

    Almost Union-Find I hope you know the beautiful Union-Find structure. In this problem, you're to imp ...

  7. uva 6910 - Cutting Tree 并查集的删边操作,逆序

    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  8. UVA - 208 Firetruck(并查集+dfs)

    题目: 给出一个结点d和一个无向图中所有的边,按字典序输出这个无向图中所有从1到d的路径. 思路: 1.看到紫书上的提示,如果不预先判断结点1是否能直接到达结点d,上来就直接dfs搜索的话会超时,于是 ...

  9. UVA 11987 Almost Union-Find 并查集单点修改

                                     Almost Union-Find I hope you know the beautiful Union-Find structur ...

随机推荐

  1. ffmpeg-20160701-git-bin.7z

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...

  2. PL/Proxy介绍

    PL/Proxy 介绍 一.概述 1.PL/Proxy 是一个采用PL Language语言的数据库分区系统. 目的:轻松访问分区数据库 它的理念是代理远程函数体内指定.函数调用同样标签创建的函数,所 ...

  3. [Android]如何获取当前用户设置的时区

    方法:TimeZone.getDefault().getDisplayName(true, TimeZone.SHORT);获取的值如GMT+08:00,GMT-04:00,EDT  另附:国家码查询 ...

  4. java获得本机IP,名称等

    import java.net.InetAddress; import java.net.UnknownHostException; public class GetLocalIP { public ...

  5. python基础——map/reduce

    python基础——map/reduce Python内建了map()和reduce()函数. 如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Pro ...

  6. CLR via C#(02)-基元类型、引用类型、值类型

    http://www.cnblogs.com/qq0827/p/3281150.html 一. 基元类型 编译器能够直接支持的数据类型叫做基元类型.例如int, string等.基元类型和.NET框架 ...

  7. Overview and Evaluation of Bluetooth Low Energy: An Emerging Low-Power Wireless Technology

    转自:http://www.mdpi.com/1424-8220/12/9/11734/htm Sensors 2012, 12(9), 11734-11753; doi:10.3390/s12091 ...

  8. java-解决业务操可能数据冲突问题

    问题提出,由于业务会出现多人同时操作,或者业务人员反复的操作,因此在业务流程中,需要对业务操作数据进行保护,由于使用数据库锁可能会引起一些难以预料的问题,因此考虑使用内存锁,设计思想:在内存中使用一个 ...

  9. HDU3364 Lanterns(求矩阵的秩)

    求矩阵的秩,及判断有无解 #include<cstdio> #include<iostream> #include<cstdlib> #include<cst ...

  10. android 入门-android Studio 解决方案

    一.当提示 解决方案: 1. 2. 二.从这步到这步 的时候,可能遇见下面的问题. 解决方案: 更新一下build-tools 19.1.0版本 放到你的sdk里并重启as. 三. 当遇见这样的情况 ...