pku 1703 Find them, Catch them(并查集)
来源:互联网 发布:java初级工程师面试题 编辑:程序博客网 时间:2024/05/23 11:52
Find them, Catch them
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 24906 Accepted: 7486
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
题意:D 表示2个人属于不同帮派,A表示问你这2个人同不同帮派
题解:用并查集做,不过要用有偏移量的并查集....
#include<stdio.h>int v[100005],ral[100005];int f(int x){ int temp=v[x]; if(x==v[x]) return x; v[x]=f(v[x]); ral[x]=(ral[x]+ral[temp])%2; return v[x];}int main(){ int T,a,b,n,m,x,y,i; char ch; //freopen("t.txt","r",stdin); scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); for(i=0;i<=n;i++) v[i]=i,ral[i]=0; for(i=0; i<m; ++i) { getchar(); scanf("%c%d%d",&ch,&a,&b); x=f(a),y=f(b); if(ch=='D') { if(x!=y) { v[x]=y; ral[x]=(ral[b]-ral[a]+3)&1; } } else { if(x!=y) puts("Not sure yet."); else { if(ral[a]==ral[b]) puts("In the same gang."); else puts("In different gangs."); } } } } return 0;}
- pku 1703 Find them, Catch them(并查集)
- pku 1703 Find them, Catch them(并查集)
- poj 1703 Find them, Catch them(并查集)
- poj 1703 Find them, Catch them (并查集)
- poj 1703 -- Find them, Catch them(并查集)
- poj 1703 Find them, Catch them(并查集)
- poj 1703 Find them, Catch them (并查集)
- POJ 1703 Find them, Catch them (并查集)
- Find them, Catch them.(POJ-1703)(并查集)
- POJ - 1703 - Find them, Catch them (并查集)
- POJ 1703 Find them, Catch them(并查集)
- poj--1703 Find them, Catch them(并查集)
- POJ 1703 Find them, Catch them (并查集)
- poj 1703 Find them, Catch them(并查集)
- POJ 1703-Find them, Catch them(并查集)
- poj 1703 Find them, Catch them(并查集)
- Poj 1703 Find them, Catch them (并查集)
- Find them, Catch them 并查集
- iOS: NSKeyedArchiverDelegate Protocol的方法archiverDidFinish:
- myeclipse 注册码生成
- webwork 类型转换器的使用
- 概念模型
- Java-接口的多继承
- pku 1703 Find them, Catch them(并查集)
- 提高你的Java代码质量吧:用整数类型处理货币
- VS2010中“转到定义”提示“未能找到符号”的解决方法
- oracle部分特殊命令
- Linux OS
- 360与IE8的兼容问题
- Integer之进制转换
- URL、URI、URN difference
- iOS: NSKeyedArchiverDelegate Protocol的方法archiverWillFinish: