poj1703Find them, Catch them(并查集以及路径压缩)
/*
题目大意:有两个不同的黑帮,开始的时候不清楚每个人是属于哪个的!
执行两个操作
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(并查集以及路径压缩)的更多相关文章
- 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个 ...
- [HDOJ2818]Building Block(带权并查集,路径压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2818 题意:有N个块,每次有两个操作: M x y表示把x所在的那一堆全部移到y所在的那一堆的下方. ...
- [HDOJ3635]Dragon Balls(并查集,路径压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3635 题意:有n个龙珠,n个城市.初始状态第i个龙珠在第i个城市里.接下来有两个操作: T A B:把 ...
- 关于并查集的路径压缩(Path Compress)优化
之前在CSDN看到一篇很受欢迎的讲解并查集的博文,其中自然用到了路径压缩: int pre[1000]; int find(int x){ int root = x; while(pre[root]! ...
- BZOJ 3674 可持久化并查集加强版(路径压缩版本)
/* bzoj 3674: 可持久化并查集加强版 http://www.lydsy.com/JudgeOnline/problem.php?id=3674 用可持久化线段树维护可持久化数组从而实现可持 ...
- 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个集合了.并且 ...
- POJ 2236 Wireless Network ||POJ 1703 Find them, Catch them 并查集
POJ 2236 Wireless Network http://poj.org/problem?id=2236 题目大意: 给你N台损坏的电脑坐标,这些电脑只能与不超过距离d的电脑通信,但如果x和y ...
随机推荐
- Android PullToZoomListView实现放大回弹效果
另外一个相同项目的地址https://github.com/Frank-Zhu/PullZoomView 转自http://blog.csdn.net/wangjinyu501/article/det ...
- select 取的是session里面的值时
原来是写了一个select标签,然后用js循环取出来,发现问题是本来嵌在页面右边的页面整个弹出来, 后来改成html:optionsCollection就好了: 效果图:
- Connecting sql server2008 through C#
本实例只在于将通过ASP.NET上的控件(如button.textbox等)结合C#中类的应用,将自己需要的数据存入当地的SQL SERVER数据库中 首先我们在项目中需要建立一个ASP.NET We ...
- Individual Project - Word frequency program - Multi Thread And Optimization
作业说明详见:http://www.cnblogs.com/jiel/p/3978727.html 一.开始写代码前的规划: 1.尝试用C#来写,之前没有学过C#,所以打算先花1天的时间学习C# 2. ...
- dataview将excel表格的数据导出成txt文件
有时候需要处理大量的数据,且这些数据又存在于excel表格内,在平时的时候,我是非常喜欢这样的数据的,因为只要是excel表格内的数据,处理起来的方法就很方便.也可能我平时遇见的数据总是以一种杂乱无章 ...
- 积累一点ctf需要掌握的常见脚本知识
1.暴力破解压缩包. 2.利用像素点还原图片. from PIL import Image import re if __name__ == '__main__': x = 887 //将像素点个数进 ...
- Go+sublime text3的环境搭建
1.安装Go语言. .msi下载地址:http://download.csdn.net/detail/u014075041/9555543 根据提示安装成功! 在命令行中执行 go env 有提示 ...
- 新装ubuntu12.04需要敲的命令集合
1.sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup sudo gedit /etc/apt/sources.list copy: ...
- RESTful接口设计原则/最佳实践(学习笔记)
RESTful接口设计原则/最佳实践(学习笔记) 原文地址:http://www.vinaysahni.com/best-practices-for-a-pragmatic-restful-api 1 ...
- Online Judge中基本的输入输出
一.利用c语言处理输入和输出 1.输入: 在C语言中输入中stdin指针表示标准的输入,scanf默认读取的就是stdin指向的输入,在acm中我们可能需要不断的测试,最好将stdin重定向到某一个文 ...