Wednesday, September 1, 2010


Problem Links:



Find them, Catch them
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 15702
Accepted: 4620
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.
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.
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
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.
POJ Monthly--2004.07.18


Disjoint set structure, find-union algorithms.
1st, the meaning of the rank, that's the key part of this problem.
2nd, take care of the scanf of cin >> c >> a >> b; This took me 2 or 3 WAs.

Source Code:

//Thu Jun  3 13:48:07 EDT 2010
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define MMAX 100005
using namespace std;

class Gang
    int parent; //the number of element in this union;
    int rank;   //0 for the same as set root,1 for different from it;

vector<Gang> v(MMAX);

void MakeSet(int N)
    for (int i = 1; i <= N; i++)
        v[i].parent = i;
        v[i].rank = 0;

int Find(int x)
    if (x == v[x].parent)
        return x;
    int temp = v[x].parent;
    v[x].parent = Find(temp);
//  v[x].rank = v[x].rank == v[temp].rank ? 0 : 1;
    v[x].rank = v[temp].rank == 0 ? v[x].rank : (1 - v[x].rank);
    return v[x].parent;

void Union(int t1, int t2, int x, int y, int d)
    v[t2].parent = t1;
    v[t2].rank = v[x].rank == v[y].rank ? d : (1 - d);

int main(int argc, const char* argv[])
//  freopen("", "r", stdin);
//  freopen("output.out", "w", stdout);
    int T;
    cin >> T;
//  scanf("%d", &T);
    while (T--)
        int N, M;
        cin >> N >> M;
//      scanf("%d %d", &N, &M);
        char c;
        int a, b;
        for (int i = 0; i < M; i++)
//          cin >> c >> a >> b;
            scanf("\n%c %d %d", &c, &a, &b);
//          cout << c << a << b << endl;
            int t1 = Find(a);
            int t2 = Find(b);
            if (c == 'A')
                if (t1 == t2)
                    if (v[a].rank == v[b].rank)
                        cout << "In the same gang." << endl;
                        cout << "In different gangs." << endl;
                    cout << "Not sure yet." << endl;
            else if (c == 'D')
                Union(t1, t2, a, b, 1);
//  fclose(stdin);
//  fclose(stdout);
    return 0;

No comments :