poj 1703 Find them, Catch them

                                                                                                Find them, Catch them
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 42031   Accepted: 12923

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.

题意:总共有N个人,两个帮派,每个人都属于其中的某一个帮派,输入字符D时会给信息,告诉你哪两个人不是一个帮派的,输入字符A时让你判断两个人之间的关系(是否一帮派等等)
思路:并查集,每个人具体是哪个帮派的人并不知道,所以创建2*N个元素,其中元素x和x+N分别代表一个人x属于帮派1和帮派2。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<algorithm>
using namespace std;
const int N_MAX = 100000*2+1;
int par[N_MAX];
int Rank[N_MAX];

void init(const int &n) {
    for (int i = 0;i < n;i++) {
        par[i] = i;
        Rank[i] = 0;
    }
}
int find(const int&x) {
    if (par[x] == x)
        return x;
    else {
        return par[x] = find(par[x]);
    }
}
void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)return;
    if (Rank[x] < Rank[y]) {
        par[x] = y;
    }
    else {
        par[y] = x;
        if (Rank[x] == Rank[y])Rank[x]++;
    }
}
bool same(const int &x,const int&y) {
    return find(x) == find(y);
}


int main() {
    int T,N,M;
    cin >> T;
    while (T--) {
        scanf("%d%d",&N,&M);
        init(2*N);//元素x和x+N分别代表元素x分别属于两个帮派
        char op;int x, y;
        getchar();//////!!!!!!!!!!!!!!!!!!千万别忘,不然会影响下面的输入
        while(M--) {
            scanf("%c%d%d",&op,&x, &y);
            //scanf("%c%d%d%*c", &op, &x, &y);
            x--;y--;
            if (op == 'A') {//需要判断两个元素是否属于一个帮派
                if (same(x, y))printf("In the same gang.\n");//x,y在同一个帮派
                else if (same(x, y + N))printf("In different gangs.\n");//x,y在不同帮派
                else printf("Not sure yet.\n");
            }
            else if (op == 'D') {//合并元素的操作,x,y不在同一帮派
                unite(x,y+N);
                unite(x + N, y);
            }
            cin.ignore();
        }

    }

    return 0;
}

 




posted on 2016-09-25 17:21  ZefengYao  阅读(294)  评论(0编辑  收藏  举报

导航