Find them, Catch them (poj1703)
来源:互联网 发布:音乐基础知初级书教材 编辑:程序博客网 时间:2024/06/05 09:08
Find them, Catch them
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 43890 Accepted: 13527
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.
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
15 5A 1 2D 1 2A 1 2D 2 4A 1 4
Sample Output
Not sure yet.In different gangs.In the same gang.
Source
POJ Monthly--2004.07.18
#include<stdio.h>#define N 100010int rank[2*N+1];int p[2*N+1];int find(int x){return x==p[x]?x:find(p[x]);}void unite(int x,int y){int r1,r2;r1=find(x);r2=find(y);if(r1==r2)return ;if(rank[r1]<rank[r2]) p[r1]=r2;else{p[r2]=r1;if(rank[r1]==rank[r2])rank[r1]++;}}int main(void){int T,n,m,i,a,b;char ch;scanf("%d",&T);while(T--){scanf("%d%d",&n,&m);for(i=1;i<=n*2;i++){p[i]=i;rank[i]=0;}while(m--){getchar();scanf("%c%d%d",&ch,&a,&b);if(ch=='D'){unite(a,b+n);unite(b,a+n);}else{if(find(a)==find(b))printf("In the same gang.\n");elseif(find(a+n)==find(b))printf("In different gangs.\n");elseprintf("Not sure yet.\n");}}}}
0 0
- Find them, Catch them (poj1703)
- poj1703 - Find them, Catch them
- POJ1703 Find them, Catch them
- poj1703 Find them, Catch them
- poj1703 Find them, Catch them
- POJ1703 Find them,Catch them
- POJ1703--Find them, Catch them
- poj1703 find them,catch them
- Find them, Catch them poj1703
- POJ1703 Find them, Catch them
- poj1703 Find them, Catch them
- poj1703 Find them, Catch them
- POJ1703-Find them, Catch them
- POJ1703 Find them, Catch them
- poj1703 Find them, Catch them
- poj1703 Find them, Catch them
- poj1703 Find them, Catch them(关系并查集)
- poj1703 Find them, Catch them(并查集)
- PAT甲级练习1065. A+B and C (64bit) (20)
- C++中引用&、const的用法
- hdu1880魔咒词典 字符串哈希
- Activity onDestroy() 的一个小问题
- HRBUST
- Find them, Catch them (poj1703)
- JavaScript变量
- 《牛客网剑指offer50题》找出数组中任意一个重复的数字
- 无线数字视频传输系统(图传)的方案比较
- 指针
- 【Python正则表达式】 批量去除视频名称中的网址
- C++---笔记1
- Linux日常命令记录
- 内存泄露检测方法介绍