题目:http://poj.org/problem?id=1703

题意:一个地方有两个帮派, 每个罪犯只属于其中一个帮派,D 后输入的是两个人属于不同的帮派,

A后询问 两个人是否属于 同一个帮派。

用op[]数组记录 不跟自己在一个帮派中的一个人, 然后再输入不跟自己在一个帮派的人的时候,把

这个人跟 op[]数组记录里的那个人联系起来, 这两个人属于一个帮派。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxn = ;
int bin[maxn], op[maxn], n;
void init()
{
for(int i = ; i <= n; i++)
{
bin[i] = i;
op[i] = ;
}
}
int find(int x)
{
int r, i, j;
r = x;
while(r != bin[r])
r = bin[r];
i = x;
while(bin[i] != r)
{
j = bin[i];
bin[i] = r;
i = j;
}
return r;
}
/*
int find(int x) //递归 路径压缩写法
{
return bin[x]==x?x:(bin[x]=find(bin[x]));
}
*/
void merge(int x, int y)
{
int fx, fy;
fx = find(x);
fy = find(y);
if(fx != fy)
bin[fx] = fy;
}
int main()
{
int t, m, a, b;
char ch;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &m);
init(); while(m--)
{
getchar();
scanf("%c%d%d",&ch, &a, &b);
if(ch == 'A')
{
int x, y;
x = find(a);
y = find(b);
if(x == y)
printf("In the same gang.\n");
else if(op[a]!=&&find(op[a])==y || op[b]!=&&find(op[b])==x)
printf("In different gangs.\n");
else
printf("Not sure yet.\n");
}
else
{
if(op[a] == )
op[a] = b;
else
merge(op[a],b);
if(op[b] == )
op[b] =a;
else
merge(op[b],a);
}
}
}
return ;
}

poj 1703 Find them, Catch them(并查集)的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. POJ 1703 Find them, Catch them 并查集,还是有点不理解

    题目不难理解,A判断2人是否属于同一帮派,D确认两人属于不同帮派.于是需要一个数组r[]来判断父亲节点和子节点的关系.具体思路可参考http://blog.csdn.net/freezhanacmor ...

  7. [并查集] POJ 1703 Find them, Catch them

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

  8. POJ 1703 Find them, Catch them(种类并查集)

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

  9. hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them

    http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...

  10. POJ 1703 Find them, Catch them (数据结构-并查集)

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

随机推荐

  1. VS2013中自动缩进和注释的快捷键

    自动缩进: ctrl +k 再 ctrl +f 注释: ctrl+k 再 ctrl +c 取消注释: ctrl+k 再 ctrl+u

  2. python学习小结8:I/O

    文件I/O是Python中最重要的技术之一,在Python中对文件进行I/O操作是非常简单的. 打印到屏幕上 >>> print "python is really a g ...

  3. 兼容iOS 10 资料整理笔记-b

    原文链接:http://www.jianshu.com/p/0cc7aad638d9 1.Notification(通知) 自从Notification被引入之后,苹果就不断的更新优化,但这些更新优化 ...

  4. Virtualbox虚拟机设置不完全笔记

    先说说我想实现的东西,我想在虚拟机安装各种开发环境,我个人在学习Node.然后我装了一个Ubuntu Server,所以我又想共享一个windows下的文件夹,这样可以让我在windows下开发,在L ...

  5. 2876: [Noi2012]骑行川藏 - BZOJ

    Description 蛋蛋非常热衷于挑战自我,今年暑假他准备沿川藏线骑着自行车从成都前往拉萨.川藏线的沿途有着非常美丽的风景,但在这一路上也有着很多的艰难险阻,路况变化多端,而蛋蛋的体力十分有限,因 ...

  6. Highcharts-3.0.6

    Highcharts-3.0.6 报表插件

  7. c++ switch case

    http://www.cnblogs.com/RealOnlyme/articles/2579628.html

  8. java.util.ConcurrentModificationException 解决办法(转)

    今天在项目的中有一个需求,需要在一个Set类型的集合中删除满足条件的对象,这时想当然地想到直接调用Set的remove(Object o)方法将指定的对象删除即可,测试代码:   public cla ...

  9. VIM Taglist安装配置和使用

    问题描述:            VIM  Taglist安装于配置 问题解决:             (1)安装Taglist包      (2)解压taglist压缩包         (3)将 ...

  10. 2014 Multi-University Training Contest 8

    官方解题报告:http://blog.sina.com.cn/s/blog_a19ad7a10102uzj7.html Area of Mushroom http://acm.hdu.edu.cn/s ...