题目: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. bnuoj 16493 Just Pour the Water(矩阵快速幂)

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=16493 [题解]:矩阵快速幂 [code]: #include <cstdlib> #i ...

  2. python pip和easy_install使用方式(转载)

    easy_install 跟 pip 都是Python 的套件管理程式,有了它们,在使用 Python 开发程式的时候会带来不少方便. easy_install 和pip 有什麼不一样?据 pip 官 ...

  3. [algorithm]求最长公共子序列问题

    最直白方法:时间复杂度是O(n3), 空间复杂度是常数 reference:http://blog.csdn.net/monkeyandy/article/details/7957263 /** ** ...

  4. ORA-00257: 归档程序错误。在释放之前仅限于内部连接

    今天发现oracle数据库连不上,报错:ORA-00257: 归档程序错误.在释放之前仅限于内部连接 马上联想到可能是空间满了,一看磁盘目录,果然. 解决方法如下: 1:查看归档日志目录. 登陆账号后 ...

  5. Codeforces Round #355 (Div. 2) D. Vanya and Treasure dp+分块

    题目链接: http://codeforces.com/contest/677/problem/D 题意: 让你求最短的从start->...->1->...->2->. ...

  6. Vim 配置 winmanager

    问题描述: winmanager是vim中插件,可以方便的查看当前文件夹中文件,可以切换vim打开文件,非常方便 现在说明安装和使用winmanager 问题解决: (1)winmanager源文件 ...

  7. 《云大课程助手》Android刷课工具来袭

    云大课程助手(Android)谨以此app纪念我这四年的大学生活.希望大家用的愉快. 下载地址:http://zhushou.360.cn/detail/index/soft_id/922292注:已 ...

  8. PAT-乙级-1052. 卖个萌 (20)

    1052. 卖个萌 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 萌萌哒表情符号通常由“手”.“眼”. ...

  9. crud springmvc

    实体类:Student.java package demo.entity; public class Student { private int id; private String name; pr ...

  10. jquery mouseout事件冒泡解决方法

    在界面中最多使用的是div,如果一个div中包含多个div或img对象时, 针对mouseout鼠标移出事件时,并没有移出div层,但是当移动到div中的img时,也触发了该mouseout事件,这就 ...