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. OpenGL在Ubuntu 14.04 中的设置与编程

    在sudo apt-get install XXX,别的教程讲的很详细了. 编写好程序需要在shell中链接 g++ teapot.c -o teapot -lglut -lGL -lGLU 此处要注 ...

  2. ModernUI教程:创建自定义主题

            Modern UI WPF包括两个内置主题(dark与light).在1.0.3版本,您可以构建自定义的主题.Modern UI应用程序通常有在全局资源字典App.xaml中有如下定义 ...

  3. go mysql 初窥。查询

    先来几句草泥马草泥马... 首先要安装Go-SQL-Driver/MySql,下载安装前要保证你的GOPATH对应的是你的项目目录 export GOPATH=/var/www/gogogo/test ...

  4. C#字符串操作 取文本左边 取文本右边 取文本中间 取文本中间到List集合 指定文本倒序

    /// <summary> /// 取文本左边内容 /// </summary> /// <param name="str">文本</pa ...

  5. WinNTSetup v3.8.7 正式版绿色增强版

    最强系统安装利器:WinNTSetup 现已更新至 v3.8.7 正式版!这次更新修复调整了诸多问题,新版非常好用接近完美!WinNTSetup 现在已经自带BCDBoot 选项,并且完全支持Wind ...

  6. C#文本写入文件,追加写入文件

    写入文件和这个对象 StreamWriter using (StreamWriter fs = new StreamWriter(path, true)) { fs.WriteLine(strLog) ...

  7. Bioinformatics Glossary

    原文:http://homepages.ulb.ac.be/~dgonze/TEACHING/bioinfo_glossary.html Affine gap costs: A scoring sys ...

  8. Thinking in java学习笔记之set

    Random rand = new Random(47); Set<Integer> set = new HashSet<Integer>(); for(int i=0;i&l ...

  9. Vue.JS 对比其他框架

    Angular 选择 Vue 而不选择 Angular,有下面几个原因,当然不是对每个人都适合: 在 API 与设计两方面上 Vue.js 都比 Angular 简单得多,因此你可以快速地掌握它的全部 ...

  10. jQuery插件之Cookie插件使用方法~

    一.介绍 1-1.jQuery.Cookie.js插件是一个轻量级的Cookie管理插件.下载地址:jQuery-cookie.js  有需要的朋友,右键另存为即可! 二.使用方法 2-1.引入jQu ...