/*
题目大意:有两个不同的黑帮,开始的时候不清楚每个人是属于哪个的!
执行两个操作
A a, b回答a, b两个人是否在同一帮派,或者不确定
D a, b表示a, b两个人不在同一个帮派 思路:利用并查集将相同帮派的人合并到一起!
a ,b 不在同一个城市,那么 a, 和mark[b]就在同一个城市,
b 和 mark[a]就在同一个城市!
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#define M 100005
using namespace std;
int f[M];
int mark[M];//mark[i]表示 i 与 mark[i]不在同一个黑帮
int rank[M];//为不同的集合的父亲节点记录其孩子机节点个数,在合并集合的时候,将
int n, m; //rank 值小的集合连接到 rank 值大的集合上去! 这样在路径压缩的时候省去不少时间 void init(){ memset(mark, , sizeof(int)*(n+));
memset(rank, , sizeof(int)*(n+));
for(int i=; i<=n; ++i)
f[i]=i;
} int getFather(int x){
return x==f[x] ? x : f[x]=getFather(f[x]);
} void Union(int a, int b){
int fa=getFather(a), fb=getFather(b);
if(fa!=fb){
if(rank[fa]>rank[fb]){
f[fb]=fa;
rank[fa]+=rank[fb]+;
}
else{
f[fa]=fb;
rank[fb]+=rank[fa]+;
}
}
} int main(){
int t;
char cmd[];
scanf("%d", &t);
while(t--){
scanf("%d%d", &n, &m);
init();
while(m--){
int u, v;
scanf("%s%d%d", cmd, &u, &v);
if(cmd[]=='A'){
int fu=getFather(u), fv=getFather(v);
if(fu==fv)
printf("In the same gang.\n");
else if(fu==getFather(mark[v]))
printf("In different gangs.\n");
else
printf("Not sure yet.\n");
}
else{
if(!mark[u] && !mark[v]){
mark[u]=v;
mark[v]=u;
}
else if(!mark[u]){
mark[u]=v;
Union(u, mark[v]);
}
else if(!mark[v]){
mark[v]=u;
Union(v, mark[u]);
}
else {
Union(u, mark[v]);
Union(v, mark[u]);
}
}
}
}
return ;
}

poj1703Find them, Catch them(并查集以及路径压缩)的更多相关文章

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

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

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

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

  3. [HDOJ2818]Building Block(带权并查集,路径压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2818 题意:有N个块,每次有两个操作: M x y表示把x所在的那一堆全部移到y所在的那一堆的下方. ...

  4. [HDOJ3635]Dragon Balls(并查集,路径压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3635 题意:有n个龙珠,n个城市.初始状态第i个龙珠在第i个城市里.接下来有两个操作: T A B:把 ...

  5. 关于并查集的路径压缩(Path Compress)优化

    之前在CSDN看到一篇很受欢迎的讲解并查集的博文,其中自然用到了路径压缩: int pre[1000]; int find(int x){ int root = x; while(pre[root]! ...

  6. BZOJ 3674 可持久化并查集加强版(路径压缩版本)

    /* bzoj 3674: 可持久化并查集加强版 http://www.lydsy.com/JudgeOnline/problem.php?id=3674 用可持久化线段树维护可持久化数组从而实现可持 ...

  7. poj1703 Find them, Catch them 并查集

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

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

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

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

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

随机推荐

  1. 个人阅读作业Week7

    没有银弹 <没有银弹>,Brooks在该论文中,强调真正的银弹并不存在,而所谓的没有银弹则是指没有任何一项技术或方法可以能让软件工程的生产力在十年内提高十倍.文中讨论到了软件工程中主要的两 ...

  2. 基于Axure的快速原型方法

    Axure是一个专业的快速原型设计工具,让负责定义需求和规格.设计功能和界面的专家能够快速创建应用软件或Web网站的线框图.流程图.原型和规格说明文档.作为专业的原型设计工具,它能快速.高效的创建原型 ...

  3. ubuntu安装日文分词软件MeCab及其Python插件

    1.从官网上下载三个包: http://taku910.github.io/mecab/ mecab-0.996 mecab-0.996.tar.gz mecab-ipadic-2.7.0-20070 ...

  4. 选择排序-java

    排序-选择排序 基本思想:在待排序子表中找出最大(小)元素, 并将该元素放在子表的最前(后)面. 平均时间:O(n2) 最好情况:O(n2) 最坏情况:O(n2) 辅助空间:O(1) 稳定性:不稳定 ...

  5. 美帝的emal to message gateway

    Provider Email to SMS Address Format AllTel number@text.wireless.alltel.com AT&T number@txt.att. ...

  6. Security Configuration and Auditing Scripts for Oracle E-Business Suite (文档 ID 2069190.1)

    This document provides the security configuration and auditing scripts for Oracle E-Business Suite. ...

  7. iOS开发-NSOperation与GCD区别

    Mac OS X 10.6及iOS4.0之后导入了可以使全体线程更高效运行,并且使并行处理应用更易开发的架构,GCD(Grand Central  Dispatch),同时引入的还有Run Loop, ...

  8. Golang通过Thrift框架完美实现跨语言调用

    每种语言都有自己最擅长的领域,Golang 最适合的领域就是服务器端程序. 做为服务器端程序,需要考虑性能同时也要考虑与各种语言之间方便的通讯.采用http协议简单,但性能不高.采用TCP通讯,则需要 ...

  9. netfx_NativeCompilation.msi 传说中的 .NET Native 预览版的文件列表

    卷 D 的文件夹 PATH 列表 卷序列号为 000000F6 CE0F:AB46 D:. │ LIST.TXT │ ├─12.0 │ └─Microsoft.Common.targets │ ├─I ...

  10. Snmp协议应用-扫描局域网内打印机

    .h2cls { background: #6fa833 none repeat scroll 0 0 !important; color: #fff; font-family: "微软雅黑 ...