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点走到距 ...
随机推荐
- winForm开发
http://www.cnblogs.com/wuhuacong/p/3199829.html http://www.cnblogs.com/peterzb/archive/2009/06/30/15 ...
- 个推推送Android问题检测
1.获取不到CID问题: 1. 查看配置文件是否有问题,appkey.appsecret.appid是否有空格存在. 2. 相关权限是否全部添加. 3. manifest ...
- 微价值:专訪《甜心爱消除》个人开发人员Lee,日入千元!
[导语]我们希望能够对一些个人开发人员进行专訪,这样大家更能显得接地气,看看人家做什么,怎么坚持.<甜心爱消除>作者Lee是三群的兄弟,也关注微价值.微价值的文章还是能够的,得到一些业内大 ...
- Eclipse配置PyDev插件
安装python解释器 安装PyDev: 首先需要去Eclipse官网下载:http://www.eclipse.org/,Eclipse需要JDK支持,如果Eclipse无法正常运行,请到Java官 ...
- CodeForces 176B Word Cut dp
Word Cut 题目连接: http://codeforces.com/problemset/problem/176/C Description Let's consider one interes ...
- 从来没有天才 靠自己创造未来——Leo鉴书(29)
之前在网上跟朋友们聊起天才这个话题,我认来从来没什么所谓天才,有朋友认为有的,只是我们定义不同,要不你看看苏轼? 持天才论者持两个观点:有些人天生擅长干某些事儿,也许是基因作怪:有些人的能力是上帝或者 ...
- [Angular2 Router] Exiting an Angular 2 Route - How To Prevent Memory Leaks
In this tutorial we are going to learn how we can accidentally creating memory leaks in our applicat ...
- 标准库 - fmt/format.go 解读
// Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a B ...
- 1.6.3 Uploading Data with Solr Cell using Apache Tika
1. Uploading Data with Solr Cell using Apache Tika solr使用Apache Tika工程的代码提供了一个框架,用于合并所有不同格式的文件解析器为so ...
- iOS 左右滑动 手势 响应方法
1. @property (nonatomic, strong) UISwipeGestureRecognizer *leftSwipeGestureRecognizer; @property (no ...