hdu 3234 并查集
虚拟一个根节点n,设其值为0.并且始终保持其为根。
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Maxn 40010
#define Maxm 100010
#define LL __int64
#define Abs(x) ((x)>0?(x):(-x))
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define inf 0x7fffffff
#define Mod 1000000007
using namespace std;
int fa[Maxn],val[Maxn],n;
map<int,int> num;
void init()
{
for(int i=;i<Maxn;i++){
fa[i]=i;
val[i]=;
}
}
int find(int x)
{
if(fa[x] == x)
return x;
int t = find(fa[x]);
val[x] ^= val[fa[x]];
fa[x] = t;
return t;
}
int merg(int a,int b,int v)
{
int x=find(a);
int y=find(b);
if(x==y){
return (val[a]^val[b])==v;
} //cout<<x<<" * "<<y<<endl;
if(x==n){
fa[y]=x;
val[y]=val[a]^val[b]^v;
return ;
}
if(y==n){
fa[x]=y;
val[x]=val[a]^val[b]^v;
return ;
}
fa[x]=y;
val[x]=val[a]^val[b]^v;
return ;
}
int main()
{
int q,i,j,x,y,v,cnt=,k,ans,ok,Case=;
char str[],ch[];
while(scanf("%d%d",&n,&q)!=EOF,n||q){
init();
int err=;
cnt=;
printf("Case %d:\n",++Case);
for(i=;i<=q;i++){
scanf("%s",ch);
if(ch[]=='I'){
gets(str);
if(err) continue;
++cnt;
if(sscanf(str,"%d %d %d",&x,&y,&v)==){
v=y;
y=n;
//cout<<x<<" "<<y<<" "<<v<<endl;
if(!merg(x,y,v)){
err=,printf("The first %d facts are conflicting.\n",cnt);
}
}
else {
//cout<<x<<" "<<y<<" "<<v<<endl;
if(!merg(x,y,v)){
err=,printf("The first %d facts are conflicting.\n",cnt);
}
}
}
else {
scanf("%d",&k);
ans=;
ok=;
num.clear();
for(j=;j<=k;j++){
scanf("%d",&x);
if(err) continue;
int t=find(x);
ans^=val[x];
//cout<<t<<" "<<x<<" "<<find(0)<<endl;
if(t!=n){
if(num[t]%==){
ok++;
num[t]++;
}else ok--,num[t]--;
}
}
if(err) continue;
if(!ok){
printf("%d\n",ans);
}else{
printf("I don't know.\n");
}
}
}
printf("\n");
}
return ;
}
hdu 3234 并查集的更多相关文章
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- HDU 3926 并查集 图同构简单判断 STL
给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...
- HDU 4496 并查集 逆向思维
给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- HDU 2860 并查集
http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...
- hdu 1198 (并查集 or dfs) Farm Irrigation
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...
- hdu 1598 (并查集加贪心) 速度与激情
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...
- hdu 4496(并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...
- 2015多校第6场 HDU 5361 并查集,最短路
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...
随机推荐
- C:冒泡排序
冒泡排序 C语言在运行的时候,不会帮我们检查数组的下标. 冒泡排序:有n个数字,需要进行 n - 1 趟比较大小(元素的个数减去 1).外层循环 for( int i = 0; i < coun ...
- ab性能并发测试语法
ab测试语法ab -n 全部请求数 -c 并发数 测试url 例如:ab -n 10000 -c 1000 http://myweb.com/test.html Server Software: Ap ...
- 六分钟学会创建Oracle表空间的步骤
经过长时间学习创建Oracle表空间,于是和大家分享一下,看完本文你肯定有不少收获,希望本文能教会你更多东西. 1.先查询空闲空间 select tablespace_name,file_id,blo ...
- The J1850 Core
http://www.digidescorp.com/wp-content/uploads/2012/02/J1850-Datasheet-pdf.pdf
- uva301 - Transportation
Transportation Ruratania is just entering capitalism and is establishing new enterprising activiti ...
- clientTop scrollTop offsetTop
关于top.clientTop.scrollTop.offsetTop的用法 网页可见区域宽: document.body.clientWidth;网页可见区域高: document.body.cli ...
- Codeforces Beta Round #75 (Div. 1 Only) B. Queue 线段树+二分
B. Queue Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 codeforces.com/problemset/problem/91/B Descrip ...
- 【KPC】关于为什么不用Zepto而用JQuery
1.zepto在window phone手机上不是很兼容 2.JQuery的Deferred对象在zepto上不支持. 3.JQuery经过压缩,以及部分页面的使用,以及缓存,可以达到优化用户体验的效 ...
- iOS开发——开发必备OC篇&UITableView设置界面完整封装(一)
UITableView设置界面完整封装(一) 简单MVC实现UITableView设置界面分组数据显示 一:模型 1:cell模型 /** 描述cell图片 */ @property (nonatom ...
- C++调用C#库简单例程
有些时候,为了使用别人已经写好的C#库文件,我们需要使用C++调用C#库文件: 以下做了一简单的调用工程,步骤如下: 1.准备C#库 (dll文件) 1)创建C#库: 2)编写C#类: ...