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: ...
随机推荐
- Leetcode--easy系列4
#58 Length of Last Word Given a string s consists of upper/lower-case alphabets and empty space char ...
- Java 递归、尾递归、非递归 处理阶乘问题
n!=n*(n-1)! import java.io.BufferedReader; import java.io.InputStreamReader; /** * n的阶乘,即n! (n*(n-1) ...
- HDU5638 / BestCoder Round #74 (div.1) 1003 Toposort 线段树+拓扑排序
Toposort 问题描述 给出nn个点mm条边的有向无环图. 要求删掉恰好kk条边使得字典序最小的拓扑序列尽可能小. 输入描述 输入包含多组数据. 第一行有一个整数TT, 表示测试数据组数. 对 ...
- poj_1952最大下降子序列,统计个数
其实不算难的一道题,但憋了我好久,嗯,很爽. #include<iostream> #include<cstdio> #include<string.h> #inc ...
- bzoj2464: 中山市选[2009]小明的游戏(最短路)
2464: 中山市选[2009]小明的游戏 题目:传送门 题解: 最短路的裸题... 代码: #include<cstdio> #include<cstring> #inclu ...
- DB-MySQL:目录
ylbtech-DB-MySQL:目录 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://ylbt ...
- 如何在Windows下安装Linux子系统(Ubuntu,openSUSU,SUSU Linux Server)
注意:只有win10才能安装,安装的linux没有图形界面. 1.首先在win10设置 --> 更新与安装 --> 针对开发人员 ,选择开发人员模式. 2.win10 Cortana -- ...
- C#生成高清缩略图 (装在自OPEN经验库)
来源 http://www.open-open.com/lib/view/open1389943861320.html 代码如下实现图片的高清缩略图 /// <summary> /// 为 ...
- php报错权限设置
<?php //禁用错误报告 error_reporting(0); //报告运行时错误 error_reporting(E_ERROR | E_WARNING | E_PARSE); //报告 ...
- nodejs 封装mysql连接池
写在前面的 在nodejs后台代码中,我们总是会和数据库打交道 然而,每次都要写数据库的配置以及连接和断开,不胜其烦 我就封装了一个连接池模块,不足之处还请多多批评 上代码 一下是写在mysqls.j ...