POJ1703Find them, Catch them[种类并查集]
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 42416 | Accepted: 13045 |
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
罪犯只可能属于两个团伙中的一个,
现在给出M个条件(D a b表示a和b不在同一团伙),
对于每一个询问(A a b)确定a,b是不是属于同一团伙或者不能确定。
//
// main.cpp
// poj1703
//
// Created by Candy on 30/10/2016.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int N=1e5+;
typedef long long ll;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int T,n,m,a,b;
char c[];
int fa[N],v[N];
inline int find(int x){
if(x==fa[x]) return x;
int root=find(fa[x]);
v[x]=(v[x]+v[fa[x]])&;
return fa[x]=root;
}
inline void unn(int x,int y){
int f1=find(x),f2=find(y);
if(f1!=f2){
fa[f1]=f2;
v[f1]=(v[x]+v[y]+)&;
}
}
int main(int argc, const char * argv[]) {
T=read();
while(T--){
n=read();m=read();
for(int i=;i<=n;i++) fa[i]=i,v[i]=;
for(int i=;i<=m;i++){
scanf("%s",c);a=read();b=read();
if(c[]=='D') unn(a,b);
else{
int f1=find(a),f2=find(b);
if(f1!=f2) puts("Not sure yet.");
else if(v[a]==v[b]) puts("In the same gang.");
else puts("In different gangs.");
}
}
} return ;
}
POJ1703Find them, Catch them[种类并查集]的更多相关文章
- [poj1703]Find them, Catch them(种类并查集)
题意:食物链的弱化版本 解题关键:种类并查集,注意向量的合成. $rank$为1代表与父亲对立,$rank$为0代表与父亲同类. #include<iostream> #include&l ...
- poj1703Find them, Catch them(并查集以及路径压缩)
/* 题目大意:有两个不同的黑帮,开始的时候不清楚每个人是属于哪个的! 执行两个操作 A a, b回答a, b两个人是否在同一帮派,或者不确定 D a, b表示a, b两个人不在同一个帮派 思路:利用 ...
- poj--1703--Find them, Catch them(并查集巧用)
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64 ...
- POJ 1703 Find them,Catch them ----种类并查集(经典)
http://blog.csdn.net/freezhanacmore/article/details/8774033?reload 这篇讲解非常好,我也是受这篇文章的启发才做出来的. 代码: #i ...
- POJ 1703 Find them, Catch them(种类并查集)
题目链接 这种类型的题目以前见过,今天第一次写,具体过程,还要慢慢理解. #include <cstring> #include <cstdio> #include <s ...
- POJ1703--Find them, Catch them(种类并查集)
Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 32909Accepted: 10158 Description The polic ...
- poj1703 Find them, Catch them(种类并查集
题目地址:http://poj.org/problem?id=1703 题目大意:警察抓了n个坏蛋,这些坏蛋分别属于龙帮或蛇帮.输入m个语句,A x y询问x和y的关系(在一个帮派,不在,不能确定), ...
- POJ1703 Find them Catch them 关于分集合操作的正确性证明 种类并查集
题目链接:http://poj.org/problem?id=1703 这道题和食物链那道题有异曲同工之处,都是要处理不同集合之间的关系,而并查集的功能是维护相同集合之间的关系.这道题中有两个不同的集 ...
- NOI2001|POJ1182食物链[种类并查集 向量]
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65430 Accepted: 19283 Description ...
随机推荐
- iOS阶段学习第35天笔记(Touch手势介绍)
一.Touch手势 1.利用手势实现UIButton移动效果 实例代码 1) 创建一个继承自UIButton的类 MyButton.h 代码实现 #import <UIKit/UIKit.h ...
- Oracle函数-DECODE
DECODE翻译为解码或译码,因为它可以把查询结果翻译成令用户容易理解的内容. 语法: expr: 表达式 search:搜索值 result:结果值 default:默认值 DECODE函数的第一个 ...
- WebStorage记录滚动条位置
因关注公众号<HTML5学堂>看到这篇文章 "利用本地存储,记录滚动条的位置" ,便好奇敲来试试,然后又看了一些关于WebStorage的资料 附上这篇文章的地址 ht ...
- 从头开始构建LINUX [LFS 脚本]
脚本共享在这 http://pan.baidu.com/s/1nt6yiH7 version-check.sh : 这个是检查HOST机器的软件依赖情况 host-dep.sh:针对ubuntu10_ ...
- list集合的排序Comparator和Collections.sort
一个例子 package sortt; import java.util.ArrayList; import java.util.Collections; import java.util.Compa ...
- UI笔记
tableView 自定义cell 还有之前的轮播图整理
- iOS UIPageViewController
UIPageViewController是App中常用的控制器.它提供了一种分页效果来显示其childController的View.用户可以通过手势像翻书一样切换页面.切换页面时看起来是连续的,但静 ...
- 【代码笔记】iOS-获得徐家汇的天气预报
一,代码. //获得徐家汇的天气预报 -(void)getWeatherInfo{ NSError *error; NSURLRequest *request = [NSURLRequest requ ...
- CSS3-03 样式 2
前言 在上一篇的博客中,阐述了 CSS 盒模型中的 Margin.Border.Padding 三个样式.但是总觉得,这些东西好像是 HTML 元素的包装样式,真正的要点是 HTML 元素(即:盒模型 ...
- JVM-加载,链接,初始化
Java Virtual Machine 动态的加载,链接和初始化类和接口.那么,Class 二进制文件是怎样被 JVM 加载到内存中的?JVM 如何描述一个 Java 类?类或接口怎么才能让 JVM ...