POJ 1703 Find them,Catch them ----种类并查集(经典)
http://blog.csdn.net/freezhanacmore/article/details/8774033?reload 这篇讲解非常好,我也是受这篇文章的启发才做出来的。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 100100 int fa[N],same[N]; void makeset(int n)
{
for(int i=;i<=n;i++)
{
fa[i] = i;
same[i] = ;
}
} int findset(int x)
{
if(x != fa[x])
{
int tmp = fa[x];
fa[x] = findset(fa[x]);
same[x] = (same[x] + same[tmp])%;
}
return fa[x];
} int unionset(char s,int a,int b)
{
int x = findset(a);
int y = findset(b);
if(x == y) //属于同一个集合
{
if(s == 'A')
{
if(same[a] == same[b])
{
return ; //same
}
else
{
return ; //different
}
}
}
else //不属于同一个集合
{
if(s == 'A')
{
return ; //unsure
}
else if(s == 'D')
{
fa[x] = y;
same[x] = (same[a] + same[b] + )%;
}
}
return ; //要加,这是对其他情况的处理,因为此函数必须返回一个值
} int main()
{
int t,n,m,i;
char ss[];
int a,b;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
makeset(n);
for(i=;i<m;i++)
{
scanf("%s %d %d",ss,&a,&b);
if(unionset(ss[],a,b) == )
{
cout<<"In the same gang."<<endl;
}
else if(unionset(ss[],a,b) == )
{
cout<<"In different gangs."<<endl;
}
else if(unionset(ss[],a,b) == )
{
cout<<"Not sure yet."<<endl;
}
}
}
return ;
}
POJ 1703 Find them,Catch them ----种类并查集(经典)的更多相关文章
- POJ 1703 Find them, Catch them(种类并查集)
题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...
- poj 1703 Find them, Catch them 【并查集 新写法的思路】
题目地址:http://poj.org/problem?id=1703 Sample Input 1 5 5 A 1 2 D 1 2 A 1 2 D 2 4 A 1 4 Sample Output N ...
- poj 1703 Find them, Catch them(并查集)
题目:http://poj.org/problem?id=1703 题意:一个地方有两个帮派, 每个罪犯只属于其中一个帮派,D 后输入的是两个人属于不同的帮派, A后询问 两个人是否属于 同一个帮派. ...
- POJ 1703 Find them, Catch them (并查集)
题意:有N名来自两个帮派的坏蛋,已知一些坏蛋两两不属于同一帮派,求判断给定两个坏蛋是否属于同一帮派. 思路: 解法一: 编号划分 定义并查集为:并查集里的元素i-x表示i属于帮派x,同一个并查集的元素 ...
- POJ 1703 Find them, Catch them(并查集拓展)
Description The police office in Tadu City decides to say ends to the chaos, as launch actions to ro ...
- POJ 1703 Find them, Catch them(并查集,等价关系)
DisjointSet保存的是等价关系,对于某个人X,设置两个变量Xa,Xb.Xa表示X属于a帮派,Xb类似. 如果X和Y不是同一个帮派,那么Xa -> Yb,Yb -> Xa... (X ...
- POJ1703Find them, Catch them[种类并查集]
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42416 Accepted: ...
- [poj1703]Find them, Catch them(种类并查集)
题意:食物链的弱化版本 解题关键:种类并查集,注意向量的合成. $rank$为1代表与父亲对立,$rank$为0代表与父亲同类. #include<iostream> #include&l ...
- POJ:1703-Find them, Catch them(并查集好题)(种类并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49867 Accepted: 153 ...
随机推荐
- java事务理解
还在学Hibernate,后续一大堆概念刚接触需要理解.觉得-——事务——这个概念不是很好理解,所以发上来记录一下. 首先说点千篇一律的东西.概念和特性都是随处可见的,无论哪里都很容易找到,关键是你如 ...
- PowerBuilder反编译
最近需要了解某个PowerBuilder程序如何工作的,这已经是某个时代的产物了.除了EXE之外,还有一些PBD文件.PBD文件是PowerBuilder动态库,作为本地DLL的一个替 ...
- git 上传项目到github
1.本地新建文件夹GIT,Git Bash打开命令窗口, ①git config --global user.name "名字" eg: git config --global ...
- 【JavaEE】Hibernate继承映射,不用多态查询只查父表的方法
几个月前,我在博问里面发了一个问题:http://q.cnblogs.com/q/64900/,但是一直没有找到好的答案,关闭问题以后才自己解决了,在这里分享一下. 首先我重复一下场景,博问里面举的动 ...
- OSX cordova+Ionic的安装配置
0.安装前确定你的系统安装了node和xcode 1.cordova (1)安装cordova npm config set registry http://registry.cnpmjs.org s ...
- 与成都的幸福行动家交流GTD
今年第四次来成都了,通过<小强升职记>的作者邹鑫的撮合,与成都的幸福行动家何平取得了联系,2014年12月6日下午给几个小伙伴们分享了GTD3年来的一点体会.有几位刚接触GTD的朋友,也有 ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q63-Q65)
Question 63You have a SharePoint farm that has more than 100 custom Features.You upgrade several Fea ...
- 迭代器iterator(三):Listlterator遍历arraylist,并用逆序输出结果
迭代器(iterator) 是一种对象,它能够用来遍历标准模板库容器中的部分或全部元素,每个迭代器对象代表容器中的确定的地址.迭代器修改了常规指针的接口,所谓迭代器是一种概 念上的抽象:那些行为上像迭 ...
- Instruments指南:如何调试内存泄露
Instruments指南:如何调试内存泄露 开篇 现在,你应该使用的ARC,而不是原来我们使用的MRC或者其他.但是我们在使用ARC的时候也会出现内存泄露的情况. 幸运的是,苹果为我们提供了Inst ...
- vector,arraylist, linkedlist的区别是什么
LinkedList类 LinkedList实现了List接口,允许null元素. 此外LinkedList提供额外的get,remove,insert方法在LinkedList的首部或尾部. Lin ...