POJ1703——Find them, Catch them 并查集应用
2016-03-15 09:27
337 查看
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
题目大意:有两个黑帮,有n个人,m次操作,一种告诉你某两个人属于不同的黑帮,一种要你查询两个人是不是在同一个黑帮。
分析:所以难点在哪里呢,因为告诉你的信息是某两个人属于不同的黑帮,所以你不能直接合并。
怎么分类呢?经典的方法,用x,x+n分别代表x属于A类和x属于B类。
怎么合并呢?比如告诉你x和y不在一个集合,那么就是两种可能,x-A,y-B或者x-B,y-A,我们把x,y+n合并,再把y,x+n合并。同一棵树上的时间同时发生或者同时不发生。也就是说,若x-A,则y一定属于B。
怎么查询呢?如果x和y在一棵树上,或者x+n和y+n在一棵树上,就是同一个黑帮的。如果x和y+n在同一棵树上,或者x+n和y在同一棵树上,就是不同的黑帮。否则不能判断。
代码如下
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <queue>
#include <ctype.h>
#include <vector>
using namespace std;
#define INF 1000000000
typedef long long LL;
#define MAXN 200010
int par[MAXN];//父亲
int r[MAXN];//树的高度
//初始化n个元素
void init(int n)
{
for(int i=0;i<n;i++)
{
par[i]=i;
r[i]=0;
}
}
//查询数的根
int find(int x)
{
if(par[x]==x)
return x;
else
return par[x]=find(par[x]);
}
//合并x和y所属的集合
void unite(int x,int y){
x=find(x);
y=find(y);
if(x==y)
return;
if(r[x]<r[y])
par[x]=y;
else
par[y]=x;
if(r[x]==r[y])
r[x]++;
}
//判断x和y是否在同一个树上
bool same(int x,int y)
{
return find(x)==find(y);
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
char op;
int a,b;
scanf("%d%d",&n,&m);
init(2*n);
getchar();
while(m--)
{
scanf("%c%d%d",&op,&a,&b);
a--,b--;
if(op=='D')
{
unite(a, b+n);
unite(a+n,b);
}
else if(op=='A')
{
if(same(a,b)||same(a+n,b+n))
printf("In the same gang.\n");
else if(same(a,b+n)||same(a+n,b))
printf("In different gangs.\n");
else
printf("Not sure yet.\n");
}
getchar();
}
}
}
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 39239 | Accepted: 12059 |
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.
题目大意:有两个黑帮,有n个人,m次操作,一种告诉你某两个人属于不同的黑帮,一种要你查询两个人是不是在同一个黑帮。
分析:所以难点在哪里呢,因为告诉你的信息是某两个人属于不同的黑帮,所以你不能直接合并。
怎么分类呢?经典的方法,用x,x+n分别代表x属于A类和x属于B类。
怎么合并呢?比如告诉你x和y不在一个集合,那么就是两种可能,x-A,y-B或者x-B,y-A,我们把x,y+n合并,再把y,x+n合并。同一棵树上的时间同时发生或者同时不发生。也就是说,若x-A,则y一定属于B。
怎么查询呢?如果x和y在一棵树上,或者x+n和y+n在一棵树上,就是同一个黑帮的。如果x和y+n在同一棵树上,或者x+n和y在同一棵树上,就是不同的黑帮。否则不能判断。
代码如下
#include <stdio.h>
#include <algorithm>
#include <cstring>
#include <iostream>
#include <cmath>
#include <queue>
#include <ctype.h>
#include <vector>
using namespace std;
#define INF 1000000000
typedef long long LL;
#define MAXN 200010
int par[MAXN];//父亲
int r[MAXN];//树的高度
//初始化n个元素
void init(int n)
{
for(int i=0;i<n;i++)
{
par[i]=i;
r[i]=0;
}
}
//查询数的根
int find(int x)
{
if(par[x]==x)
return x;
else
return par[x]=find(par[x]);
}
//合并x和y所属的集合
void unite(int x,int y){
x=find(x);
y=find(y);
if(x==y)
return;
if(r[x]<r[y])
par[x]=y;
else
par[y]=x;
if(r[x]==r[y])
r[x]++;
}
//判断x和y是否在同一个树上
bool same(int x,int y)
{
return find(x)==find(y);
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n,m;
char op;
int a,b;
scanf("%d%d",&n,&m);
init(2*n);
getchar();
while(m--)
{
scanf("%c%d%d",&op,&a,&b);
a--,b--;
if(op=='D')
{
unite(a, b+n);
unite(a+n,b);
}
else if(op=='A')
{
if(same(a,b)||same(a+n,b+n))
printf("In the same gang.\n");
else if(same(a,b+n)||same(a+n,b))
printf("In different gangs.\n");
else
printf("Not sure yet.\n");
}
getchar();
}
}
}
相关文章推荐
- PHP设计模式 适配器模式
- 【Boost】boost库asio详解6——boost::asio::error的用法浅析
- 上网调查一下目前流行的源程序版本管理软件和项目管理软件都有哪些?各有什么优缺点?
- iOS CLLocationManager 定位
- 【Boost】boost库asio详解5——resolver与endpoint使用说明
- 用cookie存取变量
- 虚拟机的简单使用
- 详解iOS App开发中UIViewController的loadView方法使用
- javax.el.PropertyNotFoundException: Property 'Name' not found on type com.min.test.User
- 【Boost】boost库asio详解4——deadline_timer使用说明
- 面试题记录
- Android 通知栏Notification
- 跟我一起学Microsoft SQL Server 2012 Internals(1.6)
- 有关深度学习领域的几点想法
- c++常见面试题30道
- 【J2EE十三个规范】数据层之JDBC
- php获取授时服务器时间 time zone
- QML中的RotationAnimation旋转动画
- 【Boost】boost库asio详解3——io_service作为work pool
- 使用spring redisTemplate.getValueSerializer()遇到的问题