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 ...
随机推荐
- python web 开发学习路线
转载,备着 自己目前学习python web 开发, 经过两个月的摸索,目前对web开发有了浅显的认识,把自己的学习过程贴出来.1.python入门推荐老齐<从零开始学python>,&l ...
- 通过python xlsxwriter模块生成EXCEL柱状图、饼图
xlsxwriter模块不是python自带的,使用pip下载 import xlsxwriter #新建一个excel文件,起名为expense01.xlsx workbook = xlsxwrit ...
- centos vm 桥接 --网络配置
/etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 BOOTPROTO=none BROADCAST=192.168.1.255 HWADDR= ...
- Git学习总结三(工作区和暂存区、撤销修改)
工作区和暂存区 工作区(Working Directory) 就是你在电脑里能看到的目录,比如我的learngit文件夹就是一个工作区: 版本库(Repository) 工作区有一个隐藏目录.git, ...
- 使用Python的Flask框架,结合Highchart,动态渲染图表
服务端动态渲染图表 参考文章链接:https://www.highcharts.com.cn/docs/dynamic-produce-html-page 参考文章是使用php写的,我这边改用pyth ...
- java归并排序
代码如下: public class MergeSort { public static void mergeSort(DataWrap [] data) { sort(data , 0 , data ...
- 【5】Django项目配置settings.py详解
夫唯不争,故天下莫能与之争 --老子<道德经> 本节内容 1.项目配置文件settings.py介绍 2.数据库配置[MySQL] 3.创建模型对象并和数据库同步 4.python官方提供 ...
- 【codeforces 509E】Pretty Song
[题目链接]:http://codeforces.com/contest/509/problem/E [题意] 让你计算一个字符串的所有子串里面元音字母出现的频率的和; [题解] 先处理出前缀和-&g ...
- 详解Pattern类和Matcher类
java正则表达式通过java.util.regex包下的Pattern类与Matcher类实现(建议在阅读本文时,打开java API文档,当介绍到哪个方法时,查看java API中的方法说明,效果 ...
- natural join 以及 v$statname , v$sessstat
oracle natural join是一个比较方便的用法.如果两个表的某些字段名称相同,类型相同,natural join就会把他们做等值连接.比如下面我们知道这两个视图的结构如下: SQL> ...