Find them, Catch them(并查集)
http://poj.org/problem?id=1703
题意:有两个黑帮团伙,共n名团伙成员(不知道属于这两个黑帮中的哪一个)。现在警察有一些信息,每条信息包含2个人的编号,如果给出A a b,则输出a b的关系,即是否属于同一个黑帮;
如果给出D a b,则说明a b属于不同的黑帮。
思路:典型的并查集,只要两者的关系确定了,就将他们放入同一个集合内,而另外增加一个表示关系的数组link[]来表示该节点与其父亲的关系,0表示同一类,1表示不同团伙。初始时集合只有自己一个元素,link[] 初始为0。
第一次用c++的输入输出,TLE了。。改成C的就A了。
TLE 代码:
#include <iostream>
#include <string>
using namespace std;
const int N=;
int f[N],link[N];
int n,m; int find(int x)
{
int t = f[x];
if (x!=f[x])
f[x] = find(f[x]);
link[x] = (link[x]==link[t] ? : );
return f[x]; }
void merge(int x,int y,int fu,int fv)
{
f[fu] = fv;
link[fu] = (link[x]==link[y] ? : );
}
void init()
{
for (int i = ; i <= n; i ++)
{
f[i] = i;
link[i] = ;
} }
int main()
{
int T;
cin>>T;
while(T--)
{ cin>>n>>m;
init();
while(m--)
{
int u,v;
char s;
cin>>s>>u>>v;
int fu = find(u);
int fv = find(v);
if (s=='A')
{
if(fu!=fv)
{
cout<<"Not sure yet."<<endl;
continue;
}
if (link[u]==link[v])
{
cout<<"In the same gang."<<endl;
continue;
}
cout<<"In different gangs."<<endl;
continue;
}
if (s=='D')
{
if (fu!=fv)
merge(u,v,fu,fv);
} }
}
return ;
}
AC代码:
#include <stdio.h>
const int N=;
int f[N],link[N];
int n,m; int find(int x)
{
int t = f[x];
if (x!=f[x])
f[x] = find(f[x]);
link[x] = (link[x]==link[t] ? : );
return f[x]; }
void merge(int x,int y,int fu,int fv)
{
f[fu] = fv;
link[fu] = (link[x]==link[y] ? : );
}
void init()
{
for (int i = ; i <= n; i ++)
{
f[i] = i;
link[i] = ;
} }
int main()
{
int T;
scanf("%d",&T);
while(T--)
{ scanf("%d%d%*c",&n,&m);
init();
while(m--)
{
int u,v;
char s;
scanf("%c%d%d%*c",&s,&u,&v);
int fu = find(u);
int fv = find(v);
if (s=='A')
{
if(fu!=fv)
{
printf("Not sure yet.\n");
continue;
}
if (link[u]==link[v])
{
printf("In the same gang.\n");
continue;
}
printf("In different gangs.\n");
continue;
}
if (s=='D')
{
if (fu!=fv)
merge(u,v,fu,fv);
} }
}
return ;
}
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 (并查集)
题目: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 并查集的应用
题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...
- 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 ...
- 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 ...
随机推荐
- 6、scala Map和Tuple
1. 创建Map 2.访问Map元素 3.修改Map元素的值 4.遍历Map 5.SortedMap和LinkedHashMap 6.Map的元素类型Tuple 1. 创建Map 创建不可变的Ma ...
- 5.C#编写Redis访问类
那么通过前面几篇博文,服务端的安装和配置应该没什么问题了,接下来的问题是如何通过代码来访问Redis. 这里我们使用的库为: StackExchange.Redis GitHub:https://gi ...
- css特殊效果
border-radius实现特殊形状 .box{ width: 100px; height: 100px; background: orange; border: 1px solid #000; b ...
- js 获取 checkbox[] 值
$("#btnAdd1").click(function () { console.log($("form").serialize()); var checkb ...
- Python星号表达式
有时候可能想分解出某些值然后丢弃它们,可以使用诸如 _ 或者 ign(ignored)等常用来表示待丢弃值的变量名: record = ('ACME', 50, 123.45, (12, 18, 20 ...
- 安装部署NetBeans mysql Tomact joget workflow 环境
一.安装joget workflow 1.安装jdk 下载jdk http://www.oracle.com/technetwork/java/javase/downloads/index.html ...
- (C/C++学习)8.C++ Lambda
一.生成随机数字 假设我们有一个vector<int>容器,想用100以内的随机数初始化它,其中一个办法是通过generate函数生成,如代码1所示.generate函数接受三个参数,前两 ...
- 【JAVA】AES加密
import java.security.SecureRandom; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; imp ...
- 史上最详细的CentOS 7 安装 HBase教程
1. 前半部分参考 https://www.cnblogs.com/ivictor/p/5906433.html 2.问题 namenode无法启动,参考 https://stackoverflow. ...
- Google Cloud SSH 连接配置
设置当前用户及root用户的密码 sudo passwd xx-user # 输入新密码 sudo passwd root # 输入新密码(建议保持一样) 在本地生成私钥和公钥 cd ~/.ssh s ...