题目: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. GNOME与KDE的战争

    目录1 序言2 GNOME与KDE交替发展% M" O/ h% R( b  f, ~7 W' n9 V, G3 GNOME获得商业公司的支持4 KDE3.5可实现半透明和阴影效果,界面华丽. ...

  2. ORA-06550:line 1,column 7;PLS-00201:indentifer '存储过程' must be declared;...PL/SQL Statement ignored 问题

    前段时间由于修改SMES系统,出现了一个问题. ORA-06550:line 1,column 7;PLS-00201:indentifer '存储过程' must be declared;...PL ...

  3. QT for android 比较完美解决 全屏问题

    项目用到QT qml,需要在android下面全屏显示,折腾了一晚上,搞定,分享下,希望能帮助他人. 参考 Qt on Android:让 Qt Widgets 和 Qt Quick 应用全屏显示 该 ...

  4. UVA 10954 Add All 哈夫曼编码

    题目链接: 题目 Add All Time Limit:3000MS Memory Limit:0KB 问题描述 Yup!! The problem name reflects your task; ...

  5. VMware虚拟机中Hadoop服务的端口无法访问的问题

    今天安装了一个hadoop集群,因为已经在单个虚拟机上安装成功,所以初期安装相对顺利. 初始环境如下:       通过Vmware Esxi服务器虚拟机出来四台机器,每台机器的网络配置如下:     ...

  6. MVC3 Model Binding验证方式

    1.使用ModelState在Action中进行验证 [HttpPost] public ViewResult MakeBooking(Appointment appt) { if (string.I ...

  7. trie树(前缀树)

    问题描述:   Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的优 ...

  8. 【C++之STL】理解容器(ing)

    “容器可容纳一些数据的模板类” “容器是包容其他对象的对象” 两种类型:顺序容器.关联容器   顺序容器 关联容器 访问成员 顺序访问和随机访问 经过优化关键键值访问                 ...

  9. Linq to Entity中连接两个数据库时要注意的问题

    Linq to Entity中连接两个数据库时要注意的问题 今天大学同学问了我一个问题,Linq to Entity中连接两个数据库时,报错“指定的 LINQ 表达式包含对与不同上下文关联的查询的引用 ...

  10. hdu 4111 Alice and Bob 博弈论

    这里有2种方法: 方法一:求SG函数 sg[i][j]:i表示1的个数,j表示合并操作的步数. 这共有4种操作: 1.消除一个1: 2.减掉一个1: 3.合并2个1: 4.把1合并到另外不是1中. 代 ...