poj--1703--Find them, Catch them(并查集巧用)
Time Limit: 1000MS | Memory Limit: 10000KB | 64bit IO Format: %I64d & %I64u |
Description
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
as described above.
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.
Source
输出少了一个“.”wa了四次-.-||
题意:有两个帮派,现在给了n个人m个关系,D:关系表示两个人不在一个帮派,A:查询两个人是否在一个帮派,不确定的话输出“Not sure yet.“,同一个输出”In the same gang.“,不在一个输出”In different gangs.“。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int pre[100010],rank[100010];
int find(int x)
{
if(pre[x]==x)
return x;
else
{
int temp=pre[x];
pre[x]=find(pre[x]);
rank[x]=(rank[x]+rank[temp])%2;//将x变为和temp一样的帮派
return pre[x];
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(rank,0,sizeof(rank));//rank表示层数,两层表示不一样的帮派
char op[2];
int x,y;
int m,n;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
pre[i]=i;
for(int i=0;i<m;i++)
{
scanf("%s%d%d",op,&x,&y);
int fx=find(x);
int fy=find(y);
if(op[0]=='D')
{
pre[fy]=fx;
rank[fy]=(rank[x]-rank[y]+1)%2;
continue;
}
else
{
if(fx!=fy)//根节点不一样应该就是尚未分配,所以不确定
printf("Not sure yet.\n");
else if(rank[x]!=rank[y])//层数不一样就不在一个帮派
printf("In different gangs.\n");
else if(rank[x]==rank[y])//层数一样并且根节点相同
printf("In the same gang.\n");
}
}
}
return 0;
}
poj--1703--Find them, Catch them(并查集巧用)的更多相关文章
- 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(并查集)
Find them, Catch them Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I6 ...
- POJ 1703 Find them, catch them (并查集)
题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2 D 3 4 D 5 6...这就至少有3个集合了.并且 ...
- POJ 1703 Find them, Catch them 并查集的应用
题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...
- POJ 1703 Find them, Catch them(并查集高级应用)
手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...
- POJ 1703 Find them, Catch them 并查集,还是有点不理解
题目不难理解,A判断2人是否属于同一帮派,D确认两人属于不同帮派.于是需要一个数组r[]来判断父亲节点和子节点的关系.具体思路可参考http://blog.csdn.net/freezhanacmor ...
- [并查集] POJ 1703 Find them, Catch them
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43132 Accepted: ...
- POJ 1703 Find them, Catch them(种类并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 41463 Accepted: ...
- hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them
http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...
- POJ 1703 Find them, Catch them (数据结构-并查集)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 31102 Accepted: ...
随机推荐
- 楼宇自控-RS232\RS485\RS422
1.rs-232-c rs-232-c是美国电子工业协会eia(electronic industry association)制定的一种串行物理接口标准.rs是英文"推荐标准"的 ...
- WinServer-IIS-SEO优化
来自为知笔记(Wiz)
- Java IO(三) 之 FileInputStream
前言: 对于文件系统中的文件.都能够使用FileInputStream流类以二进制的形式进行读取.可是因为Java本身的定位在JVM之上,没有处理计算机底层的能力.因此一些涉及底层处理的方法都是使用n ...
- Java线程演示样例 - 继承Thread类和实现Runnable接口
进程(Process)和线程(Thread)是程序执行的两个基本单元. Java并发编程很多其它的是和线程相关. 进程 进程是一个独立的执行单元,可将其视为一个程序或应用.然而,一个程序内部同事还包括 ...
- 多本Web前端深度修炼书籍(提供网盘下载链接)
书籍介绍:这本书涵盖了html5新增标签和功能,而且提供了jquerymobile,Phonegap,Sencha Touch框架的介绍和应用,最后还带了一个移动web应用的样例,绝对是移动web开发 ...
- CxImage 简单配置与使用
CxImage 简单配置与使用 如果本篇文章还不能解决你在生成解决方案以及便宜过程中的问题 请参阅: http://blog.csdn.net/afterwards_/article/details/ ...
- 使用终端改变MAC默认截图存放地址
使用终端改变MAC默认截图存放地址的过程主要分为两步: 第一步:输入如下命令,回车 defaults write com.apple.screencapture location 要存放到的位置的绝对 ...
- Java中如何解决线程安全问题
给出一个问题,如下: 解决方案如下: public class Demo_5 { public static void main(String[] args) { //创建一个窗口 TicketWin ...
- 【转载】java编程中'为了性能'一些尽量做到的地方
1.尽量在合适的场合使用单例 使用单例可以减轻加载的负担,缩短加载的时间,提高加载的效率,但并不是所有地方都适用于单例,简单来说,单例主要适用于以下三个方面 第一,控制资源的使用,通过线程同步来控制资 ...
- 1,http协议的细节部分学习
http协议(80端口)https(443端口) 主要是一直对三次握手模模糊糊,并且抓包的时候不知道那些Accept.User-Agent什么意思,就仔细找课程学了一下. 学习简介: 1,涉及工具(w ...