poj1703 Find them, Catch them(并查集)
https://vjudge.net/problem/POJ-1703
9ms多,卡着时间过了。上次一道并查集也是这样,总觉得要学一波并查集的优化。。
续:好像是可以只做一层存放敌人即可。
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<stack>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define INF 0x3f3f3f3f
typedef unsigned long long ll;
using namespace std;
const int N=;
int t, n, m, x, y, pre[];
char c;
int find(int x)
{
while(x != pre[x]){
x = pre[x];
}
return x;
}
int is_same(int a, int b)
{
int tx = find(a);
pre[a] = tx;
int ty = find(b);
pre[b] = ty;
if(tx == ty) return ;
return ;
}
void join(int a, int b)
{
int tx = find(a);
pre[a] = tx;
int ty = find(b);
pre[b] = ty;
pre[tx] = ty;
}
int main()
{
scanf("%d", &t);
while(t--){
scanf("%d%d", &n, &m);
for(int i = ; i <= N*; i++){
pre[i] = i;
}
//c = getchar();
for(int i = ; i < m; i++){
c = getchar();
scanf("%c%d%d", &c, &x, &y);
if(c == 'A'){
if(is_same(x, y)||is_same(x+N, y+N)){
printf("In the same gang.\n");
}
else{
if(!is_same(x, y)&&!is_same(x, y+N)){
printf("Not sure yet.\n");
}
else printf("In different gangs.\n");
}
}
else{
join(x, y+N);
join(x+N, y);
}
}
}
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(并查集&数组记录状态)
题目: 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 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入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 并查集的应用
题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...
- poj1703_Find them, Catch them_并查集
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 42451 Accepted: ...
- 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 ...
随机推荐
- Java基础知识➣面向对象(八)
概述 Java和C#都是面向对象语言,面向对象编程是目前高级语言习惯的编程模式,与C++编写过程编程而言,面向对象使用起来高效.灵活:面向对象的三个特征:封装.继承和多态. Java面向对象 1.类封 ...
- 【转载】Python and Mysql Andy Dustman
0. http://mysql-python.sourceforge.net/ Python and MySQL: This is a presentation I did a couple year ...
- Ubuntu安装搜狗sougou输入法
昨天ubuntu闪屏后进不去系统,之后重新安装的Ubuntu14.04,在软件中心安装了新立得软件包管理器 在网上下载搜狗输入法forlinux 网址:http://pinyin.sogou.com/ ...
- Python_模块介绍
模块:一组或者一个.py文件实现了某个功能的代码集合 模块分为三种: 自定义模块 内置标准模块(又称标准库):Python自带的模块 开源模块:自己写的模块,有可以供人使用的功能 开源模块的集散地:P ...
- easyui 信息提示
/*消息提示begin*/jQuery.Info = function (msg) { $.messager.alert("温馨提示", msg, "info" ...
- 组合数的简单求法(dfs)
组合数的具体应用可以参考这个例子:https://www.cnblogs.com/FengZeng666/p/10496223.html 下面这段代码可以作为求组合数的固定套路或者公式: #inclu ...
- sql语句start with connect by prior语法解析
prior分两种放法: 1 放在子节点端 表示start with 指定的节点作为根节点,按照从上到下的顺序遍历 2 放在父节点端 表示start with指定的节点作为最底层节点,按照从下到上的顺序 ...
- Uniform Generator
Computer simulations often require random numbers. One way to generate pseudo-random numbers is via ...
- linux硬盘挂载-新硬盘挂载和扩容硬盘挂载
这里对当前我实际操作后的两种硬盘挂载进行整理: 第1种是直接添加一块新硬盘,然后进行挂载. 第2种是对硬盘进行扩容后,对扩容后的空间进行分区再进行挂载. [内容为参考网上资料,再加自已实际操作情况进行 ...
- Sublime Text3搭建PHP开发环境
Sublime Text3搭建PHP开发环境 本文主要给大家分享了关于Sublime Text3搭建PHP开发环境 ,感兴趣的小伙伴可以做一下参考 一.Sublime text3安装 到官网http: ...