poj1703 Find them,Catch them 【并查集】
做过一些的带权并查集,再来做所谓的“种类并查集",发现好像就顿悟了。
种类并查集与带权并查集实质上的区别并不大。 关键的区别就是种类并查集仅仅是带权并查集再弄个%取余操作而已。然后余数就表示他属于哪个种类。
这题仅仅有两个种类,也就是仅仅有0和1两种, 对于两个不同的种类,那么之间的权值是相差1的,所以依照带权并查集的方法做加上1。然后取余2就可以。
#include<cstdio> const int N = 100005;
int n, m, f[N], rank[N]; inline void init(){
for(int i=1; i<=n; ++i)
f[i]=i,rank[i]=0;
} int find(int x){
if(x==f[x])return f[x];
int fa=f[x];
f[x] = find(f[x]);
rank[x] = (rank[x]+rank[fa])&1;
return f[x];
} inline bool Union(int x,int y){
int a=find(x), b=find(y);
if(a==b) return false;
f[b] = a;
rank[b] = (rank[x]-rank[y]+1)&1;
} int main(){
int T,a,b,fa,fb;
char ch;
scanf("%d",&T);
while(T--){
scanf("%d%d%*c",&n,&m);
init();
for(int i=0; i<m; ++i){
scanf("%c%d%d%*c",&ch,&a,&b);
if(ch=='D'){
Union(a,b);
}
else{
fa = find(a), fb=find(b);
if(fa==fb){
if(rank[a]==rank[b]) puts("In the same gang.");
else puts("In different gangs.");
}
else
puts("Not sure yet.");
}
}
}
return 0;
}
poj1703 Find them,Catch them 【并查集】的更多相关文章
- poj1703 Find them, Catch them 并查集
poj(1703) Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26992 ...
- POJ-1703 Find them, Catch them(并查集&数组记录状态)
题目: The police office in Tadu City decides to say ends to the chaos, as launch actions to root up th ...
- POJ 1703 Find them, catch them (并查集)
题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2 D 3 4 D 5 6...这就至少有3个集合了.并且 ...
- poj1703--Find them, Catch them(并查集应用)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32073 Accepted: ...
- POJ1703-Find them, Catch them 并查集构造
Find them, Catch them 好久没有做并查集的题,竟然快把并查集忘完了. 题意:大致是有两个监狱,n个 ...
- POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集
POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...
- POJ 1703 Find them, Catch them 并查集的应用
题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...
- poj1703_Find them, Catch them_并查集
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42451 Accepted: ...
- poj.1703.Find them, Catch them(并查集)
Find them, Catch them Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
- POJ 1703 Find them, Catch them(并查集高级应用)
手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...
随机推荐
- luogu1640 [SCOI2010]连续攻击游戏
二分图匹配,一边是属性值,一边是武器 #include <iostream> #include <cstring> #include <cstdio> using ...
- Redis学习笔记01---配置文件
1.配置文件用法 启动redis的时候指定配置⽂件路径: ./redis-server /path/to/redis.conf 不指定配置⽂件的时候使⽤内置配置⽂件启动,此⽅法仅适⽤于开发和测试. 2 ...
- WordPress添加前台注册功能
一.添加注册表单 1.首先在当前主题的目录下新建一个php文件,命名为reg-page.php,然后将page.php中的所有代码复制到reg-page.php中: 2.删除reg-page.php开 ...
- python--getitem一拦截索引运算
getitem一拦截索引运算 __getitem__方法拦截实例的索引运算.当实例x出现在x[i]这样的索引运算中时,Python会调用这个实例继承的__getitem__方法(如果有的话),把x作为 ...
- CSAPP学习笔记—虚拟内存
CSAPP学习笔记—虚拟内存 符号说明 虚拟内存地址寻址 图9-12展示了MMU如何利用页表来实现这种映射.CPU中的一个控制寄存器,页表基址寄存器(Page Table Base Register, ...
- Leetcode 337.大家结舍III
打家劫舍III 在上次打劫完一条街道之后和一圈房屋后,小偷又发现了一个新的可行窃的地区.这个地区只有一个入口,我们称之为"根".除了"根"之外,每栋房子有且只有 ...
- poj2945 Find the Clones
Find the Clones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 8490 Accepted: 3210 D ...
- Laravel 5 Form 和 HTML 的使用
最近在用 laravel 5 做例子,在做到表单的时候,习惯性的使用 Form::open() 结果发现提示错误,没有这个类, 好吧,找了找,发现 在laravel 5 中,把 from 和 html ...
- linux-起步
学习网站: linux中国开源社区 Vmware下载与安装 https://blog.csdn.net/Ywaken/article/details/78839005 https://blog.csd ...
- FastDFS上传/下载过程[转载-经典图列]
FastDFS上传/下载过程: 首先客户端 client 发起对 FastDFS 的文件传输动作,是通过连接到某一台 Tracker Server 的指定端口来实现的,Tracker Server 根 ...