poj1703 - Find them, Catch them
来源:互联网 发布:什么时候错过淘宝 编辑:程序博客网 时间:2024/05/16 00:33
想看更多的解题报告:http://blog.csdn.net/wangjian8006/article/details/7870410
转载请注明出处:http://blog.csdn.net/wangjian8006
题目大意:在这个城市里有两个黑帮团伙,现在给出N个人,问任意两个人他们是否在同一个团伙
输入D x y代表x于y不在一个团伙里
输入A x y要输出x与y是否在同一团伙或者不确定他们在同一个团伙里
虽然是用并查集做的,但是那个类别偏移和特殊解一直没考虑到,无限WA后看到别人的才晓得当N=2时他们属于不同的帮派,因为题目有说两个帮派至少有一个人
这里不能用模拟要用并查集的原因是,可能根据输入有好几个集合,但是如果两个不属于同一个集合的话就不能确定他们是否在同一个帮派
而把一个集合合并到另一个集合的话,就需要把x根节点的ans变成和y根节点ans相对的,然后在查找的时候要修改ans值,因为生成的树每一层和隔层的ans值是相对的(0和1)
因为ans的值只能为0和1(只有两个帮派),所以类别偏移可以用位运算
/*Memory 904KTime 360MS*/#include <stdio.h>#define MAXV 100010int ans[MAXV],tree[MAXV];int find(int x){ int rt; if(tree[x]!=x){ rt=find(tree[x]); ans[x]=ans[x]^ans[tree[x]];//类别偏移 return tree[x]=rt; } return x;}void TUnion(int x,int y){ int fx,fy; fx=find(x); fy=find(y); tree[fx]=fy; ans[fx]=~(ans[y]^ans[x]);//类别偏移}int main(){ int i,n,m,a,b; char c; int t; scanf("%d",&t); while(t--){ scanf("%d%d\n",&n,&m); for(i=0;i<=n;i++){ ans[i]=0; tree[i]=i; } for(i=1;i<=m;i++){ scanf("%c %d %d\n",&c,&a,&b); if(c=='D'){ TUnion(a,b); }else{ if(n==2) //特殊解 printf("In different gangs.\n"); else if(find(a)==find(b)){ if(ans[a]==ans[b]) printf("In the same gang.\n"); else printf("In different gangs.\n"); }else printf("Not sure yet.\n"); } } } return 0;}
- poj1703 - Find them, Catch them
- POJ1703 Find them, Catch them
- poj1703 Find them, Catch them
- poj1703 Find them, Catch them
- POJ1703 Find them,Catch them
- POJ1703--Find them, Catch them
- poj1703 find them,catch them
- Find them, Catch them poj1703
- POJ1703 Find them, Catch them
- poj1703 Find them, Catch them
- poj1703 Find them, Catch them
- POJ1703-Find them, Catch them
- POJ1703 Find them, Catch them
- poj1703 Find them, Catch them
- poj1703 Find them, Catch them
- poj1703——Find them, Catch them
- poj1703——Find them, Catch them
- Find them, Catch them (poj1703)
- JavaScript实现x秒后自动跳转
- Java 设计模式
- 请求http网络,返回的Http协议的状态码
- 设置网络连接
- java中String,StrignBuffer和StringBuilder之间的区别
- poj1703 - Find them, Catch them
- php.js版
- Ubuntu Server关机的方法
- 手机4G网络(网速超级震撼)
- xml基础_Schema(2)
- C# 拓展字符串,枚举类型
- andorid 我们该如何管理你的内存?
- 并查集题目总结
- POJ 2029 Get Many Persimmon Trees (标记)