poj 1703 Find them, Catch them
来源:互联网 发布:优化探究 编辑:程序博客网 时间:2024/06/10 03:52
Find them, Catch them
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 24150 Accepted: 7225
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
15 5A 1 2D 1 2A 1 2D 2 4A 1 4
Sample Output
Not sure yet.In different gangs.In the same gang.
种类并查集
AC代码:
#include <cstdio>#include <cstring>#define N 100005using namespace std;int father[N],rank[N];int Find_set(int n){ int p=father[n]; if(n==father[n]) return n; father[n]=Find_set(father[n]); rank[n]=(rank[n]+rank[p])%2; return father[n];}void Union(int a,int b){ int ra=Find_set(a); int rb=Find_set(b); if(ra!=rb) { father[ra]=rb; rank[ra]=(rank[a]+rank[b]+1)%2; }}int main(){ int n,m,x,t,y; char ch; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { father[i]=i; rank[i]=0; } getchar(); while(m--) { scanf("%c%d%d",&ch,&x,&y); if(ch=='D') Union(x,y); else { int ra=Find_set(x); int rb=Find_set(y); if(ra!=rb) printf("Not sure yet.\n"); else if(rank[x]!=rank[y]) printf("In different gangs.\n"); else printf("In the same gang.\n"); } getchar(); } } 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 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
- C++ 小心你的析构函数不会执行
- 聚簇表
- IOS开发(23)之UITableView控件
- 黑马程序员——JAVA学习日志1
- android 集成第三方静态库的编译方法
- poj 1703 Find them, Catch them
- Poco::Base64Encoder,Poco::Base64Decoder
- osx 配置 Apache+Mysql+Php
- FFmpeg 移植 Android
- 对象的引用
- HttpSession与Hibernate中session的区别
- Qwt源码解读之拾取操作类(一)
- 在TextView中插入图片
- 物化视图