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. ssl协议以及生成

    一.https协议https是一安全为目标的httpt通道,简单讲师http的安全版.即http下加入ssl层,https的安全基础是ssl,因此加密的详细内容就需要ssl.http和https的区别 ...

  2. iOS开发中KVC、KVO简介

    在iOS开发中,KVC和KVO是经常被用到的.可以使用KVC对对象的属性赋值和取得对象的属性值,可以使用KVO监听对象属性值的变化.简单介绍一下KVC和KVO. 一:键值编码(KVC) KVC,全称 ...

  3. 最小覆盖_KEY

    最小覆盖(cover)..线段树 [题目描述] 给定 N 个区间[Li,Ri],需要你按照顺序选出一个区间序列使得[1,M]完全被覆盖.并且在选出来的序列中,某个区间[a,b]之前必须保证[1,a]都 ...

  4. Apache服务器处理404错误页面技巧

    1.打开Apache目录,查找httpd.conf文件 2.打开httpd.conf文件,找到<Directory "    "></Directory>这 ...

  5. 初始Socket编程(python)

    通信双方要有一个服务端和一个客户端,所以要分开去写代码. 所以我创建了两个py程序,第一个是服务端:iServer.py 和客户端 iClient.py 服务端: #coding:utf-8from ...

  6. [Tjoi2013]循环格

    [Tjoi2013]循环格 2014年3月18日1,7500 Description Input 第一行两个整数R,C.表示行和列,接下来R行,每行C个字符LRUD,表示左右上下. Output 一个 ...

  7. java中集合的增删改操作及遍历总结

      集合的增删改操作及遍历总结

  8. Ubuntu 16安装GPU版本tensorflow

    pre { direction: ltr; color: rgb(0, 0, 0) } pre.western { font-family: "Liberation Mono", ...

  9. 苹果手机使用替代onkeyup的方法

    今天项目有这个问题,苹果手机就不行   使用keyup事件检测文本框内容:  $('#keyup_i').bind('keyup', function(){         $('#keyup_s') ...

  10. Linux下安装jdk8的详细步骤

    一.登录Linux,切换到root用户 sudo su 二.在usr目录下建立java安装目录 cd /usr mkdir java 三.下载jdk 登录网址:http://www.oracle.co ...