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 ...
随机推荐
- BZOJ 1587: 叶子合并leaves
题目大意:求n个数分成k段的最小代价. 题解:DP,没什么好说的. 代码: #include<cstdio> #include<algorithm> using namespa ...
- go的相关用法
1. have gone to和have been to的区别 have gone to和have been to的区别如下: 1.have gone to,第三人称时用 has gone to ha ...
- Knockout v3.4.0 中文版教程-10-绑定-控制文本内容和外观-visible绑定
4.绑定 1. 控制文本内容和外观 1. visible绑定 目的 visible绑定可以根据你传入绑定的值控制关联的DOM元素显示或隐藏. 例子 <div data-bind="vi ...
- 【14】PNG,GIF,JPG的区别及如何选
[14]PNG,GIF,JPG的区别及如何选 GIF: 8位像素,256色 无损压缩 支持简单动画 支持boolean透明 适合简单动画 JPEG: 颜色限于256 有损压缩 可控制压缩质量 不支持透 ...
- cf898d Alarm Clock
区间上有 \(n\) 个点,问你为达到目的:长度为 \(m\) 的区间内点的个数都 $ < k$需要去掉多少个点. 贪心.每个区间我们总是去掉最后的,也就是说除非万不得已我们是不会去掉点的. 队 ...
- Python requests模块params、data、json的区别
json和dict对比 json的key只能是字符串,python的dict可以是任何可hash对象(hashtable type): json的key可以是有序.重复的:dict的key不可以重复. ...
- 高级java、C#、php、SQL、JavaScript......+n多编程语言学习分享
/*入园两周年纪念.在搬砖之路一去不返*/ //搬砖什么都好,就是有点伤Ctrl键. <div style="display:none;"> </div>
- Educational Codeforces Round 24
A. Diplomas and Certificates time limit per test 1 second memory limit per test 256 megabytes input ...
- [BZOJ2393] Cirno的完美算数教室(dfs+容斥原理)
传送门 先通过dfs预处理出来所有只有2和9的数,也就大概2000多个. 想在[L,R]中找到是这些数的倍数的数,可以通过容斥原理 那么如果a % b == 0,那么便可以把 a 去掉,因为 b 的倍 ...
- 【loj6191】「美团 CodeM 复赛」配对游戏
题目 显然期望dp. 简单想法: f[i][j]表示前i个人中向右看并且没有被消除的人数的概率 如果第i+1个人是向右,$f[i+1][j+1]=f[i][j]/2$ 如果第i+1个人是向左,$f[i ...