Find them, Catch them
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 42482 | Accepted: 13075 |
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.
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.
Source
先说一下题意:有两个帮派,有N个人属于两个帮派的其中一个,各处一个M值,代表M组测试数据,每一组测试数据,如果输入字符为'D',代表两个人不属于一个帮派,如果输入字符为'A',判断给出两个人是否属于一个帮派,并且输出对应结果。
并查集:分为三步:Init(), find(), merge()
本题是一道种类并查集:这类并查集是给出不在同一个集合的若干点,让你判断;一般是加入到同一棵树,然后通过记录和判断与父节点的关系来求解。
用到了和其父节点的关系,用一个数组status[]表示,status[x]表示x这个节点和其目前父节点的关系(关系有两种:1和0;如果和父节点不同帮派就为1,如果相同就为0);
所以在find()他的父节点的时候要及时更新status[]的值,记住:f[]改变,则status[]改变。