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 Function Description: disjoint-set 
 * @ development environment Environment: the Eclipse 
 * @ technology features Technique: look the question, know disjoint-set, but how to write it? Disjoint-set maximal a and b are the same as before the merger, it is different from a and b. 
                                  The key word is: the enemy's enemy is my friend. 
                                  We x everyone, assume that initially he had an enemy x + n, each encounter D ab, put a merger with the enemy b, b and a merger of the enemy. 
                                  When the query: If you find (a) == find (b ): friend 
                                  if find (a) == find (b + n) || find (b) == find (a + n): the enemy; 
                                  not: unconfirmed. 
 * @ version 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; 
        the while (countCase-- = 0! ) { 
            st.nextToken (); 
            n- = ( int ) st.nval; // n-personal 
            st.nextToken (); 
            m = ( int ) st.nval; // m message sequence 
            makeSet (2 * n-);
             for ( int I = 0; I <m; I ++ ) { 
                st.nextToken (); 
                Command = st.sval; // that instruction 
                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) { // if the root equal, proved entered (D x ~) and (D ~ Y) 
            return "Gang the in the Same." ; 
        } the else {
             IF (FX == Find ( n-+ y) == || Find FY (n-x +)) { // entered (x, y), or (y, x), x and y are so different gangs
                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];
    }

}

 

Reproduced in: https: //www.cnblogs.com/followYourDreams/archive/2012/08/16/2641671.html

Guess you like

Origin blog.csdn.net/weixin_34354173/article/details/93344302