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(并查集)的更多相关文章

  1. poj1703 Find them, Catch them 并查集

    poj(1703) Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26992   ...

  2. 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 ...

  3. POJ 1703 Find them, catch them (并查集)

    题目:Find them,Catch them 刚开始以为是最基本的并查集,无限超时. 这个特殊之处,就是可能有多个集合. 比如输入D 1 2  D 3 4 D 5 6...这就至少有3个集合了.并且 ...

  4. poj1703--Find them, Catch them(并查集应用)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32073   Accepted: ...

  5. POJ1703-Find them, Catch them 并查集构造

                                             Find them, Catch them 好久没有做并查集的题,竟然快把并查集忘完了. 题意:大致是有两个监狱,n个 ...

  6. POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集

    POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...

  7. POJ 1703 Find them, Catch them 并查集的应用

    题意:城市中有两个帮派,输入中有情报和询问.情报会告知哪两个人是对立帮派中的人.询问会问具体某两个人的关系. 思路:并查集的应用.首先,将每一个情报中的两人加入并查集,在询问时先判断一下两人是否在一个 ...

  8. poj1703_Find them, Catch them_并查集

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42451   Accepted: ...

  9. poj.1703.Find them, Catch them(并查集)

    Find them, Catch them Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I6 ...

  10. POJ 1703 Find them, Catch them(并查集高级应用)

    手动博客搬家:本文发表于20170805 21:25:49, 原地址https://blog.csdn.net/suncongbo/article/details/76735893 URL: http ...

随机推荐

  1. COOKIE和SESSION有什么区别?

    1,位置--session 在服务器端,cookie 在客户端(浏览器)2,形式--session 默认被存在在服务器的一个文件里(session 可以放在 文件.数据库.或内存中都可以),cooki ...

  2. 2016-06-19 exshop第5天

    昨天对grails3和spring-security进行了全面的调研并进行了试验,试用下来发现grails3的启动速度.代码修改后刷新速度.内存占用以及架构的设计上更加合理,asset-pipelin ...

  3. zabbix通过自动发现tomcat应用端口监控连接数

    192.168.10.98上 netstat -anp | wc -l netstat -anp|grep 8094 | grep ESTABLISHED | wc -l netstat -anp|g ...

  4. Codeforces 725E Too Much Money (看题解)

    Too Much Money 最关键的一点就是这个贪心可以在sqrt(n)级别算出答案. 因为最多有sqrt(n)个不同的数值加入. 我们可以发现最优肯定加入一个. 然后维护一个当前可以取的最大值, ...

  5. openstack2 kvm

    一.kvm安装 1.首先虚拟机的话需要打开虚拟化功能,服务器的话需要在bios中设置 2.安装kvm用户态管理工具qemu-kvm 和 用来管理kvm虚拟机的插件libvirt和创建虚拟机用的virt ...

  6. Codeforces 986D Perfect Encoding FFT 分治 高精度

    原文链接https://www.cnblogs.com/zhouzhendong/p/9161557.html 题目传送门 - Codeforces 986D 题意 给定一个数 $n(n\leq 10 ...

  7. Django 1.11.7学习,配置MySQL数据库(python3.5)

    1.链接MySQL数据库 a.导入pymysql包 import pymysql pymysql.install_as_MySQLdb() b.改变DATABASES参数 ENGINE : 指定数据库 ...

  8. Noj - 在线强化训练4

    状态 题号 竞赛题号 标题 × 1092 A 童年的回忆——计算24 × 1145 B 求图像的周长 × 1144 C 农场灌溉问题 × 1202 D 数独游戏 × 1243 E 循环赛日程表 × 1 ...

  9. GIT结合android studio使用总结

    使用GIT前请阅读(有git基础可略过) git指引 :http://www.bootcss.com/p/git-guide/ 一. 下载git  http://git-scm.com/downloa ...

  10. 【LeetCode算法-13】Roman to Integer

    LeetCode第13题 Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symb ...