poj 1703 Find them, Catch them(并查集)
2014-10-10 18:44
627 查看
Language: Default Find them, Catch them
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. Source POJ Monthly--2004.07.18 |
思路: 并查集可以找到能不能确定这两个人有关系,刚开始把所有用人标记为0,在以后的操作中把一部分人标记为1(另一伙)
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<queue> #include<vector> #define L(x) (x<<1) #define R(x) (x<<1|1) #define MID(x,y) ((x+y)>>1) using namespace std; #define N 100005 int father ,num ; int n,m; int cha(int x) { if(x!=father[x]) { int t=father[x]; father[x]=cha(father[x]); num[x]=(num[x]+num[t])%2; //递归,更新父亲节点,在更新这个节点 } return father[x]; } int main() { int t,i,j; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(i=0;i<=n;i++) { father[i]=i; num[i]=0; } char c[10]; int x,y; while(m--) { scanf("%s%d%d",c,&x,&y); if(c[0]=='D') { int xx=cha(x); int yy=cha(y); father[xx]=yy; num[xx]=(-num[x]+num[y]+1+2)%2; } else { int xx=cha(x); int yy=cha(y); if(xx==yy) //可以确定这两个点有关系 { if(num[x]==num[y]) printf("In the same gang.\n"); else printf("In different gangs.\n"); } else printf("Not sure yet.\n"); } } } return 0; }
相关文章推荐
- 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 2236 Wireless Network ||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(种类并查集)