题意:城市中有两个帮派,输入中有情报和询问。情报会告知哪两个人是对立帮派中的人。询问会问具体某两个人的关系。

思路:并查集的应用。首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个集合中,如果是,则表明两个人的关系已知。

本题还需要判断出两人是不是在同一帮派,这里用一个relation数组来维护每个人与根结点的关系。

0表示该人与根节点为同一个帮派。

1表示该人与根节点为对立帮派。

并查集有两个基本操作:find——查找根节点, merge——合并两个集合。

在合并两个集合时,其中一个集合的relation的值要发生变化。在这里只需要改变集合根节点的relation(改变的规则见代码中merge函数,relation改变的规则自己举几个例子就明白了)。

其余点的值在find函数压缩路径时就顺便完成了。relation的状态只有两种(0和1),在压缩路径时的变化规则就是点到根节点的relation=(与父亲结点的relation+父亲结点与根节点的relation)% 2。

 #include<stdio.h>
#define maxn 100010
int father[maxn];
bool relation[maxn];
int Find(int x)
{
if (father[x] != x)
{
int t = father[x];
father[x] = Find(father[x]);
relation[x] = (relation[t] + relation[x]) % ;
}
return father[x];
}
void Merge(int x,int y)
{
int a = Find(x);
int b = Find(y);
father[a] = b;
if (relation[y] == )
relation[a] = ^ relation[x];
else relation[a] = relation[x];
}
int main()
{
int t, n, m;
scanf("%d",&t);
while (t--)
{
scanf("%d%d",&n,&m);
for (int i = ; i <= n; i++)
{
father[i] = i;
relation[i] = ;
}
char op;
int a, b;
while (m--)
{
getchar();
scanf("%c",&op);
if (op == 'D')
{
scanf("%d%d",&a,&b);
Merge(a, b);
}
else
{
scanf("%d%d",&a,&b);
if (Find(a) == Find(b))
{
if (relation[a] == relation[b])
printf("In the same gang.\n");
else
printf("In different gangs.\n");
}
else
printf("Not sure yet.\n");
}
}
}
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(并查集高级应用)

    手动博客搬家:本文发表于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. 初识java,编写hello world语句

    JDK: Java Develpment Kit - java开发工具包 JRE: Java Runtime Environment - java运行环境 JVM: Java Virtual Mach ...

  2. 【Scramble String】cpp

    题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...

  3. 安装 Redis的Python客户端redis-py

    安装 redis-py 使用easy_install sudo easy_install redis 源码安装 git clone https://github.com/andymccurdy/red ...

  4. Atom-无懈可击的Markdown编辑器

    备战美赛期间,向岳神学习,搞了Atom玩协作开发,第一次没有自动补全的手撸了遗传算法.今天发现Atom还有写Markdown的妙用,遂拿来练手. 1. 安装Atom 下载安装Atom:https:// ...

  5. Lambda表达式的本质

    //.net 1.0写法 /*delegate bool MyMethod(string s); bool myMethod(string s) { return s.IndexOf("ab ...

  6. DNS(转载)

    最近帮朋友注册域名配置主机,碰到一些DNS上的一些概念,惭愧于有一些东西已经忘记是啥意思,于是决定重新学习一下DNS方面的基本概念. 常用概念: TTL: TTL为Time to live的缩写,网络 ...

  7. HTML中使用JavaScript

    1.script中使用src引入外部js.注意:不能是<script />,必须是<script></script> 2.使用link引入外部js 3.使用scri ...

  8. [POJ2942][LA3523]Knights of the Round Table

    [POJ2942][LA3523]Knights of the Round Table 试题描述 Being a knight is a very attractive career: searchi ...

  9. [NOI2014][bzoj3670] 动物园 [kmp+next数组应用]

    题面 传送门 思路 首先,这题最好的一个地方,在于它给出的关于$next$的讲解实在是妙极......甚至可以说我的kmp是过了这道题以后才脱胎换骨的 然后是正文: 如何求$num$数组? 这道题的输 ...

  10. Posix线程编程指南

    Posix线程编程指南 Posix线程编程指南... 1 一线程创建与取消... 2 线程创建... 2 1.线程与进程... 2 2. 创建线程... 2 3. 线程创建属性... 2 4. 创建的 ...