POJ1703-Find them, Catch them 并查集构造
好久没有做并查集的题,竟然快把并查集忘完了。
题意:大致是有两个监狱,n个犯人,m次操作,每次操作可以是查询也可以是确定两个人是否在同一个监狱里。
思路:其实这题完全可能做出来,结果就是没做出来。理清思路还是很好想的,我们用一个并查集来把能够确定在同一监狱里的人放在一个集合里,用一个diff数组把确定不同监狱的两个犯人分别与对方不同监狱的犯人放在一个集合里,就是这么简单。
const int N=1e5+10;
int f[N],dif[N];
char s[5];
void init()
{
for(int i=0;i<N;i++) f[i]=i;
memset(dif,-1,sizeof(dif));
}
int find(int x)
{
return f[x]==x?x:f[x]=find(f[x]);
}
int main()
{
int t,n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
int u,v;
while(m--)
{
scanf("%s%d%d",s,&u,&v);
if(s[0]=='A')
{
if(find(u)==find(v)) printf("In the same gang.\n");
else if(find(dif[u])==find(v)||find(dif[v])==find(u)) printf("In different gangs.\n");
else printf("Not sure yet.\n");
}
else
{
if(dif[u]==-1) dif[u]=v;
if(dif[v]==-1) dif[v]=u;
int uu=find(dif[u]),vv=find(dif[v]);
f[uu]=find(v);
f[vv]=find(u);
}
}
}
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 ...
- HDU-1829 A Bug's Life。并查集构造,与POJ1709异曲同工!
A Bug's Life Find them, Catch them 都是并查集构造的题,不久前 ...
- 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: ...
- 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 ...
随机推荐
- Linux 命令与学习
2014-10-10 ps -ef|grep *** 可以查找包含***名称的进程 netstat -ntlp 查看端口占用 kill -9 pid 强制杀死进程 ...
- [转]ASP.NET MVC URL重写与优化(进阶篇)-继承RouteBase玩转URL
本文转自:http://www.cnblogs.com/John-Connor/archive/2012/05/03/2478821.html 引言-- 在初级篇中,我们介绍了如何利用基于ASP.NE ...
- SpringBoot 2.x (8):模板引擎
SpringBoot中有很多的starter:本质是多个JAR包集合 比如我们常用的: <dependency> <groupId>org.springframework.bo ...
- client系列、offset系列、scroll系列
一.client系列 clientWidth/clientHeight 是我们设置的宽和高加上内边距(没有边框) clientLeft/clientTop 就是我们设置的边框值 二.offset ...
- java 获取ip地址
1.使用WIFI 首先设置用户权限 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"& ...
- Smart SVN的使用
最近项目使用了SVN,为管理代码起到了很好的作用!但是,对于很多初步使用着,还是非常不容易! 公司使用的是Smart SVN 客户端. Smart SVN 这个工具总体还是挺不错的! 在代码的提交和获 ...
- grep的几个参数
-a 在二进制问就爱你中,以文本方式进行搜索 -c 计算找到搜索字符串的次数 -i 忽略大小写 -n 输出行号 -v 反向选择,即没有显示搜索字符串内容的那一行 grep -n '\.$' file ...
- Oracle逻辑备份与恢复(Data Pump)
1. 备份的类型 按照备份方式的不同,可以把备份分为两类: 1.1 逻辑备份:指通过逻辑导出对数据进行备份.将数据库中的用户对象导出到一个二进制文件中,逻辑备份使用导入导出工具:EXPDP/IMPDP ...
- SQL 导出csv
https://jingyan.baidu.com/album/4b07be3c466b5d48b280f37f.html?picindex=9
- css 两列 左侧列固定 width: 100px; float: left; 右侧列自适应 margin-left:100px; 注意要用在div上的style
css 两列 左侧列固定 width: 100px; float: left; 右侧列自适应 margin-left:100px; 注意要用在div上的style .con1{ width: 100p ...