POJ 1703 Find them, Catch them(关系并查集)
2016-12-14 22:29
375 查看
Find them, Catch them
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."
Sample Input
Sample Output
Source
POJ Monthly--2004.07.18
题目思路:这题也是。。。
其实总的来说就是食物链的简化版本,把食物链中的三种关系简化成了两种关系。做法是一样的,用0来表示同一帮派,1来表示不同帮派。那么如果字母是d,那么连接两个根节点就可以用((cun[a]+cun[b]+1)%2),cun数组存的是节点和其父亲节点的关系。这题最坑的就是,不能用cin,(可以试一试)对于cin党的我来说,一直找优化算法。。。并且 ios_base::sync_with_stdio(false);并没有起到什么作用。。。。最终抱着试一试的心态改了scanf,结果就不T了。悲伤
代码如下:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxx=100000+50;
int fa[maxx],cun[maxx];
int n,m;
int findnum(int x)
{
if(x==fa[x])return x;
else{
int root=findnum(fa[x]);
cun[x]=(cun[x]+cun[fa[x]])%2;
return fa[x]=root;
}
}
void init(){
for(int i=0;i<=n;i++)fa[i]=i;
memset(cun,0,sizeof(cun));
}
int main(){
int t,a,b;char c[2];
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
while(m--)
{
scanf("%s%d%d",c,&a,&b);
int a1=findnum(a);
int b1=findnum(b);
if(c[0]=='D')
{//he
fa[b1]=a1;
cun[b1]=(cun[a]+cun[b]+1)%2;
}
else {
if(a1!=b1)cout<<"Not sure yet."<<endl;//检查各自父节点,如果两个的根节点相同,那么就表示他们之间有关系,如果不相同忙就表示没有
else {
if((cun[a]+cun[b])%2==0)cout<<"In the same gang."<<endl;//如果两个之间有关系了,那么通过各自和根节点之间的关系可以判断是否在同一帮派
else cout<<"In different gangs."<<endl;
}
}
}
}
return 0;}
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 43097 | Accepted: 13248 |
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来表示不同帮派。那么如果字母是d,那么连接两个根节点就可以用((cun[a]+cun[b]+1)%2),cun数组存的是节点和其父亲节点的关系。这题最坑的就是,不能用cin,(可以试一试)对于cin党的我来说,一直找优化算法。。。并且 ios_base::sync_with_stdio(false);并没有起到什么作用。。。。最终抱着试一试的心态改了scanf,结果就不T了。悲伤
代码如下:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int maxx=100000+50;
int fa[maxx],cun[maxx];
int n,m;
int findnum(int x)
{
if(x==fa[x])return x;
else{
int root=findnum(fa[x]);
cun[x]=(cun[x]+cun[fa[x]])%2;
return fa[x]=root;
}
}
void init(){
for(int i=0;i<=n;i++)fa[i]=i;
memset(cun,0,sizeof(cun));
}
int main(){
int t,a,b;char c[2];
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
init();
while(m--)
{
scanf("%s%d%d",c,&a,&b);
int a1=findnum(a);
int b1=findnum(b);
if(c[0]=='D')
{//he
fa[b1]=a1;
cun[b1]=(cun[a]+cun[b]+1)%2;
}
else {
if(a1!=b1)cout<<"Not sure yet."<<endl;//检查各自父节点,如果两个的根节点相同,那么就表示他们之间有关系,如果不相同忙就表示没有
else {
if((cun[a]+cun[b])%2==0)cout<<"In the same gang."<<endl;//如果两个之间有关系了,那么通过各自和根节点之间的关系可以判断是否在同一帮派
else cout<<"In different gangs."<<endl;
}
}
}
}
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【关系并查集】
- poj1703 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(带权并查集)