Find them, Catch them
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 42451   Accepted: 13059

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

 #include <iostream>
#include <cstdio> #define MAX_N 1000000+5 using namespace std; int par[MAX_N];//父节点
int depth[MAX_N];//深度 void init(int n){
for(int i=;i<=n;i++){
par[i]=i;
depth[i]=;
}
}
int find_father(int t){
if(t==par[t]){
return t;
}else{
return par[t]=find_father(par[t]);
//实现了路径压缩
}
}
void unite(int t1,int t2){
int f1=find_father(t1);
int f2=find_father(t2);
if(f1==f2){
return ;
}
if(depth[f1]<depth[f2]){
par[f1]=f2;
}else{
par[f2]=f1;
if(depth[f1]==depth[f2]){
depth[f1]++;
//记录深度
}
}
} bool same(int x,int y){
return find_father(x)==find_father(y);
} int main()
{
int t;
int n,m;
char c;
int a,b;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
init(n*);
while(m--){
getchar();
scanf("%c %d %d",&c,&a,&b);
if(c=='D'){
unite(a,b+n);
unite(a+n,b);
}else{
if(same(a,b+n)){
printf("In different gangs.\n");
continue;
}
if(same(a,b)){
printf("In the same gang.\n");
continue;
}else{
printf("Not sure yet.\n");
continue;
}
}
}
}
return ;
}

poj1703_Find them, Catch them_并查集的更多相关文章

  1. poj1703 Find them, Catch them 并查集

    poj(1703) Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26992   ...

  2. POJ 1703 Find them, catch them (并查集)

    题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2  D 3 4 D 5 6...这就至少有3个集合了.并且 ...

  3. poj1703--Find them, Catch them(并查集应用)

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

  4. POJ1703-Find them, Catch them 并查集构造

                                             Find them, Catch them 好久没有做并查集的题,竟然快把并查集忘完了. 题意:大致是有两个监狱,n个 ...

  5. POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

    POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...

  6. POJ 1703 Find them, Catch them 并查集的应用

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

  7. POJ-1703 Find them, Catch them(并查集&数组记录状态)

    题目: The police office in Tadu City decides to say ends to the chaos, as launch actions to root up th ...

  8. poj.1703.Find them, Catch them(并查集)

    Find them, Catch them Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...

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

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

随机推荐

  1. 集合覆盖 顶点覆盖: set cover和vertex cover

    这里将讲解一下npc问题中set cover和vertex cover分别是什么. set cover: 问题定义: 实例:现在有一个集合A,其中包含了m个元素(注意,集合是无序的,并且包含的元素也是 ...

  2. remove ---会报错discard不会报错

    s = {1,2,3,4,5,6,'sn','7'} s.remove('hellfjsdjfsjdfljsdl')#删除元素不纯在会报错 print(s) s.discard("sbbbb ...

  3. android 动画

    public void onClicked(View v_) { //wa.startAnimation(); // TextView tv = (TextView)findViewById(R.id ...

  4. C++ 顺序容器基础知识总结

    0.前言 本文简单地总结了STL的顺序容器的知识点.文中并不涉及具体的实现技巧,对于细节的东西也没有提及.一来不同的标准库有着不同的实现,二来关于具体实现<STL源码剖析>已经展示得全面细 ...

  5. BZOJ 2463: [中山市选2009]谁能赢呢?

    2463: [中山市选2009]谁能赢呢? Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1887  Solved: 1390[Submit][Sta ...

  6. WebUploader UEditor chrome 点击上传文件选择框会延迟几秒才会显示 反应很慢

    chrome52.0.2743.80以上, accept: { title: 'Images', extensions: 'jpg,jpeg,png', mimeTypes: 'image/*' } ...

  7. Fedora 23 忘记root密码

    方法:进入单用户模式改密码 进入grub后,按e进入编辑模式.找到以“linux"开头的那一行,在末尾加” rw init=/bin/bash".ctrl-x启动 (grub2用c ...

  8. 忍不住记录下小型的CMDB系统

  9. Instant Radiosity实现

    本来说等把课程作业做完再来弄这个,但是还是没有忍住,先做了,主要原因还是这个算法很容易实现.这个算法在1997年由Keller首次提出.虽然名字叫Instant Radiosity,但是它和Radio ...

  10. FineUI常见问题

    常见问题解答(必读) http://fineui.com/bbs/forum.php?mod=viewthread&tid=655