#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的更多相关文章

  1. [并查集] POJ 1703 Find them, Catch them

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

  2. [并查集] POJ 2236 Wireless Network

    Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 25022   Accepted: 103 ...

  3. 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条臭 ...

  4. [并查集] POJ 1182 食物链

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 66294   Accepted: 19539 Description ...

  5. [并查集] POJ 1611 The Suspects

    The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 35206   Accepted: 17097 De ...

  6. [ An Ac a Day ^_^ ] [kuangbin带你飞]专题五 并查集 POJ 2236 Wireless Network

    题意: 一次地震震坏了所有网点 现在开始修复它们 有N个点 距离为d的网点可以进行通信 O p   代表p点已经修复 S p q 代表询问p q之间是否能够通信 思路: 基础并查集 每次修复一个点重新 ...

  7. 并查集--poj 2492

    Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...

  8. 【转】并查集&MST题集

    转自:http://blog.csdn.net/shahdza/article/details/7779230 [HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基 ...

  9. [POJ 1988] Cube Stacking (带值的并查集)

    题目链接:http://poj.org/problem?id=1988 题目大意:给你N个方块,编号从1到N,有两种操作,第一种是M(x,y),意思是将x所在的堆放到y所在的堆上面. 第二种是C(x) ...

随机推荐

  1. hdu 1800 (map)

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 Flying to the Mars Time Limit: 5000/1000 MS (Java/ ...

  2. Hibernate之N+1问题

    什么是hibernate的N+1问题?先了解这样一个描述: 多个学生可以对应一个老师,所以student(n)---teacher(1).Stu类中有一个属性teacher.在hibernate配置文 ...

  3. DOM 操作XML(CRUD)

    <?xml version="1.0" encoding="UTF-8" standalone="no"?><书架> ...

  4. 堆内存指针的管理类(禁,引数(指针copy),值copy,移)

    //copyp template<typename T> class pm_copyP{ public: pm_copyP(T* _p); pm_copyP(const pm_copyP& ...

  5. bootstrap学习笔记<六>(表单二之按钮)

    按钮(补充) (ps:居中元素可以使用<center></center>标签) 块级按钮(ps:按钮占一整行) <button class="btn btn-p ...

  6. 【Todo】用python进行机器学习数据模拟及逻辑回归实验

    参考了这个网页:http://blog.csdn.net/han_xiaoyang/article/details/49123419 数据用了 https://pan.baidu.com/s/1pKx ...

  7. 5.6 a、b交换

    答案:int max = ((a+b)+abs(a-b))/2;

  8. iOS开发之Xcode 6 国际化

    Xcode6 国际化 (1) 新建一个Single View app模版项目,命名为LocalizationTest 1.建立strings文件,命名为Localization.strings 2.点 ...

  9. 使用js加载器动态加载外部Javascript文件

    原文:http://www.cnblogs.com/xdp-gacl/p/3927417.html 今天在网上找到了一个可以动态加载js文件的js加载器,具体代码如下: JsLoader.js var ...

  10. D3.js 第一个程序 HelloWorld

    一.HTML 是怎么输出 HelloWorld 的 <html> <head> <meta charset="utf-8"> <title ...