题目: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. AIDL与stub

     Stub翻译成中文是存根的意思,注意Stub对象是在被调用端进程,也就是服务端进程,至此,服务端aidl服务端得编码完成了.  stub是为了方便client,service交互而生成出来的代码.A ...

  2. 【HDOJ】4322 Candy

    状态DP显然可以解,发现T了,不知道优化后能不能过.然后发现费用流可以解.trick是对need拆解成need/K, need%K两种情况讨论. /* 4312 */ #include <ios ...

  3. 重启php-fpm

    查看pid位置 cat /etc/php-fpm.conf pid = /usr/local/php/var/run/php-fpm.pid #测试php-fpm配置/usr/local/php/sb ...

  4. 使用JRockit进行性能优化一:环境搭建

    1. jrockit简介   jrockit前身是BA jrockit,后被oracle收购,并免费发布,但并不开源.   jrockit可以看做是兼容标准的JDK基础上的JVM,同原有的JVM相比, ...

  5. 在“BindingNavigator”删除数据前弹出确认框的实现

    1)先设置DeleteItem为空,不让它调用自动生成的删除代码. 2)然后自己写代码实现,如下: private void bindingNavigatorDeleteItem_Click(obje ...

  6. A*算法完全理解

    注:原文出自Patrick Lester,一稿翻译为Panic.很久以前的老文章了,但我觉得真的非常的经典,想把它完善一下让以后的人能够更好的体会原作者和原翻译的精髓吧.我在此基础上修改了部分译文,更 ...

  7. BZOJ3451: Tyvj1953 Normal

    题解: 好神的一道题.蒟蒻只能膜拜题解. 考虑a对b的贡献,如果a是a-b路径上第一个删除的点,那么给b贡献1. 所以转化之后就是求sigma(1/dist(i,j)),orz!!! 如果不是分母的话 ...

  8. oracle 字段上下两条记录的相减

    SELECT T.ID  ,BALANCE,nvl(lag (BALANCE,1) over (order by T.ID ) ,0) FROM  AN T ORDER BY T.ID [转]orac ...

  9. ASDL + WN725N 配置无线AP

    1.  ASDL 正常拨号上网 2.  安装TP-LINK无线客户端应用程序 打开之后选择模拟AP 如下图设置----应用 3.  本地连接----属性----高级 如下图设置 4.  宽带连接--- ...

  10. Android_1_渐变背景色

    首先创建一个渐变背景色文件drawable-mdpi/bg_color.xml <?xml version="1.0" encoding="utf-8"? ...