Question:
Find them, Catch them
Time Limit: 1000 MS Memory Limit: 10000 K
Total Submissions: 22289 Accepted: 6648
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. 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 shocould 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.
Source
POJ Monthly -- 2004.07.18
Analysis and Summary:
I have done some ownership and query sets, and then I made the so-called "type and query set". It seems like I had an epiphany.
In essence, the difference between a type-based query set and a permission-based query set is not big. The key difference is that a type-based query set only requires a permission-based query set and a % remainder operation, then the remainder indicates the type of the remainder.
There are only two types, namely, 0 and 1. For two different types, the weights are different by 1, therefore, add 1 according to the method with permission and query set, and then take the remaining 2.
Code:
[Cpp]
# Include <cstdio>
Const int n= 100005;
Int n, m, f [N], rank [N];
Inline void init (){
For (int I = 1; I <= n; ++ I)
F [I] = I, rank [I] = 0;
}
Int find (int x ){
If (x = f [x]) return f [x];
Int fa = f [x];
F [x] = find (f [x]);
Rank [x] = (rank [x] + rank [fa]) & 1;
Return f [x];
}
Inline bool Union (int x, int y ){
Int a = find (x), B = find (y );
If (a = B) return false;
F [B] =;
Rank [B] = (rank [x]-rank [y] + 1) & 1;
}
Int main (){
Int T, a, B, fa, fb;
Char ch;
Scanf ("% d", & T );
While (T --){
Scanf ("% d % * c", & n, & m );
Init ();
For (int I = 0; I <m; ++ I ){
Scanf ("% c % d % * c", & ch, & a, & B );
If (ch = 'D '){
Union (a, B );
}
Else {
Fa = find (a), fb = find (B );
If (fa = fb ){
If (rank [a] = rank [B]) puts ("In the same gang .");
Else puts ("In different gangs .");
}
Else
Puts ("Not sure yet .");
}
}
}
Return 0;
}