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 ...
随机推荐
- CNN结构:色彩空间建模-色彩空间分析
原文: 色彩空间基础 好一个NB的知乎专栏:色彩空间基础 第一章:色彩空间基础 关于色彩分析,引出了专门的数学基础.整个过程给出了完备的数学阐述,虽然没有试验数据,论述的相当精彩. 摘抄出一段: 上 ...
- SPPNet论文翻译-空间金字塔池化Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition
http://www.dengfanxin.cn/?p=403 原文地址 我对物体检测的一篇重要著作SPPNet的论文的主要部分进行了翻译工作.SPPNet的初衷非常明晰,就是希望网络对输入的尺寸更加 ...
- 【centOS7】Jenkins安装--漫漫踩坑路
安装步骤: https://www.cnblogs.com/h--d/p/5673085.html 安装后遇到的问题及解决办法: jenkins的admin用户的初始密码路径 https://blog ...
- /etc目录常用配置文件
/etc/resolv.conf DNS客户端配置文件,逐渐被网卡配置文件所替代 /etc/hosts 本机DNS解析文件,优先级高于DNS服务器 /etc/hostname CentOS 7 主机名 ...
- 2018年为什么要学习Python?Python还有前景吗?
近年来,Python一直是当仁不让的开发入行首选,无论是职位数量.就业广度还是使用排行都远超其他语言,而且Python语言接近自然语言,学习起来非常的轻松简便,因此也越来越受到人们的欢迎.进入到201 ...
- DOClever线下部署安装说明文档
先本地要安装node环境,推荐6.10.0版本 到nodejs官网进行下载window版本进行安装,这里就不做说明了 接下来我们开始安装mongodb,首先下载mongodb 下载完成后我们一路 ...
- 配置sublime text 前端环境
SublimeLinter是Sublime的一个代码检测工具插件.安装前台是配置好node环境 1,在sublime text安装 SublimeLinter 按下 Ctrl+Shift+p 进入 C ...
- 记一个简单的webpack.config.js
module.exports = { entry: './basic/app.js', output: { path: './assets/', filename: '[name].bundle.js ...
- redo allocation latch redo copy latch
这两个latch 是干什么的一直有点迷糊,刚才上网查了一下,总结如下: redo allocation latch 在Log Buffer中分配内存空间时需要获取Redo allocation lat ...
- iphone照片查看器
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...