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 ...
随机推荐
- The "get" method should be used when the form is idempotent---正交的两个概念---
https://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.1 17.13.1 Form submission method The me ...
- jdk Tomcat配置
安装Tomcat需要先安装JDKJDK安装JDK安装会有两次,两次不能再同一目录 你可以新建两个文件夹 JDK JRE 第一次安装在JDK 第二次在JRE在我的电脑右键属性 高级系统设置 环境变量 系 ...
- java编程算法
一.字符串相关操作 String s = " Hello java,hello android,hello OOP,HELLO String,hello JAVASE!"; Sys ...
- Redis学习笔记(1)-Key
package cn.com; import java.text.ParseException; import java.util.List; import java.util.Set; import ...
- this绑定
js中关于this的用法,在初期时候经常会弄混,即使现在,也不敢说就一定不会混,但是起码好很多了. 函数执行过程中,主要有4种方法决定this的绑定对象. 分别为:默认绑定.隐式绑定. 显示绑定和ne ...
- ArcGIS API for Silverlight 当DataGrid选中项时,地图聚焦弹出窗口,并可以播放音频文件
原文:ArcGIS API for Silverlight 当DataGrid选中项时,地图聚焦弹出窗口,并可以播放音频文件 先看效果图,然后上代码: <UserControl x:Class= ...
- 【上手centos】一、前情以及sublime_text_3安装
笔记本自大一入手,只重装过一次系统,从不曾拆机清灰过.读研之后,日常工作与学习都在实验室进行,笔记本一直在宿舍的桌子上落灰,只偶尔打开来看个电影.上周末,心血来潮,把笔记本抱到实验室拆了清灰,以前一直 ...
- javac 错误: 编码GBK的不可映射字符
在java代码中有中文注释,使用javac编译时,出现编码报错. 错误: 编码GBK的不可映射字符 问题原因: 在编译的时候,如果我们没有用-encoding参数指定我们的JAVA源程序的编码格式,则 ...
- imx6 android5.1 打开 调试串口
imx6的工板烧录android 5.1的镜像,uboot中能使用debug口,kernel,文件系统中不能使用debug口. 打开kenel和文件系统debug口方法,在uboot的bootargs ...
- PHP接口类interface的正确使用方法
对于那些初学PHP语言的人来说,对于PHP的接口类也许了解的还不是很深入,接下来我们就来具体讲述PHP接口类interface的使用方法. 如何正确运用PHP XMLReader解析XML文档 深入解 ...