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(并查集,等价关系)的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. POJ 1703 Find them, Catch them 并查集,还是有点不理解

    题目不难理解,A判断2人是否属于同一帮派,D确认两人属于不同帮派.于是需要一个数组r[]来判断父亲节点和子节点的关系.具体思路可参考http://blog.csdn.net/freezhanacmor ...

  7. [并查集] POJ 1703 Find them, Catch them

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

  8. POJ 1703 Find them, Catch them(种类并查集)

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

  9. 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条臭 ...

  10. POJ 1703 Find them, Catch them (数据结构-并查集)

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

随机推荐

  1. eros --- Windows Android真机调试

    1.下载并安装JDK 2.下载并安装Android Studio 上面两项不管用weex还是eros都是前置条件,度娘有大量教程. 开始eros 手脚架安装: $ npm i -g eros-cli ...

  2. mybatis笔记 - 初始配置及dao的封装

    1.用户实体类 package com.javasm.entity; /** * *TODO 用户表实体类 * @author CaoLei 2018年6月26日上午10:50:12 * Manage ...

  3. linux线程私有数据---TSD池

    进程内的所有线程共享进程的数据空间,所以全局变量为所有线程共有.在某些场景下,线程需要保存自己的私有数据,这时可以创建线程私有数据(Thread-specific Data)TSD来解决.在线程内部, ...

  4. windows系统IIS7环境下如何部署MVC项目

    首先打开IIS:第一步:添加MVC程序映射 打开其中的:处理程序映射,如下图: 点击界面右边操作中的:添加脚本映射,弹出下图: 请求路径:*           可执行文件:c:/Windows/Mi ...

  5. Linux 常用命令一览

    本篇博文讲述系统内核.Bash解释器的关系与作用,如何正确的执行Linux命令以及常见排错方法. 经验丰富的运维人员可以恰当的组合命令与参数,使Linux字符命令更加的灵活且相对减少消耗系统资源. 强 ...

  6. spark sql 导出数据

    如果用户希望在spark sql 中,执行某个sql 后,将其结果集保存到本地,并且指定csv 或者 json 格式,在 beeline 中,实现起来很麻烦.通常的做法是将其create table ...

  7. 总结工作中用到的ES6语法,方便工作中查看,也总结一下经验

    1.模板字符串: 表现形式:${} 举例子: import axios from 'axios'; let base = 'https://www.baidu.com/home/msg/data/pe ...

  8. linux环境下jdk部署配置

    1.java官网下载相关的jdk包 2.配置系统环境变量,编辑/etc/profile文件,在文件的末尾添加一下信息: export JAVA_HOME=/usr/jdk1.8.0_101export ...

  9. 安装mongodb并配置

    下载网址http://dl.mongodb.org/dl/win32/x86_64 mongodb-win32-x86_64-2008plus-ssl-v3.4-latest.zip 解压d盘命名mo ...

  10. AssetDatabase的方法总结

    AssetDatabase的方法总结 1.AssetDatabase.FindAssets public static string[] FindAssets(string filter);publi ...