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. Fiddler抓包工具使用详解

    一.Fiddler简介 Fiddler是最强大最好用的Web调试工具之一, 它能记录所有客户端和服务器的http和https请求.允许你监视.设置断点.甚至修改输入输出数据.Fiddler包含了一个强 ...

  2. 简单实用的CSS网页布局中文排版技巧

    由于汉字的特殊性,在css网页布局中,中文排版有别于英文排版.排版是一个麻烦的问题,小编认为,作为一个优秀的网页设计师和网页制作人员,掌握一些简单的中文排版技巧是不可或缺的,所以今天特意总结了几个简单 ...

  3. VS2015 + EF6连接MYSQL

    ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案,不仅支持SQL Server,还支持MySQL.Ora ...

  4. sqoop使用的问题

    找不到表 17/05/02 18:15:47 ERROR tool.ImportTool: Imported Failed: There is no column found in the targe ...

  5. hdu4632

    Palindrome subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65535 K (Java/ ...

  6. IFrame父页面和子页面的交互

    现在在页面里面用到iframe的情况越来越少了,但有时还是避免不了,甚至这些页面之间还需要用js来做交互,那么这些页面如何操作彼此的dom呢?下面将会逐步介绍. 1.父页面操作子页面里面的dom 下面 ...

  7. 如何创建一个Django项目

    Django 软件框架 软件框架是由其中的各个模块组成,每个模块负责特定的功能,模块与模块之间相互协作来完成软件开发. MVC简介 MVC框架的核心思想是:解耦,让不同的代码块之间降低耦合,增强代码的 ...

  8. 自签名的https证书是不安全的

    一.项目内的需求 我们做的app都是企业级的应用,而企业级的应用的下载需要遵循itms协议,itms协议下需要https链接,这就需要你的服务器支持https的协议,该协议需要申请SSL证书,我们测试 ...

  9. MVVM命令绑定原理

    跟据网上前辈们的资料.了解到命令在MVVM绑定有三种行式. 1.DelegateCommand 2.RelayCommand 3.AttachbehaviorCommand /// <summa ...

  10. 使用hive客户端java api读写hive集群上的信息

    上文介绍了hdfs集群信息的读取方式,本文说hive 1.先解决依赖 <properties> <hive.version>1.2.1</hive.version> ...