并查集 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) ...
随机推荐
- hdu 栈题1022&1702
http://acm.hdu.edu.cn/showproblem.php?pid=1022 http://blog.csdn.net/swm8023/article/details/6902426此 ...
- Codeforces Round #382 (Div. 2) D. Taxes 歌德巴赫猜想
题目链接:Taxes D. Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- hiho #1050 : 树中的最长路 树的直径
#1050 : 树中的最长路 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到,小Ho得到了一棵二叉树玩具,这个玩具是由小球和木棍连接起来的,而在拆拼它的过程中, ...
- Windows Internals学习笔记(三)Procdump的使用
参考资料: 1. 下载地址 2. 使用示例
- jQuery调用AJAX异步详解[转]
AJAX 全称 Asynchronous JavaScript and XML(异步的 JavaScript 和 XML).它并非一种新的技术,而是以下几种原有技术的结合体. 1) 使用CSS和X ...
- SAS Annotated Output GLM
SAS Annotated Output GLM 在使用SAS过程中,proc glm步输出离差平方和有4种算法,分别是SS1 SS2 SS3 SS4 下面文章介绍了其中SS3的具体计算步骤和例子 ...
- 流媒体基础实践之——RTMP直播推流
一.RTMP推流:用户可将RTMP视频流推送到阿麦提供的打流地址.地址格式类似于: rtmp://livepush.myqcloud.com/live 现在可以支持哪些直播源?和那些直播软件?推流参数 ...
- poj2208Pyramids(四面体面积--公式)
链接 一公式题.. 证明讲解参照http://www.cnblogs.com/dgsrz/articles/2590309.html 注意对棱 顺序 #include <iostream> ...
- 构件工具Maven----坐标、依赖、仓库、生命周期的简单学习
这篇文章对Maven中几个比较重要的概念坐标.依赖.仓库.生命周期做一个简单的介绍. 1.关于Maven坐标 用来区别Maven世界中任何一个构件,Maven坐标的元素包括groupId.artifa ...
- linux 命令集
cat chattr chgrp c hmod chown cksum cmp diff diffstat file find git gitview indent cut ln less ...