做过一些的带权并查集,再来做所谓的“种类并查集",发现好像就顿悟了。

种类并查集与带权并查集实质上的区别并不大。 关键的区别就是种类并查集仅仅是带权并查集再弄个%取余操作而已。然后余数就表示他属于哪个种类。

这题仅仅有两个种类,也就是仅仅有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 【并查集】的更多相关文章

  1. poj1703 Find them, Catch them 并查集

    poj(1703) Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26992   ...

  2. 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 ...

  3. POJ 1703 Find them, catch them (并查集)

    题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2  D 3 4 D 5 6...这就至少有3个集合了.并且 ...

  4. poj1703--Find them, Catch them(并查集应用)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32073   Accepted: ...

  5. POJ1703-Find them, Catch them 并查集构造

                                             Find them, Catch them 好久没有做并查集的题,竟然快把并查集忘完了. 题意:大致是有两个监狱,n个 ...

  6. POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

    POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...

  7. POJ 1703 Find them, Catch them 并查集的应用

    题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...

  8. poj1703_Find them, Catch them_并查集

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42451   Accepted: ...

  9. poj.1703.Find them, Catch them(并查集)

    Find them, Catch them Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...

  10. POJ 1703 Find them, Catch them(并查集高级应用)

    手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...

随机推荐

  1. ci $this->load->database()

    http://pengbotao.cn/codeigniter-database.html

  2. 使用systemctl命令管理服务mysql

    启动mysql systemctl start mysqld.service 停止mysql systemctl stop mysqld.service 重启mysql systemctl resta ...

  3. x86 idt

    Interrupt/trap gate

  4. Leetcode 330.按要求补齐数组

    按要求补齐数组 给定一个已排序的正整数数组 nums,和一个正整数 n .从 [1, n] 区间内选取任意个数字补充到 nums 中,使得 [1, n] 区间内的任何数字都可以用 nums 中某几个数 ...

  5. Azure Storage Blob文件名区分大小写

    最近在使用Azure Storage的时候发现Storage的命名是区分大小写的,导致我们系统在更新图片的时候有时候更新不上,最终通过判断处理文件名解决. 因此我们在使用Storage需要注意一下文件 ...

  6. 性能学习之--loaderunner中run-time setting常用功能

    先打开run-time setting界面 一.Run Logic   设置迭代次数,只控制action的迭代次数,init和end只执行一次. 如果迭代次数设置10,10个并发用户,那么init和e ...

  7. 【Luogu】P1383高级打字机

    可持久化线段树模板题之一. 权当温习主席树模板 #include<cstdio> #include<cstdlib> #include<cctype> #defin ...

  8. POJ 2396 Budget ——有上下界的网络流

    给定矩阵的每行每列的和,和一些大于小于等于的限制.然后需要求出一组可行解. 上下界网络流. 大概的思想就是计算出每一个点他需要强行流入或者流出的量,然后建出超级源点和汇点,然后删除下界,就可以判断是否 ...

  9. 刷题总结——火柴排队(NOIP2013)

    题目: 题目背景 NOIP2013 提高组 Day1 试题 题目描述 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间 ...

  10. 刷题总结——系列维护(ssoi)

    题目: 题解: 题解如上图,至于计算大于s的数字的数量和小于s数字的和用权值线段树或者树状数组维护就行了···注意离散化 另外发现cout和puts比printf快好多····· 代码: #inclu ...