poj1703

Find them, Catch them
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 21916   Accepted: 6523

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.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;


/**
 *  @功能Function Description:    并查集
 *  @开发环境Environment:         eclipse
 *     @技术特点Technique:           看题,知道是并查集,但是怎么写呢?并查集要得是a与b相同才合并,现在是a与b不同。
                                  关键就是一句话:敌人的敌人是朋友。
                                  我们对每个人x,都假设初始时他有一个敌人x+n,每次碰到D a b,就把a与b的敌人合并,b与a的敌人合并。
                                  当查询时:若find(a)== find(b):朋友
                                  若find(a)==find(b+n)||find(b)==find(a+n):敌人;
                                  都不是:无法确定。
 *    @版本Version:                  
 *    @日期Date:                    20120815
 *    @备注Notes:                   
 */

public class PK1703_3_20120815 {
    
    public static int[] father = new int[200005];
    
    public static void main(String[] args) throws IOException {
        StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        st.nextToken();
        int countCase = (int)st.nval;
        int n, m;
        int x, y;
        String command;
        while(countCase-- !=0) {
            st.nextToken();
            n = (int)st.nval;//n个人
            st.nextToken();
            m = (int)st.nval;//m个消息序列
            makeSet(2*n);
            for(int i=0; i<m; i++) {
                st.nextToken();
                command = st.sval;//那种指令
                st.nextToken();
                x = (int)st.nval; 
                st.nextToken();
                y = (int)st.nval;
                if("D".equals(command)) {
                    union(x, y+n);
                    union(y, x+n);
                } else {
                    if(n==2) {
                        if(x==y) System.out.println("In the same gang.");
                        else System.out.println("In different gangs.");
                    } else {
                        System.out.println(judge(x, y, n));
                    }
                }
            }
        }
    }
    
    public static String judge(int x, int y, int n) {
        int fx = find(x);
        int fy = find(y);
        if(fx == fy) {//如果根相等的话,证明输入过(D x ~)和(D y ~) 
            return "In the same gang.";
        } else {
            if(fx==find(y+n) || fy==find(x+n)) {//输入过(x,y),或者(y,x),因此x和y不同帮派
                return "In different gangs.";
            } else {
                return "Not sure yet.";
            }
        }
    }
    
    public static void union(int x, int y) {
        x = find(x);
        y = find(y);
        if(x != y) {
            father[x] = y;
        }
    }
    
    public static void makeSet(int n) {
        for(int i=1; i<=n; i++) {
            father[i] = i;
        }
    }
    
    public static int find(int x) {
        if(x != father[x]) {
            father[x] = find(father[x]);
        }
        return father[x];
    }

}

 

转载于:https://www.cnblogs.com/followYourDreams/archive/2012/08/16/2641671.html

上一篇:1034 Head of a Gang (30 分)


下一篇:1034 Head of a Gang