POJ 1703 Find them, Catch them(并查集拓展)
2013-09-02 12:02
369 查看
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."
题目大意:有n个人,D a b表示a b位于不同的集合,A a b则代表问a b是否属于同一个集合(在线提问)。
思路:并查集拓展的简单应用,不多讲。就用rela[x]表示x与当前父节点(不一点是根节点)是否相同。
代码(313MS):
View Code
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."
题目大意:有n个人,D a b表示a b位于不同的集合,A a b则代表问a b是否属于同一个集合(在线提问)。
思路:并查集拓展的简单应用,不多讲。就用rela[x]表示x与当前父节点(不一点是根节点)是否相同。
代码(313MS):
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int MAXN = 100010; int fa[MAXN]; bool rela[MAXN]; int get_set(int x) { if(x == fa[x]) return x; else { int ret = get_set(fa[x]); rela[x] ^= rela[fa[x]]; return fa[x] = ret; } } void unionSet(int x, int y) { int fx = get_set(x); int fy = get_set(y); fa[fy] = fx; rela[fy] = 1 ^ (rela[x] ^ rela[y]); } void query(int x, int y) { int fx = get_set(x); int fy = get_set(y); if(fx != fy) puts("Not sure yet."); else if(rela[x] == rela[y]) puts("In the same gang."); else puts("In different gangs."); } int main() { int T, n, m, x, y; char c; scanf("%d", &T); while(T--) { scanf("%d%d", &n, &m); for(int i = 1; i <= n; ++i) fa[i] = i, rela[i] = 0; while(m--) { scanf(" %c%d%d", &c, &x, &y); if(c == 'D') unionSet(x, y); else query(x, y); } } }
View Code
相关文章推荐
- poj 1703 Find them, Catch them 【并查集拓展】
- POJ 1703 Find them, Catch them(并查集的拓展)
- POJ 1703 Find them, Catch them (并查集)
- POJ 1703 Find them, Catch them 种类并查集(入门)
- POJ 1703 Find them, Catch them (并查集)
- poj 1703 Find them, Catch them (并查集,思维)
- poj 1703 Find them, Catch them(带权并查集)
- 并查集 POJ-1703 Find them, Catch them
- POJ 1703 Find them, Catch them 二分图 并查集
- poj 1703 Find them, Catch them 并查集
- 【POJ】1703 Find them, Catch them 并查集
- POJ 1703 Find them, Catch them (并查集)
- POJ 1703 Find them, Catch them(并查集的扩展应用)
- poj 1703 Find them, Catch them(带偏移量并查集)
- poj 1703 -- Find them, Catch them(并查集)
- POJ:1703 Find them, Catch them(种类并查集(影子并查集))
- POJ 1703 Find them, Catch them【带权并查集】
- poj 1703 Find them, Catch them(并查集)
- Find them, Catch them.(POJ-1703)(并查集)
- poj 1703 Find them, Catch them 并查集