首页 > 代码库 > poj 1703
poj 1703
Find them, Catch them
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 43855 | Accepted: 13509 |
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
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.
思路:弱化的食物链
代码:
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 #include<map> 7 #define pi acos(-1.0) 8 #define mj 9 #define inf 0x3f3f3f 10 typedef long long ll; 11 using namespace std; 12 const int N=1e5+5; 13 const int mod=1e9+7; 14 int f[N],dis[N]; 15 16 int find(int x){ 17 if(x==f[x]) 18 return x; 19 int t=f[x]; 20 f[x]=find(f[x]); 21 dis[x]=(dis[x]+dis[t])%2; 22 return f[x]; 23 } 24 25 void init(int n) 26 { 27 for(int i=0;i<=n;i++){ 28 f[i]=i; 29 dis[i]=0; 30 } 31 } 32 33 void unit(int x,int y) 34 { 35 int fx=find(x),fy=find(y); 36 if(fx!=fy) 37 { 38 f[fx]=fy; 39 dis[fx]=(1+dis[x]+dis[y])%2;//fx到根fy的距离 40 // if(x!=fx) dis[x]=(dis[fx]+dis[x])%2;操作已经在find里面完成,重复操作会出现错误! 41 } 42 } 43 int main() 44 { 45 int t; 46 scanf("%d",&t); 47 while(t--) 48 { 49 int n,m; 50 scanf("%d%d",&n,&m); 51 init(n); 52 int a,b; 53 getchar(); 54 for(int i=0;i<m;i++){ 55 char e; 56 scanf("%c %d %d",&e,&a,&b); 57 getchar(); 58 59 if(e==‘D‘){ 60 unit(a,b); 61 } 62 else 63 { 64 if(find(a)!=find(b)){ 65 puts("Not sure yet."); 66 } 67 else { 68 if(dis[a]^dis[b]==0){ 69 puts("In the same gang."); 70 } 71 else if(dis[a]^dis[b]!=0) puts("In different gangs."); 72 } 73 } 74 // for(int i=1;i<=n;i++){ 75 // printf("dis[%d]:%d\n",i,dis[i]); 76 // } 77 } 78 79 } 80 return 0; 81 82 }
poj 1703
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。