题目: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. 在ASP.NET MVC应用程序中实现Server.Transfer()类似的功能

    在ASP.NET MVC应用程序中,如果使用Server.Transfer()方法希望将请求转发到其它路径或者Http处理程序进行处理,都会引发“为xxx执行子请求时出错”的HttpException ...

  2. C# WinForm开发系列 - ZedGraph

    ZedGraph是用于创建任意数据的二维线型.条型.饼型图表的一个类库,也可以作为Windows窗体用户控件和Asp.Net网页控件.这个类库具有高度的适应性,几乎所有式样的图表都能够被创建.这个类库 ...

  3. 【锋利的JQuery-学习笔记】输入框提示语-隐藏/显示

    html <div class="search"> <input type="text" id="inputSearch" ...

  4. HDU4758 Walk Through Squares AC自动机&&dp

    这道题当时做的时候觉得是数论题,包含两个01串什么的,但是算重复的时候又很蛋疼,赛后听说是字符串,然后就觉得很有可能.昨天队友问到这一题,在学了AC自动机之后就觉得简单了许多.那个时候不懂AC自动机, ...

  5. POJ2104 K-th Number Range Tree

    又是区间第k大,这次选择这道题是为以后写线段树套平衡树铺路的.Range Tree可以理解成线段树套vector吧,相当于每个结点多存了对应区间的一个排好序的序列.画一下就会知道空间的消耗是nlogn ...

  6. grunt安装失败处理

    1.官网 Grunt官网 http://gruntjs.com 2.前言 前段时间一不小心升级了win10(万恶的360),各种不适应各种问题各种软件bug,最终决定回退到win7,然后悲催的发现系统 ...

  7. android Notification定义与应用

    首先要明白一个概念: Intent 与 PendingIntent 的区别: Intent:是意图,即告诉系统我要干什么,然后做Intent应该做的事,而intent是消息的内容 PendingInt ...

  8. 【转载】Eclipse自动编译问题

    今天调试的时候发现问题:调试的时候竟然在我注释的里面走,当时那个郁闷啊,每次都要clean下才可以,晚上感觉不对劲,上网查了查,原来是bulid automatically这个我把勾去掉了,下面是原文 ...

  9. 黑马程序员-- .net基础加强8之委托,事件,程序集

    ---------------------- ASP.Net+Android+IOS开发..Net培训.期待与您交流! ---------------------- 一.委托 ============ ...

  10. Spring多资源文件properties的配置

    Spring简化了加载资源文件的配置,可以通过<context:property-placeholder去加载,这个元素的写法如下: <context:property-placehold ...