Exclusive-OR

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 152 Accepted Submission(s): 55
 
Problem Description
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:

 
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 220. 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.
 
 
Source
2009 Asia Wuhan Regional Contest Hosted by Wuhan University
 
Recommend
chenrui
 
/*
题意:有n个数,但是n个数还没确定,有两种操作:
I:p v告诉你Xp的值。
I:p q v告诉你Xp^Xq=v。
Q:k p1 p2...pk 询问 Xp1^Xp2...^Xpk=?
如果I操作有矛盾请指出。 初步思路:带权并查集。r[i]数组表示i与根节点的异或值,查询的时候,r[a]^r[b]实际上就是a^b的值,因为,父节点的已经
已经异或掉了,很显然如果是奇数个肯定是求不出来的
*/
#include<bits/stdc++.h>
using namespace std;
int n,m,k;
char op[];
int num[];//存放各种数据
int father[];//存储i的父节点
int r[];//存放i与父节点的异或
int vis[];//记录访问过的点
int ca=;
int u,v,c;
char str[];
int times=;//用来记录I操作进行了几次
int findx(int x){
if(x!=father[x])
{
int fx = father[x];
father[x] = findx(father[x]);
r[x]^=r[fx];
}
return father[x];
}
bool Union(int x,int y,int z){
int fx=findx(x);
int fy=findx(y);
if(fx==fy){
if((r[x]^r[y])!=z)
return false;
return true;
}
if(fx==n) swap(fx,fy);
father[fx]=fy;
r[fx] = r[x]^r[y]^z;
return true;
}
int query(){
memset(vis,,sizeof vis);
int ans=;
for(int i=;i<k;i++){
if(vis[i]) continue;
int cnt=;
int root=findx(num[i]);
for(int j=i;j<k;j++) {
if(!vis[j] && root == findx(num[j])){
vis[j] = ;
cnt++;
ans^=r[num[j]];
}
}
if(root!=n&&(cnt&)){
return -;
}
}
return ans; }
void init(){
memset(r,,sizeof r);
for(int i=;i<=n;i++){
father[i]=i;
}
times=;
}
int main(){
// freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m)!=EOF&&(n+m)){
init();
printf("Case %d:\n",ca++);
bool flag=false;//表示命令的正确性
for(int i=;i<m;i++){
scanf("%s",op);
// cout<<op<<" ";
getchar();
if(op[]=='I'){
times++;
gets(str);
// cout<<str<<endl;
if(flag) continue;
int cur=;
for(int i=;str[i];i++){
if(str[i]==' ') cur++;
}
if(cur==){//u c
sscanf(str,"%d%d",&u,&c);
v=n;
// cout<<u<<" "<<c<<endl;
}else{//u v c
sscanf(str,"%d%d%d",&u,&v,&c);
// cout<<u<<" "<<v<<" "<<c<<endl;
}
if(!Union(u,v,c))
{
printf("The first %d facts are conflicting.\n",times);
flag = true;
}
}else{
scanf("%d",&k);
// cout<<k<<" ";
for(int i=;i<k;i++){
scanf("%d",&num[i]);
// cout<<num[i]<<" ";
}
// cout<<endl;
if(flag) continue;
int ans = query();
if(ans == -)
printf("I don't know.\n");
else
printf("%d\n",ans);
}
}
// for(int i=0;i<n;i++){
// cout<<father[i]<<" ";
// }
// cout<<endl;
printf("\n");
}
return ;
}

Exclusive-OR(带权并查集)的更多相关文章

  1. POJ 1703 Find them, Catch them(带权并查集)

    传送门 Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42463   Accep ...

  2. [NOIP摸你赛]Hzwer的陨石(带权并查集)

    题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...

  3. poj1417 带权并查集 + 背包 + 记录路径

    True Liars Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2713   Accepted: 868 Descrip ...

  4. poj1984 带权并查集(向量处理)

    Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5939   Accepted: 2 ...

  5. 【BZOJ-4690】Never Wait For Weights 带权并查集

    4690: Never Wait for Weights Time Limit: 15 Sec  Memory Limit: 256 MBSubmit: 88  Solved: 41[Submit][ ...

  6. hdu3038(带权并查集)

    题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 题意: n表示有一个长度为n的数组, 接下来有m行形如x, y, d的输入, 表示 ...

  7. 洛谷OJ P1196 银河英雄传说(带权并查集)

    题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山 ...

  8. poj1984 带权并查集

    题意:有多个点,在平面上位于坐标点上,给出一些关系,表示某个点在某个点的正东/西/南/北方向多少距离,然后给出一系列询问,表示在第几个关系给出后询问某两点的曼哈顿距离,或者未知则输出-1. 只要在元素 ...

  9. poj1611 带权并查集

    题意:病毒蔓延,现在有 n 个人,其中 0 号被认为可能感染,然后给出多个社交圈,如果某个社交圈里有人被认为可能被感染,那么所有这个社交圈里的人都被认为可能被感染,现在问有多少人可能被感染. 带权并查 ...

  10. hdu 1829-A Bug's LIfe(简单带权并查集)

    题意:Bug有两种性别,异性之间才交往, 让你根据数据判断是否存在同性恋,输入有 t 组数据,每组数据给出bug数量n, 和关系数m, 以下m行给出相交往的一对Bug编号 a, b.只需要判断有没有, ...

随机推荐

  1. iOS根据域名获取ip地址

    引入头文件 #include <netdb.h> #include <sys/socket.h> #include <arpa/inet.h> //根据域名获取ip ...

  2. 三、js的函数

    三.函数 函数是定义一次但却可以调用或执行任意多次的一段JS代码.函数有时会有参数,即函数被调用时指定了值的局部变量.函数常常使用这些参数来计算一个返回值,这个值也成为函数调用表达式的值. 1.函数声 ...

  3. 替换应用程序DLL动态库的详细方法步骤 (gts.dll为例)

    在C++ builder编译器IDE软件下 1.View -Project Manageer --找到需要替换的x.dll(gts.dll)对应的x.lib(gts.lib),然后Remove2.Pr ...

  4. Spring3.2不支持jdk8

    解决方案: http://stackoverflow.com/questions/24128045/spring-context-initialization-failed-with-java-lan ...

  5. ssh (免密码登录、开启服务)

    ssh 无密码登录要使用公钥与私钥.linux下可以用用ssh-keygen生成公钥/私钥对,下面我以Unbutun为例.有机器A(192.168.1.155),B(192.168.1.181).现想 ...

  6. HDU1197 Specialized Four-Digit Numbers

    进制转化 hdu1197 #include<cstdio> #include<cstdlib> #include<iostream> #include<mem ...

  7. jQuery扩展easyui.datagrid,添加数据loading遮罩效果代码

    //jquery.datagrid 扩展加载数据Loading效果 (function (){ $.extend($.fn.datagrid.methods, { //显示遮罩 loading: fu ...

  8. WPF 验证没有通过无法保存数据(非常好)+ 虚似数据库

    Validation control with a single validation rule is easy, but what if we need to validate a control ...

  9. Table样式设置

    <table class="listTable"> <tr><th width="40px">序号</th>&l ...

  10. 如何使用 C# 爬虫获得专栏博客更新排行

    昨天,梦姐问我们,她存在一个任务,找到 关注数排行100 和 浏览量排行100 的专栏博客,在2017年还有更新的专栏. 梦姐说他要出去一趟,M大神在吃饭,于是我估算时间,只有半个钟. 整理一下:半个 ...