poj1703 Find them, Catch them 并查集
poj(1703)
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 26992 | Accepted: 8188 |
Description
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
Output
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次操作,D[a][b]代表a和b不是一个帮派,A[a][b]询问a和b是什么关系,即:关系不确定,在一个帮派,不再一个帮派。
分析:并查集,用一个辅助数组pre[],(pre[a]=b||pre[b]=a)代表a的敌人是b,b的敌人是a,输入D的时候有四种情况构造并查集
(1)若pre[a]==-1&&pre[b]==-1,则:pre[a]=b;pre[b]=a;
(2)若pre[a]==-1&&pre[b]!=-1:则:pre[a]=b;make(a,pre[b]):把b的敌人和a并在一棵树里,即为朋友;
(3)若pre[a]1=-1&&pre[b]==-1;则:同上;
(4)若pre[a]!!=-1&&pre[b]!=-1:则:make(a,pre[b]),make(pre[a],b);
对于每次A询问:
若finde(a)==finde(b)一定是朋友;
否则有两种情况,要么不确定,要么是敌人;
当:finde(pre[a])==finde(b)即:a的老大的敌人与b的老大是朋友,则a和b一定是敌人
否则是不确定的
程序:
#include"stdio.h"
#include"string.h"
#define M 100004
int f[M];
int pre[M];
int finde(int x)
{
if(x!=f[x])
f[x]=finde(f[x]);
return f[x];
}
void make(int a,int b)
{
int x=finde(a);
int y=finde(b);
if(x!=y)
f[x]=y;
}
int main()
{
int w,n,m,a,b,i;
char ch[2];
scanf("%d",&w);
while(w--)
{
scanf("%d%d",&n,&m);
for(i=0;i<=n;i++)
f[i]=i;
memset(pre,-1,sizeof(pre));
while(m--)
{
scanf("%s%d%d",ch,&a,&b);
if(ch[0]=='D')
{
if(pre[a]==-1&&pre[b]==-1)
{
pre[a]=b;
pre[b]=a;
}
else if(pre[a]==-1&&pre[b]!=-1)
{
make(a,pre[b]);
pre[a]=b;
}
else if(pre[a]!=-1&&pre[b]==-1)
{
make(b,pre[a]);
pre[b]=a;
}
else
{
make(pre[a],b);
make(pre[b],a);
}
}
else
{
if(finde(a)==finde(b))
printf("In the same gang.\n");
else
{
if(finde(pre[a])==finde(b))
printf("In different gangs.\n");
else
printf("Not sure yet.\n");
}
}
}
}
return 0;
}
poj1703 Find them, Catch them 并查集的更多相关文章
- POJ-1703 Find them, Catch them(并查集&数组记录状态)
题目: The police office in Tadu City decides to say ends to the chaos, as launch actions to root up th ...
- POJ 1703 Find them, catch them (并查集)
题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2 D 3 4 D 5 6...这就至少有3个集合了.并且 ...
- poj1703--Find them, Catch them(并查集应用)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32073 Accepted: ...
- POJ1703-Find them, Catch them 并查集构造
Find them, Catch them 好久没有做并查集的题,竟然快把并查集忘完了. 题意:大致是有两个监狱,n个 ...
- POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集
POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...
- POJ 1703 Find them, Catch them 并查集的应用
题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...
- poj1703_Find them, Catch them_并查集
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42451 Accepted: ...
- poj.1703.Find them, Catch them(并查集)
Find them, Catch them Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
- POJ 1703 Find them, Catch them(并查集高级应用)
手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...
随机推荐
- passing parameters by value is inefficient when the parameters represent large blocks of data
Computer Science An Overview _J. Glenn Brookshear _11th Edition_C Note that passing parameters by va ...
- 【转】Problems with HTTPS, HttpWebRequest, and iOS?
We're using HttpWebRequest to hit HTTPS urls, on iOS. In Debug, local builds, etc. everything works ...
- jboss中文支持
一.中文问题 如果操作系统不支持中文, 应首先使操作系统上的Server支持中文. 修改run.conf中 -Dfile.encoding=gbk -Ddefault.client.encoding= ...
- 实例讲述PHP面向对象的特性;;;php中const与define的使用区别
php中const与define的使用区别 1.const:类成员变量定义,一旦定义且不能改变其值. define:定义全局常量,在任何地方都可以访问.2.define:不能在类中定义,而const可 ...
- WPF绑定Model的实例对象
创建一个用户控件 1. behindcode指定this.DataContext=XXViewModel 2. "{Binding DataContext.EditModel.MId,Rel ...
- RTSP协议详解
RTSP(Real Time Streaming Protocol)是由Real Network和Netscape共同提出的如何有效地在IP网络上传输流媒体数据的应用层协议.RTSP对流媒体提 ...
- rabbitMq 转自 http://gaoyangang.iteye.com/blog/1566600
rabbitMq 转自 http://gaoyangang.iteye.com/blog/1566600
- Processes vs Threads
A process is an executing instance of an application. What does that mean? Well, for example, when y ...
- PySe-002-Py-简单示例及编码设定
非常简单而又国际化的例子 Hello World!在控制台依次输入命令:python, print "This is first program %s" % "Hello ...
- javascript知识点记录(1)
javascript一些知识点记录 1.substring,slice,substr的用法 substring 和slice 都有startIndex 和 endIndex(不包括endInex),区 ...