题目:Find them,Catch them

  刚开始以为是最基本的并查集,无限超时。

  这个特殊之处,就是可能有多个集合。

  比如输入D 1 2  D 3 4 D 5 6...这就至少有3个集合了。并且任意2个集合之间成员的敌我关系不明。

  这里每个集合里面的成员关系要记录,他们在一个集合里,只是因为他们关系明确,敌人或者朋友。

  千万不要简单的认为成朋友在一个集合,敌人在另外一个集合,分成2个集合。因为题目说只有2个匪帮,很容易进入这个误区。

  我们只要记录一个成员和自己父亲的敌我关系和建树就可以了。

  

代码:

#include<iostream>
#include<cstdio>
using namespace std;
const int MM = ;
int father[MM];
int relation[MM]; int find(int x)
{
if(x == father[x]) return x;
int r = find(father[x]);
relation[x] ^= relation[father[x]];
return father[x] = r;
} void join(int x, int y)
{
int fx = find(x);
int fy = find(y); father[fx] = fy;
if(relation[y] == )
relation[fx] = relation[x];
else
relation[fx] = - relation[x];
} void solve()
{
int T,N,M,i,a,b,ta,tb;
char str;
scanf("%d", &T);
while(T--)
{
scanf("%d %d",&N, &M); for(i = ; i <= N; ++i)
{
father[i] = i;
relation[i] = ;
} for(i = ; i < M; ++i)
{
getchar();
scanf("%c %d %d", &str, &a, &b);
if(str == 'D')
join(a, b);
else
{
ta = find(a);
tb = find(b);
if(ta == tb)
{
if(relation[a] == relation[b])
printf("In the same gang.\n");
else
printf("In different gangs.\n");
}
else
printf("Not sure yet.\n");
}
}
}
} int main()
{
solve();
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 并查集的应用

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

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

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

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

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

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

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

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

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

  8. 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条臭 ...

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

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

随机推荐

  1. VPN DNS leak 问题的解决

    前一段时间遇到一个问题.customer说发现连接VPN后在PPP端发现security leak,整个转了好大一个圈子才把问题解决了.之所以费这么大周折,倒不是因为很难解决,只是费了很大劲儿才定位了 ...

  2. cisco vpn client for win10 x64 setup package

    win10 x64安装cisco vpn client报错,解决方法如下: 1.卸载以前安装的所有cisco vpn client,并重启电脑. 2.运行winfix.exe 3.安装Global V ...

  3. SQL列数据转换为字符串

    行列转换,将列数据转换为字符串输出 ) SET @center_JZHW = ( SELECT DISTINCT STUFF( ( SELECT ',' + ce_code FROM ap_cente ...

  4. Visual Studio Developer Command Prompt删除localdb的方法

    PM> sqllocaldb.exe stop v11. LocalDB instance "v11.0" stopped. PM> sqllocaldb.exe de ...

  5. input之placeholder与行高的问题。

    我们实现一个输入框的视觉的时候为了保持其各种各样的兼容性: 1.鼠标要跟文字一样高度. 2.文字要居中对齐. 3.还要有placeholder 第一个目标,当实现一个高度为40像素的高度输入框时,为了 ...

  6. 西南科技大学第十一届ACM程序设计大赛发言稿

    西南科技大学第十一届ACM程序设计大赛发言稿 各位老师.志愿者及参赛选手: 大家好,我是来自计科学院卓软1301的哈特13,很荣幸今天能站在这里代表参赛选手发言. 回想起来,我参加ACM比赛已经快两年 ...

  7. Java [leetcode 27]Remove Element

    题目描述: Given an array and a value, remove all instances of that value in place and return the new len ...

  8. 为枚举类型添加说明 zt

    enum Orientation { [DescriptionAttribute("东")] East, [DescriptionAttribute("南")] ...

  9. Codeforces 629C Famil Door and Brackets DP

    题意:给你一个由括号组成的字符串,长度为m,现在希望获得一个长度为n(全由括号组成)的字符串,0<=n-m<=2000 这个长度为n的字符串要求有两个性质:1:就是任意前缀,左括号数量大于 ...

  10. 使用appium做安卓手机web自动化测试 真机demo

    一:appium相关环境搭建过程略. 二:连接真机: 1.手机(andriod 4.2.2)连接电脑,打开USB调试模式. 2.运行cmd 输入 adb devices -l 查看UDID,如图: 3 ...