设为首页 加入收藏

TOP

poj 1703 Find them, Catch them(并查集)
2015-07-20 17:30:28 来源: 作者: 【 】 浏览:2
Tags:poj 1703 Find them Catch 查集

Language: Find them, Catch them
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 32057 Accepted: 9882

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

POJ Monthly--2004.07.18



题意:按照输入说,1组样例,5个人,5个操作,有两伙,A代表询问下面两个人是不是一伙的,D说明下面两个人不是一伙


思路: 并查集可以找到能不能确定这两个人有关系,刚开始把所有用人标记为0,在以后的操作中把一部分人标记为1(另一伙)



#include
  
   
#include
   
     #include
    
      #include
     
       #include
      
        #include
       
         #include
        
          #define L(x) (x<<1) #define R(x) (x<<1|1) #define MID(x,y) ((x+y)>>1) using namespace std; #define N 100005 int father[N],num[N]; int n,m; int cha(int x) { if(x!=father[x]) { int t=father[x]; father[x]=cha(father[x]); num[x]=(num[x]+num[t])%2; //递归,更新父亲节点,在更新这个节点 } return father[x]; } int main() { int t,i,j; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(i=0;i<=n;i++) { father[i]=i; num[i]=0; } char c[10]; int x,y; while(m--) { scanf("%s%d%d",c,&x,&y); if(c[0]=='D') { int xx=cha(x); int yy=cha(y); father[xx]=yy; num[xx]=(-num[x]+num[y]+1+2)%2; } else { int xx=cha(x); int yy=cha(y); if(xx==yy) //可以确定这两个点有关系 { if(num[x]==num[y]) printf("In the same gang.\n"); else printf("In different gangs.\n"); } else printf("Not sure yet.\n"); } } } return 0; } 
        
       
      
     
    
   
  



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇hdu 4777 Rabbit Kingdom(树状数.. 下一篇map+pair Bayan 2015 Contest War..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·每日一道面试题-多线 (2025-12-26 06:20:17)
·java项目中哪些地方 (2025-12-26 06:20:14)
·Java真的是要没落了 (2025-12-26 06:20:12)
·C++ Lambda表达式保 (2025-12-26 05:49:45)
·C++ Lambda表达式的 (2025-12-26 05:49:42)