zoukankan      html  css  js  c++  java
  • POJ-1703 Find them, Catch them(并查集&数组记录状态)

    题目:

    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.
    

    题意:

    给你N个英雄,这些英雄术语两个阵营。D a b表示知道a和b在对立阵营,A a b表示询问a和b是否属于同一阵营或者不确定关系。

    分析:

    敌人的敌人就是朋友,用一个数组记录关系:vis[a] = b表示a的敌人是b,然后并查集把对立的人和对立的人放到一个集合里,具体看代码实现。

    #include<cstdio>
    using namespace std;
    const int maxn = 1e5+5;
    int f[maxn],vis[maxn];
    int t,n,m,a,b;
    int get(int x){
    	return f[x] == x?x : f[x] = get(f[x]);
    }
    void merge(int x,int y){
    	f[get(x)] = get(y);
    }
    void init(){
    	for (int i = 1; i <= n; i++){
    		f[i] = i;
    		vis[i] = 0;	
    	}
    }
    int main(){
    	char ch;
    	scanf("%d",&t);
    	while (t--){
    		scanf("%d%d",&n,&m);
    		init();
    		for (int i = 1; i <= m; i++){
    			getchar();
    			scanf("%c",&ch);
    			if (ch == 'D'){
    				scanf("%d%d",&a,&b);
    				if (vis[a]) merge(vis[a],b);
    				if (vis[b]) merge(vis[b],a);
    				vis[a] = b;vis[b] = a;
    			}
    			else{
    				scanf("%d%d",&a,&b);
    				if (get(a) == get(b)) printf("In the same gang.
    ");
    				else if (get(vis[a]) == get(b)) printf("In different gangs.
    ");
    				else printf("Not sure yet.
    ");
    			}	
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    SourceForge上发布dedup util 收藏
    IBM快速整合Diligent 触动了谁的神经?
    Data Domain揭开其神秘面纱 加速中国业务发展
    Java NIO原理和使用
    用PDB库调试Python程序
    windows下配置eclipse + Python + PyDev + tornado,并使用pylint进行静态检测
    函数fork与vfork的区别与联系详解
    关于C++中友元类和友元函数的示例讲解
    const成员函数
    pthread_cond_wait()深入解析
  • 原文地址:https://www.cnblogs.com/hznudreamer/p/12375742.html
Copyright © 2011-2022 走看看