Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u

SubmitStatus

Description

The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Dragon and Gang Snake. However, the police first needs to identify which gang a criminal belongs to.
The present question is, given two criminals; do they belong to a same clan? You must give your judgment based on incomplete information. (Since the gangsters are always acting secretly.)



Assume N (N <= 10^5) criminals are currently in Tadu City, numbered from 1 to N. And of course, at least one of them belongs to Gang Dragon, and the same for Gang Snake. You will be given M (M <= 10^5) messages in sequence, which are in the following two kinds:



1. D [a] [b]

where [a] and [b] are the numbers of two criminals, and they belong to different gangs.



2. A [a] [b]

where [a] and [b] are the numbers of two criminals. This requires you to decide whether a and b belong to a same gang.

Input

The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. Each test case begins with a line with two integers N and M, followed by M lines each containing one message
as described above.

Output

For each message "A [a] [b]" in each case, your program should give the judgment based on the information got before. The answers might be one of "In the same gang.", "In different gangs." and "Not sure yet."

Sample Input

1
5 5
A 1 2
D 1 2
A 1 2
D 2 4
A 1 4

Sample Output

Not sure yet.
In different gangs.
In the same gang.

Source

POJ Monthly--2004.07.18



输出少了一个“.”wa了四次-.-||

题意:有两个帮派,现在给了n个人m个关系,D:关系表示两个人不在一个帮派,A:查询两个人是否在一个帮派,不确定的话输出“Not sure yet.“,同一个输出”In the same gang.“,不在一个输出”In different gangs.“。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int pre[100010],rank[100010];
int find(int x)
{
if(pre[x]==x)
return x;
else
{
int temp=pre[x];
pre[x]=find(pre[x]);
rank[x]=(rank[x]+rank[temp])%2;//将x变为和temp一样的帮派
return pre[x];
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(rank,0,sizeof(rank));//rank表示层数,两层表示不一样的帮派
char op[2];
int x,y;
int m,n;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
pre[i]=i;
for(int i=0;i<m;i++)
{
scanf("%s%d%d",op,&x,&y);
int fx=find(x);
int fy=find(y);
if(op[0]=='D')
{
pre[fy]=fx;
rank[fy]=(rank[x]-rank[y]+1)%2;
continue;
}
else
{
if(fx!=fy)//根节点不一样应该就是尚未分配,所以不确定
printf("Not sure yet.\n");
else if(rank[x]!=rank[y])//层数不一样就不在一个帮派
printf("In different gangs.\n");
else if(rank[x]==rank[y])//层数一样并且根节点相同
printf("In the same gang.\n");
}
}
}
return 0;
}

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. Leetcode--easy系列4

    #58 Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space char ...

  2. Java 递归、尾递归、非递归 处理阶乘问题

    n!=n*(n-1)! import java.io.BufferedReader; import java.io.InputStreamReader; /** * n的阶乘,即n! (n*(n-1) ...

  3. HDU5638 / BestCoder Round #74 (div.1) 1003 Toposort 线段树+拓扑排序

    Toposort   问题描述 给出nn个点mm条边的有向无环图. 要求删掉恰好kk条边使得字典序最小的拓扑序列尽可能小. 输入描述 输入包含多组数据. 第一行有一个整数TT, 表示测试数据组数. 对 ...

  4. poj_1952最大下降子序列,统计个数

    其实不算难的一道题,但憋了我好久,嗯,很爽. #include<iostream> #include<cstdio> #include<string.h> #inc ...

  5. bzoj2464: 中山市选[2009]小明的游戏(最短路)

    2464: 中山市选[2009]小明的游戏 题目:传送门 题解: 最短路的裸题... 代码: #include<cstdio> #include<cstring> #inclu ...

  6. DB-MySQL:目录

    ylbtech-DB-MySQL:目录 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://ylbt ...

  7. 如何在Windows下安装Linux子系统(Ubuntu,openSUSU,SUSU Linux Server)

    注意:只有win10才能安装,安装的linux没有图形界面. 1.首先在win10设置 --> 更新与安装 --> 针对开发人员 ,选择开发人员模式. 2.win10 Cortana -- ...

  8. C#生成高清缩略图 (装在自OPEN经验库)

    来源 http://www.open-open.com/lib/view/open1389943861320.html 代码如下实现图片的高清缩略图 /// <summary> /// 为 ...

  9. php报错权限设置

    <?php //禁用错误报告 error_reporting(0); //报告运行时错误 error_reporting(E_ERROR | E_WARNING | E_PARSE); //报告 ...

  10. nodejs 封装mysql连接池

    写在前面的 在nodejs后台代码中,我们总是会和数据库打交道 然而,每次都要写数据库的配置以及连接和断开,不胜其烦 我就封装了一个连接池模块,不足之处还请多多批评 上代码 一下是写在mysqls.j ...