POJ 1703 Find them, Catch them(并查集,等价关系)
DisjointSet保存的是等价关系,对于某个人X,设置两个变量Xa,Xb。Xa表示X属于a帮派,Xb类似。
如果X和Y不是同一个帮派,那么Xa -> Yb,Yb -> Xa...
(Xa,Yb)对称性,传递性,自反都满足。所以可以用合并到一个集合里面。
判断就比较简单了。
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
//#include<bits/stdc++.h>
using namespace std; const int maxn = 2e5+;
int pa[maxn];
int hei[maxn]; int fdst(int x){ return x==pa[x]?x:pa[x]=fdst(pa[x]); }
inline void mrge(int a,int b)
{
int s1 = fdst(a), s2 = fdst(b);
if(s1 != s2){
if(hei[s1] < hei[s2])
pa[s1] = s2;
else {
pa[s2] = s1;
if(hei[s1] == hei[s2]) hei[s1]++;
}
}
}
inline bool same(int a,int b){ return fdst(a) == fdst(b); } void initDisjointSet(int n)
{
for(int i = ; i <= n; i++){
pa[i] = i;
hei[i] = ;
}
} //#define LOCAL
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif
int T; cin>>T;
while(T--){
int n, m; scanf("%d%d",&n,&m);
initDisjointSet(n*);
while(m--){
char ch[];
int a,b;
scanf("%s%d%d",ch,&a,&b);
if(*ch == 'D'){
mrge(a,b+n);
mrge(a+n,b);
}else {
if(same(a,b+n)){
puts("In different gangs.");
}else if(same(a,b)){
puts("In the same gang.");
}else puts("Not sure yet.");
}
}
}
return ;
}
POJ 1703 Find them, Catch them(并查集,等价关系)的更多相关文章
- 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(并查集)
Find them, Catch them Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
- POJ 1703 Find them, catch them (并查集)
题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2 D 3 4 D 5 6...这就至少有3个集合了.并且 ...
- POJ 1703 Find them, Catch them 并查集的应用
题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...
- POJ 1703 Find them, Catch them(并查集高级应用)
手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...
- POJ 1703 Find them, Catch them 并查集,还是有点不理解
题目不难理解,A判断2人是否属于同一帮派,D确认两人属于不同帮派.于是需要一个数组r[]来判断父亲节点和子节点的关系.具体思路可参考http://blog.csdn.net/freezhanacmor ...
- [并查集] POJ 1703 Find them, Catch them
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43132 Accepted: ...
- POJ 1703 Find them, Catch them(种类并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41463 Accepted: ...
- hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them
http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...
- POJ 1703 Find them, Catch them (数据结构-并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31102 Accepted: ...
随机推荐
- 799C(xjb)
题目链接: http://codeforces.com/problemset/problem/799/C 题意: 有c, d两种货币, 有 n 个货物, 可以用 c 货币或者 d 货币购买, 现在需要 ...
- heap 堆
实现了交换.向上维护,向下维护的原子功能,其它插入.删除.修改的功能应该不在话下. 于是有了代码:(luogu3378模板题) // luogu-judger-enable-o2 #include & ...
- Isolation Forest算法实现详解
本文介绍的 Isolation Forest 算法原理请参看我的博客:Isolation Forest异常检测算法原理详解,本文中我们只介绍详细的代码实现过程. 1.ITree的设计与实现 首先,我们 ...
- java socket 网络通信 指定端口的监听 多线程 乱码
Java Socket编程 对于Java Socket编程而言,有两个概念,一个是ServerSocket,一个是Socket.服务端和客户端之间通过Socket建立连接,之后它们就可以进行通信了.首 ...
- php静态方法和属性
静态方法和属性由static关键字定义 静态方法和属性不用实例化也可以直接访问,如 self::test(),self::tt 类实例化后可以访问静态方法,但是不可以访问静态属性 声明类属性或方法为静 ...
- spring mvc做上传图片,文件小于10k就不生成临时文件了
这是spring-mvc.xml中的 <bean id="multipartResolver" class="org.springframework.web.mul ...
- POJ1013 Counterfeit Dollar
题目来源:http://poj.org/problem?id=1013 题目大意:有12枚硬币,其中有一枚假币.所有钱币的外表都一样,所有真币的重量都一样,假币的重量与真币不同,但我们不知道假币的重量 ...
- Abp 添加阿里云短信发送
ABP中有短信发送接口ISmsSender public interface ISmsSender { Task<string> SendAsync(string number, stri ...
- mysql sql语句集锦
1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- 创建 备份 ...
- <s:property>的用法
1,访问Action值栈中的普通属性: <s:property value="attrName"/> 2,访问Action值栈中的对象属性(要有get set方法): ...