并查集 POJ 1988
#include <cstdio>
#define N 30010
int pa[N]; //parent
int siz_tree[N]; //size of tree
int d[N]; //dist between node and root
int Find(int x)
{
if(pa[x] == x) return x;
int t = pa[x];
pa[x] = Find(pa[x]);
d[x] += d[t];
return pa[x];
}
void Union(int x, int y)
{
x = Find(x), y = Find(y);
pa[x] = y;
d[x] += siz_tree[y];
siz_tree[y] += siz_tree[x];
siz_tree[x] = 0;
}
int main()
{
int p;
while(~scanf("%d", &p))
{
for(int i = 0; i < N; i++) pa[i] = i, siz_tree[i] = 1, d[i] = 0; char c;
int x, y; for(int i = 0; i < p; i++) {
while(1) {
c = getchar();
if(c == 'M' || c == 'C') break;
}
if(c == 'M') {
scanf("%d%d", &x, &y);
Union(x, y);
}
else {
scanf("%d", &x);
Find(x);
printf("%d\n", d[x]);
}
}
}
return 0;
}
并查集 POJ 1988的更多相关文章
- [并查集] POJ 1703 Find them, Catch them
Find them, Catch them Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 43132 Accepted: ...
- [并查集] POJ 2236 Wireless Network
Wireless Network Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 25022 Accepted: 103 ...
- hdu - 1829 A Bug's Life (并查集)&&poj - 2492 A Bug's Life && poj 1703 Find them, Catch them
http://acm.hdu.edu.cn/showproblem.php?pid=1829 http://poj.org/problem?id=2492 臭虫有两种性别,并且只有异性相吸,给定n条臭 ...
- [并查集] POJ 1182 食物链
食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 66294 Accepted: 19539 Description ...
- [并查集] POJ 1611 The Suspects
The Suspects Time Limit: 1000MS Memory Limit: 20000K Total Submissions: 35206 Accepted: 17097 De ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题五 并查集 POJ 2236 Wireless Network
题意: 一次地震震坏了所有网点 现在开始修复它们 有N个点 距离为d的网点可以进行通信 O p 代表p点已经修复 S p q 代表询问p q之间是否能够通信 思路: 基础并查集 每次修复一个点重新 ...
- 并查集--poj 2492
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- 【转】并查集&MST题集
转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...
- [POJ 1988] Cube Stacking (带值的并查集)
题目链接:http://poj.org/problem?id=1988 题目大意:给你N个方块,编号从1到N,有两种操作,第一种是M(x,y),意思是将x所在的堆放到y所在的堆上面. 第二种是C(x) ...
随机推荐
- Mybatis+SpringMVC+Spring整合
1,先添加spring支持: applicationContext.xml 配在WEBINF下,四个命名空间:aop,context,tx,p 配Listener:ContextLoaderList ...
- 通知(NSNotificationCenter)
// 监听加载更多的通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadMoreDeals ...
- Object-C : Block的实现方式
摘自:http://www.cnblogs.com/GarveyCalvin/p/4204167.html> Date : 2015-12-4 前言:我们可以把Block当作一个闭包函数,它可以 ...
- sqlserver前面加N解释
From : http://lzde360.blog.163.com/blog/static/6780720820111026112033917/ 加上 N 代表存入数据库时以 Unicode 格式存 ...
- Win7路由器设置过程
随着应用win7系统的人越来越多,对于这个系统的应用就更多了,其中大家最关注的就是这个系统和路由器上网的问题.下面,我们就来讲解一下win7系统的路由器的设置过程. 首先打开浏览器,在地址栏输入192 ...
- img_jquerydim
- E2 结帐方案如何理解?
E2 结帐方案如何理解? 此文转载自宇然软件官方网站:http://www.fsyuran.com
- mysql概要(八)视图
1.视图使用时,以表的方式使用 视图修改 alter view 视图名 as select ...; 1.1创建视图: 2.视图的好处: 通过id取模存放四个表中,然后通过视图把表合成一张表: 3.视 ...
- 操作符 Thinking in Java 第三章
3.1 更简单的打印语句 3.2 使用Java操作符 3.3 优先级 *int类型+String类型 直接转换为String类型 3.4 赋值 1. *引用=引用 两个引用指向同一个对象,所以操作 ...
- [转载]VFS—Kernel Space & User Space
在了解虚拟文件系统之前 , 需要先了解 Kernel Space 和 User Space 的区别 . 二者的差别在于内存使用上安全机制的差异 . kernel 执行时会占据一段系统的内存空间 , 这 ...