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 ...
随机推荐
- JavaScript:颜色辨别
<script> //参考文章:http://www.cnblogs.com/xuechenlei/p/5940729.html //游戏页面:http://www.webhek.com/ ...
- 【sqli-labs】 less60 GET -Challenge -Double Query -5 queries allowed -Variation3 (GET型 挑战 双查询 只允许5次查询 变化3)
http://192.168.136.128/sqli-labs-master/Less-60/?id=1")%23 http://192.168.136.128/sqli-labs-mas ...
- C# 字符串到字节数组,字节数组转整型
; , ); byte[] bytes = BitConverter.GetBytes(num);//将int32转换为字节数组 num = BitConverter.ToInt32(bytes, ) ...
- ipv4的设置
有段时间自己的网总是连不上,别人的都可以,因为公司又wifi,就将就着用wifi了,没有去查看原因,后来由于公司1网段大部分ip号被占用,系统要接入32路主机测试,每个主机都要分配ip,只好开辟2网段 ...
- windows下使用批处理设置环境变量
1. 设置临时环境变量 set BAT_HOME=c:\bat 此命令只对当前窗口有效,批处理或cmd窗口一关闭,变量就恢复原来的值了. 2. 设置永久环境变量 方法一 setx BAT_HOME C ...
- 配置Android的NDK开发环境(eclipse)
ndk下载地址: http://blog.csdn.net/zhanghuoding/article/details/51345256 在eclipse设置ndk位置 右键你的工程,android t ...
- vi 命令学习(三)
[末行命令] 末行命令主要是针对文件进行操作的:保存.退出.保存&退出.搜索&替换.另存.新建.浏览文件 命令 ...
- 【转载】Linux下各文件夹的含义和用途
原文地址:https://www.cnblogs.com/lanqingzhou/p/8037269.html Linux下各文件夹的含义和用途 Linux根目录”/“下各个系统文件夹的含义和用途 1 ...
- Linux 开启443端口
1 在Linux终端输入指令: iptables -I INPUT -p tcp --dport 443 -j ACCEPT 2 回车之后继续输入指令,输入保存防火墙配置指令: service ...
- mysql中having和where区别?
having和where有相似之处但也有区别,都是设定条件的语句. 在查询过程中,聚合语句(sum,min,max,avg,count),要比having子句有限执行. 在查询过程中,要先执行wher ...