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: ...
随机推荐
- 彻底理解tomcat是怎样多线程处理http请求并将代码执行到controller里的的
彻底理解tomcat是怎样多线程处理http请求并将代码执行到controller里的的 1.线程池,thread = threadPool.getThread(),thread.executeHtt ...
- Firefox OS简单介绍
Firefox OS系统架构框图 一些Firefox相关的术语简单介绍: B2G Boot to Gecko 的简称. Boot to Gecko Firefox OS 操作系统的project代号. ...
- 基于redis ae实现 Linux中的文件系统监控机制(inotify)
(英文部分为转的.代码是个人代码) 1 What's inotify The inotify API provides a mechanism for monitoring file system ...
- crm操作报价单实体
using System; using Microsoft.Xrm.Sdk; using Microsoft.Crm.Sdk.Messages; using Microsoft ...
- Oracle分析函数ntile
有这么一个需求.将课程的成绩分成四个等级,为学生打A.B.C.D的绩效. drop table course purge; create table course ( id number, g ...
- bzoj2748: [HAOI2012]音量调节(背包)
2748: [HAOI2012]音量调节 题目:传送门 题解: sb省选题..呵呵一眼背包: f[i][j]表示第i时刻能否为音量j 代码: #include<cstdio> #inclu ...
- GitHub客户端和Shell的基本操作和理解
GitHub客户端和Shell指令的简单实用 客户端操作, web端操作, shell指令操作. 掌握了这三种操作,基本上就可以很好的运用gitHub了. 创建项目, 可以通过web端进行创建. 可以 ...
- Opencv Mat矩阵中data、size、depth、elemSize、step等属性的理解
data: uchar类型的指针,指向Mat数据矩阵的首地址.可以理解为标示一个房屋的门牌号: dims: Mat矩阵的维度,若Mat是一个二维矩阵,则dims=2,三维则dims=3,大多数情况下处 ...
- System.IO.FileLoadException异常
未能加载文件或程序集“NHibernate, Version=4.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4”或它的某一个依赖 ...
- js小知识colspan和rowspan
colspan和rowspan这两个属性用于合并表格的列或者行. colspan是"column span"(跨列)的缩写,所以colspan属性用在td标签中,用来跨列合并单元 ...