Exclusive-OR(带权并查集)
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 |
Sample Output
Case 1: |
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(带权并查集)的更多相关文章
- POJ 1703 Find them, Catch them(带权并查集)
传送门 Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42463 Accep ...
- [NOIP摸你赛]Hzwer的陨石(带权并查集)
题目描述: 经过不懈的努力,Hzwer召唤了很多陨石.已知Hzwer的地图上共有n个区域,且一开始的时候第i个陨石掉在了第i个区域.有电力喷射背包的ndsf很自豪,他认为搬陨石很容易,所以他将一些区域 ...
- poj1417 带权并查集 + 背包 + 记录路径
True Liars Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2713 Accepted: 868 Descrip ...
- poj1984 带权并查集(向量处理)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5939 Accepted: 2 ...
- 【BZOJ-4690】Never Wait For Weights 带权并查集
4690: Never Wait for Weights Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 88 Solved: 41[Submit][ ...
- hdu3038(带权并查集)
题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 题意: n表示有一个长度为n的数组, 接下来有m行形如x, y, d的输入, 表示 ...
- 洛谷OJ P1196 银河英雄传说(带权并查集)
题目描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦 创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山 ...
- poj1984 带权并查集
题意:有多个点,在平面上位于坐标点上,给出一些关系,表示某个点在某个点的正东/西/南/北方向多少距离,然后给出一系列询问,表示在第几个关系给出后询问某两点的曼哈顿距离,或者未知则输出-1. 只要在元素 ...
- poj1611 带权并查集
题意:病毒蔓延,现在有 n 个人,其中 0 号被认为可能感染,然后给出多个社交圈,如果某个社交圈里有人被认为可能被感染,那么所有这个社交圈里的人都被认为可能被感染,现在问有多少人可能被感染. 带权并查 ...
- hdu 1829-A Bug's LIfe(简单带权并查集)
题意:Bug有两种性别,异性之间才交往, 让你根据数据判断是否存在同性恋,输入有 t 组数据,每组数据给出bug数量n, 和关系数m, 以下m行给出相交往的一对Bug编号 a, b.只需要判断有没有, ...
随机推荐
- UVW源码漫谈(番外篇)—— Emitter
这两天天气凉了,苏州这边连续好几天都是淅淅沥沥的下着小雨,今天天气还稍微好点.前两天早上起来突然就感冒了,当天就用了一卷纸,好在年轻扛得住,第二天就跟没事人似的.在这里提醒大家一下,天气凉了,睡凉席的 ...
- PYTHON 函数局部变量和全局变量
有这样一段PYTHON代码,从事C语言开发的人都知道,如果定义了全局变量,而函数内没有定义同名的函数变量的话,那么在函数内对该变量的赋值就是对全局变量空间数值的修改, 然后在PYTHON中却不尽相同, ...
- Java随机数的使用
在java中实现随机数的类有两种,分别是和java.util.Math 和 java.util.Random 第一种:java.lang.Math.random() Math.random()方法创建 ...
- End up with More Teams UVA - 11088
End up with More Teams Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu ...
- 课程作业02(关于Java的几点讨论)
---恢复内容开始--- 1.一个Java类文件中真的只能有一个公有类吗? public class Test { public static void main(String[] args) { } ...
- golang 标准库间依赖的可视化展示
简介 国庆看完 << Go 语言圣经 >>,总想做点什么,来加深下印象.以可视化的方式展示 golang 标准库之间的依赖,可能是一个比较好的切入点.做之前,简单搜了下相关的内 ...
- 在前端页面对easyui中的datagrid与jqgrid加载后的数据进行操作
因为项目的需求,需要在grid中加载数据后再在前端页面执行操作,所以在easyui中的grid与jqgrid都进行了测试和操作: eayui中grid数据的操作: //构造集合对象 var list ...
- ubuntu命令查询版本和内核版本
1.查看ubuntu版本号: 方法一: cat /etc/issue 返回结果: Ubuntu 6.06.2 LTS \n \l 方法二: sudo lsb_release -a 返回结果: ...
- List之Union(),Intersect(),Except() 即并集,交集,差集运算。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 浅谈Java多态
什么是Java中的多态?又是一个纸老虎的概念,老套路,把它具体化,细分化,先想三个问题(注意,这里不是简单的化整为零,而是要建立在学习一个新概念时的思考框架): 1.这个东西有什么用?用来干什么的?它 ...