(用树结构支持并查集8.2.2)POJ 1703 Find them, Catch them(并查集的简单使用: 判断两个元素是否属于同一集合)
2013-11-05 15:54
573 查看
/* * POJ_1703.cpp * * Created on: 2013年11月5日 * Author: Administrator */ #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int maxn = 100000; int set[maxn*2+10]; int set_find(int p){//并查集的查找过程 if(set[p] < 0){ return p; } return set[p] = set_find(set[p]); } int main(){ int t; scanf("%d",&t); while(t--){ memset(set,-1,sizeof(set));//开始时,每个罪犯都是一个集合 int n,m; scanf("%d%d",&n,&m); char str[15]; int a,b; int i; for(i = 0 ; i < m ; ++i){ scanf("%s %d %d",str,&a,&b); if(str[0] =='A'){ //如果a与b不是同一帮派&&a与b的另一个帮派也不是同一个帮派 if(set_find(a) != set_find(b) && set_find(a) != set_find(b+n)){ printf("Not sure yet.\n"); }else if(set_find(a) == set_find(b)){//如果a与b属于同一个帮派 printf("In the same gang.\n"); }else{ printf("In different gangs.\n"); } }else{ if(set_find(a) != set_find(b+n)){ set[set_find(a)] = set_find(b+n);//将a的帮派设为b的另外一个帮派 set[set_find(b)] = set_find(a+n); } } } } return 0; }
相关文章推荐
- (使用树结构来支持并查集操作8.4.2)POJ 2236 Wireless Network(判断两个元素是否在同一个集合&&有条件的合并)
- (用树结构支持并查集8.2.2)POJ 1988 Cube Stacking(并查集的使用:判断一个集合中元素的个数---->>某一个元素的下面还有多少个元素)
- POJ 1703 Find them, Catch them【典型并查集:判断在不同的集合】
- POJ 1703 Find them, Catch them【典型并查集:判断在不同的集合】
- poj 1703 [ 并查集 不同帮派 仅仅是判断是否是属于同一帮派 code orz ]
- (POJ 1703) Find them, Catch them【典型并查集:判断在不同的集合】
- POJ 1703 Find them, Catch them 并查集 (关系问题 判断是否在一个集合)
- POJ 1703 Find them, Catch them 并查集 (关系问题 判断是否在一个集合)
- POJ 1703 Find them, Catch them 并查集 (关系问题 判断是否在一个集合)
- ccsu1359 木棒相交 (叉积线段判交,并查集判断是否属于同一个集合)
- POJ 1703 Find them, Catch them 并查集 (关系问题 判断是否在一个集合)
- POJ 1703 Find them, Catch them 并查集 (关系问题 判断是否在一个集合)
- POJ 1703 Find them, Catch them 并查集 (关系问题 判断是否在一个集合)
- (使用树结构来支持并查集操作8.4.4)POJ 2524 Ubiquitous Religions(并查集的基本操作: 求有多少个相互独立的集合)
- POJ 1703 Find them, Catch them 并查集 (关系问题 判断是否在一个集合)
- POJ 1703 Find them, Catch them 并查集 (关系问题 判断是否在一个集合)
- (使用树结构支持并查集8.4.1)UVA 10608 FRIENDS(查看一个集合中有多少个元素)
- 判断两个IP是否属于同一子网
- poj 1703 Find them, Catch them(带权并查集)
- POJ 1703 Find them, Catch them【带种类的并查集】