并查集 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;
}

  

时间: 2024-08-09 14:36:46

并查集 POJ 1988的相关文章

并查集 POJ 1988 Cube Stacking

题目传送门:    -------->这里<---------- 题目大意: 有标记1-N的方块,初始时一个砖块是一堆.然后进行以下操作 M X Y : 将 X 砖块所在堆 叠到 Y 砖块所在堆上面: C X :数在 X 砖块所在堆中 叠在X砖块下的砖块个数: 1<=N<=30000; 思路: 并查集,每一个砖块堆是一个集合.将每一堆的最上面一块砖设为根,合并时更新 Y 堆的根的 father 和 val (在Y上面的方块数) . 要达到这些目的,需要一个all数组,来记录每一堆里

(带权并查集)poj 1988

Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 20392   Accepted: 7148 Case Time Limit: 1000MS Description Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes labeled 1 through N. They start w

Catenyms+欧拉回路/欧拉路+并查集+POJ

Catenyms Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9617   Accepted: 2524 Description A catenym is a pair of words separated by a period such that the last letter of the first word is the same as the last letter of the second. For e

[并查集] POJ 2236 Wireless Network

Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 25022   Accepted: 10399 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network with the lap compute

[并查集] POJ 1182 食物链

食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 66294   Accepted: 19539 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人用两种说法对这N个动物所构成的食物链关系进行描述: 第一种说法是"1 X Y",表示X和Y是同

[并查集] POJ 1611 The Suspects

The Suspects Time Limit: 1000MS   Memory Limit: 20000K Total Submissions: 35206   Accepted: 17097 Description Severe acute respiratory syndrome (SARS), an atypical pneumonia of unknown aetiology, was recognized as a global threat in mid-March 2003. T

并查集 POJ 1703 Find them, Catch them

题目传送门 题意:有两个黑帮集团,给出一些两个小弟属于不同的黑帮,询问两个小弟是否关系能确定 分析:首先直接弄两个集合是不好的,正确的做法是类似食物链的做法,关系已确定不属于同一个帮派的x 和 y 使得x 和 y + n属于同一个集合,y 和 x + n属于同一个集合,那么最后只要判断x 和 y 是否在同一个集合就可以了 收获:关系的连通问题 代码: /************************************************ * Author :Running_Time

(并查集) poj 2492

A Bug's Life Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 29940   Accepted: 9802 Description Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes that they feature two different genders

(并查集) poj 1703

Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33284   Accepted: 10258 Description The police office in Tadu City decides to say ends to the chaos, as launch actions to root up the TWO gangs in the city, Gang Drago