POJ1703(2集合并查集)
Find them, Catch them
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 39402 | Accepted: 12101 |
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.
/* @ title : poj1703 @ method : union-find sets @ result : Accepted 1948K 813MS G++ 967B @ auther : baneHunter @ date : 2016/3/19 */ #include<cstdio> #include<cstring> using namespace std; const int MAXN=100005; int par[MAXN+MAXN],rnk[MAXN+MAXN]; int n,m; void prep() { for(int i=0;i<MAXN+MAXN;i++) { par[i]=i; rnk[i]=0; } } int fnd(int x) { if(x==par[x]) return x; return par[x]=fnd(par[x]); } void unite(int x,int y) { int a=fnd(x); int b=fnd(y); if(a==b) return ; if(rnk[a]<rnk[b]) { par[a]=b; } else { par[b]=a; if(rnk[a]==rnk[b]) rnk[a]++; } } bool same(int x,int y) { return fnd(x)==fnd(y); } int main() { int T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); prep(); while(m--) { scanf("%*c"); char op; int x,y; scanf("%c",&op); scanf("%d%d",&x,&y); if(op=='A') { if(same(x,y)) { printf("In the same gang.\n"); } else if(same(x,y+n)) { printf("In different gangs.\n"); } else printf("Not sure yet.\n"); } else { unite(x+n,y); unite(x,y+n); } } } return 0; }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步