poj1703_Find them, Catch them_并查集
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 42451 | Accepted: 13059 |
Description
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.
Source
#include <iostream>
#include <cstdio> #define MAX_N 1000000+5 using namespace std; int par[MAX_N];//父节点
int depth[MAX_N];//深度 void init(int n){
for(int i=;i<=n;i++){
par[i]=i;
depth[i]=;
}
}
int find_father(int t){
if(t==par[t]){
return t;
}else{
return par[t]=find_father(par[t]);
//实现了路径压缩
}
}
void unite(int t1,int t2){
int f1=find_father(t1);
int f2=find_father(t2);
if(f1==f2){
return ;
}
if(depth[f1]<depth[f2]){
par[f1]=f2;
}else{
par[f2]=f1;
if(depth[f1]==depth[f2]){
depth[f1]++;
//记录深度
}
}
} bool same(int x,int y){
return find_father(x)==find_father(y);
} int main()
{
int t;
int n,m;
char c;
int a,b;
scanf("%d",&t);
while(t--){
scanf("%d %d",&n,&m);
init(n*);
while(m--){
getchar();
scanf("%c %d %d",&c,&a,&b);
if(c=='D'){
unite(a,b+n);
unite(a+n,b);
}else{
if(same(a,b+n)){
printf("In different gangs.\n");
continue;
}
if(same(a,b)){
printf("In the same gang.\n");
continue;
}else{
printf("Not sure yet.\n");
continue;
}
}
}
}
return ;
}
poj1703_Find them, Catch them_并查集的更多相关文章
- poj1703 Find them, Catch them 并查集
poj(1703) Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26992 ...
- 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 并查集的应用
题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...
- 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 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 ...
随机推荐
- C/C++实践笔记 004
转义字符 #define _CRT_SECURE_NO_WARNINGS#include<stdio.h>#include<stdlib.h> void main1() { c ...
- 认识和使用NSOperation
原文链接:http://www.jianshu.com/p/2de9c776f226 NSOperation是OC中多线程技术的一种,是对GCD的OC包装.它包含队列(NSOperationQueue ...
- NGUI Tween动画Scale与Transform冲突
NGUI中我们要同时完成Scale与Transform的效果,会发现动画并不是同我们想的那样运行的. 原因就是Tween Scale与Tween Transform的冲突调用. Tween Scale ...
- BZOJ2815: [ZJOI2012]灾难
传送门 学LCA的时候根本没意识到LCA可以有这么多玩法. 这玩意据说是个高级数据结构(支配树)的弱化版,蒟蒻没学过呀.所以出题人提出一个概念叫灾难树. 我理解的灾难树的意思实际上是属于DAG的一个子 ...
- List集合的迭代器方法
1.后台JAVA代码的实现 //获取所有的支付方式的迭代器 // 获取支付方式 @RequestMapping(value = "get/payed/type", method = ...
- 分析DH加密算法,一种适基于密钥一致协议的加密算法。
DH Diffie-Hellman算法(D-H算法),密钥一致协议.是由公开密钥密码体制的奠基人Diffie和Hellman所提出的一种思想.简单的说就是允许两名用户在公开媒体上交换信息以生成&quo ...
- Win10---------专区
待完善中---------------------------------- -----------------------------------------The End------------- ...
- win7使用iis并搭建 图片服务器
1.打开控制面板 2.程序-卸载程序 3.点击左边的 打开或关闭windows功能 4.如下图所示,找到internet信息服务勾选.顺便把FTP服务器也全部勾选了,后面会用到 5.进入 控制面板 – ...
- 盒子模型简单理解(box-sizing)
普通解析: 概念图示和公式: html结构 <div class="num1"></div> 1.只写 width.height(写背景是为了区分) .nu ...
- 关于学习JavaScript 的 高三编程 一些心得
面对JS 问题来说,很多的细节问题以及 弱类型转换的问题,往往会成为学习js 路上的一个阻碍. 那么问题来了,今天我看到的是 高三 里面的 基本概念的 语法问题. 直奔主题.(还是帖代码先) sw ...