poj_1703 Find them,Catch them
来源:互联网 发布:淘宝上什么木制品好卖 编辑:程序博客网 时间:2024/06/06 05:26
Find them, Catch them
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 30512 Accepted: 9388
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
AC未整理版:
#include<cstdio>#include<cstdlib>#include<cstring>using namespace std;#define range 100010int pa[2*range],layer[2*range];int find_pa(int k){ if(pa[k]==0) return k; pa[k]=find_pa(pa[k]); return find_pa(pa[k]);}void make_union(int a,int b){ int paa=find_pa(a); int pab=find_pa(b);// printf("!!\n"); if(paa==pab) return; if(layer[paa]<=layer[pab]) { pa[paa]=pab; if(layer[paa]==layer[pab]) layer[pab]++; } else pa[pab]=paa;// printf("uniona=%d unionb=%d\n",find_pa(a),find_pa(b));}int main(){// freopen("in.txt","r",stdin); int t,n,m,i,j; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); memset(pa,0,sizeof(pa)); memset(layer,0,sizeof(layer));// printf("m=%d\n",m); for(i=0;i<m;i++) { int a,b; char com,c[3];// 底下這樣讀入會錯,害我WA了好久!(雖然不知道為什麼!!// scanf("%c",&com);// char space=getchar();// space=getchar();// com=getchar();// scanf("%d%d",&a,&b); scanf("%s%d%d",c,&a,&b);// printf("com=%c a=%d b=%d\n",com,a,b); if(c[0]=='D') { int paa=find_pa(a); int inpab=find_pa(b+n); make_union(a,b+n); make_union(a+n,b);// printf("a=%d b=%d paa=%d inpaa=%d pab=%d inpab=%d\n",a,b,paa,find_pa(a+n),find_pa(b),inpab); } if(c[0]=='A') {// if(n==2 && a!=b) {printf("In different gangs.\n"); continue;} int paa=find_pa(a); int inpaa=find_pa(a+n); int pab=find_pa(b); int inpab=find_pa(b+n);// printf("paa=%d inpaa=%d pab=%d inpab=%d\n",paa,inpaa,pab,inpab); if(paa==pab ) printf("In the same gang.\n"); else if(inpaa==pab || inpab==paa) printf("In different gangs.\n"); else printf("Not sure yet.\n"); } } } return 0;}
0 0
- poj_1703 Find them,Catch them
- Find them, Catch them
- Find them, Catch them
- Find them, Catch them
- Find them, Catch them
- Find them, Catch them
- Find them, Catch them
- Find them, Catch them
- Find them, Catch them
- Find them, Catch them
- Find them, Catch them
- Find them, Catch them
- Find them, Catch them
- pku1703 Find them, Catch them
- PKU1703 find them catch them
- Find them,catch them(P1703)
- poj1703 - Find them, Catch them
- POJ1703 Find them, Catch them
- [LeetCode] Permutations II
- POJ 1742 Coins
- LRU Cache
- uva10561(SG函数)
- 求其带权路径长度
- poj_1703 Find them,Catch them
- Python使用PyMedia播放mp3,wave等文件拍
- Linux下ppp拨号+3G模块(evdo 中兴MC8630模块)
- 基于移动平台的体感控制系统项目
- 数据结构_多项式_C语言源代码
- Cocos2d-x的SprideMonkey的JavaScript与C++的交互
- php文件头信息详解匝
- LDA gibbs
- 项目开发的一些信息,以后可能用到,先记下来