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(并查集)的更多相关文章

  1. poj1703 Find them, Catch them 并查集

    poj(1703) Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26992   ...

  2. POJ 1703 Find them, catch them (并查集)

    题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2  D 3 4 D 5 6...这就至少有3个集合了.并且 ...

  3. poj1703--Find them, Catch them(并查集应用)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32073   Accepted: ...

  4. POJ1703-Find them, Catch them 并查集构造

                                             Find them, Catch them 好久没有做并查集的题,竟然快把并查集忘完了. 题意:大致是有两个监狱,n个 ...

  5. POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

    POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...

  6. POJ 1703 Find them, Catch them 并查集的应用

    题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...

  7. 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 ...

  8. poj1703_Find them, Catch them_并查集

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42451   Accepted: ...

  9. poj.1703.Find them, Catch them(并查集)

    Find them, Catch them Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...

  10. POJ 1703 Find them, Catch them(并查集高级应用)

    手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...

随机推荐

  1. Java_Web三大框架之Struts2

    今天正式接触Java_Web三大框架之Struts2框架.对于初学者来说,先来了解什么是框架技术: 一.“框架技术”帮我们更快更好地构建程序: 1.是一个应用程序的半成品 2.提供可重用的公共结构 3 ...

  2. 黑苹果开启retina,大分辨率的方法

    首先,管理分辨率RDM的软件这里下载: http://pan.baidu.com/s/1bpjL07P 在终端输入: curl -o ~/enable-HiDPI.sh https://raw.git ...

  3. BZOJ 2626: JZPFAR KDtree + 堆

    Code: #include<bits/stdc++.h> #define maxn 200000 #define inf 1000000000000000 #define mid ((l ...

  4. B.3 字典

    在框架中,字典的选择要比列表少得多.只有三个主流的非并发 IDictionary<TKey, TValue> 实现,此外还有 ExpandoObject (第14章已介绍过). Concu ...

  5. Array的内置方法思维导图整理(JavaScript)

    按照MDN整理的数组部分的思维导图,主要目的是方便查漏补缺,所以写的不是很详细.

  6. MySQL中是索引

    MySQL中是索引: --.唯一索引: 一行中的内容不能一样, create t2( id int , num int, unique weiyisuiyin (id,num) ) --唯一; --约 ...

  7. 2.使用term filter搜索数据

    主要知识点 根据用户ID.是否隐藏.帖子ID.发帖日期来搜索帖子 一.准备数据 1.插入一些测试帖子数据     POST /forum/article/_bulk { "index&quo ...

  8. Keil-MDK编译完成后代码大小

    Code 代表执行的代码,程序中所有的函数都位于此处. RO-data 代表只读数据,程序中所定义的全局常量数据和字符串都位于此处. RW-data 代表已初始化的读写数据,程序中定义并且初始化的全局 ...

  9. 【ACM】hdu_zs3_1008_Train Problem I_201308100835

    Train Problem I Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)Tota ...

  10. mybatis原理流程

    无论是用过的hibernate,mybatis,你都可以法相他们有一个共同点: 从配置文件(通常是XML配置文件中)得到 sessionfactory. 由sessionfactory  产生 ses ...