poj 1988(并查集)

题意:有30000个木块,编号从1到30000,然后有两种操作M a b,把木块a所在的堆块放到木块b所在的堆块上,操作C a,询问木块a下面有多少个木块。

题解:用一个数组s[i]存木块i所在堆块一共有多少个木块,因为要求木块i下面有多少个木块,所以再添加一个数组dis[i]存木块i到根节点有多少个木块,这样res = s[i]-dis[i]-1。dis数组更新放在寻找根结点递归的后面,因为要先更新父亲的再更新自己的。

#include <stdio.h>
const int N = 30005;
char str[5];
int p, pa[N], s[N], dis[N];

int get_parent(int x) {
    if (x != pa[x]) {
        int f = pa[x];
        pa[x] = get_parent(pa[x]);
        dis[x] += dis[f];
    }
    return pa[x];
}

int main() {
    int t;
    for (int i = 0; i <= N; i++) {
        pa[i] = i;
        s[i] = 1;
        dis[i] = 0;
    }
    scanf("%d", &t);
    while (t--) {
        scanf("%s", str);
        if (str[0] == ‘M‘) {
            int a, b;
            scanf("%d%d", &a, &b);
            int px = get_parent(a);
            int py = get_parent(b);
            pa[py] = px;
            dis[py] = s[px];
            s[px] += s[py];
        }
        else {
            int a;
            scanf("%d", &a);
            int px = get_parent(a);
            printf("%d\n", s[px] - dis[a] - 1);
        }
    }
    return 0;
}
时间: 2024-10-27 18:03:47

poj 1988(并查集)的相关文章

poj 1988 并查集(终于看懂一个了/(ㄒoㄒ)/~~)

题意:有几个stack,初始里面有一个cube.支持两种操作:1.move x y: 将x所在的stack移动到y所在stack的顶部.2.count x:数在x所在stack中,在x之下的cube的个数. 链接:点我 1 /* 2 POJ 1988 3 */ 4 #include <stdio.h> 5 #include <iostream> 6 #include <algorithm> 7 #include <string.h> 8 using name

POJ 2524 并查集

Ubiquitous Religions Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 23580 Accepted: 11609 Description There are so many different religions in the world today that it is difficult to keep track of them all. You are interested in finding o

poj 2513 并查集,Trie(字典树), 欧拉路径

- Colored Sticks POJ - 2513 You are given a bunch of wooden sticks. Each endpoint of each stick is colored with some color. Is it possible to align the sticks in a straight line such that the colors of the endpoints that touch are of the same color?

POJ 2492 并查集扩展(判断同性恋问题)

G - A Bug's Life Time Limit:10000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2492 Appoint description: Description BackgroundProfessor Hopper is researching the sexual behavior of a rare species of bugs. H

poj 1417 并查集+dp

转自:点我 题目:给出p1+p2个人,其中p1个是好人,p2个是坏人.然后有一些关系 ,a说b是好人(坏人).其中没有矛盾的,判断是否有唯一解判断哪些人是好人,哪些人是坏人. 其中比较重要的是,好人总说真话,坏人总说假话.不需要判断矛盾.唯一解 http://poj.org/problem?id=1417 其中好人说真话,坏人说假话这点很重要. 那么如果一个人说另一个人是好人,那么如果这个人是好人,说明 对方确实是好人,如果这个是坏人,说明这句话是假的,对方也是坏人. 如果一个人说另一个人是坏人

POJ 2492 并查集应用的扩展

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

poj 1984 并查集

题目意思是一个图中,只有上下左右四个方向的边.给出这样的一些边, 求任意指定的2个节点之间的距离. 就是看不懂,怎么破 1 /* 2 POJ 1984 3 并查集 4 */ 5 6 #include <stdio.h> 7 #include <string.h> 8 #include <iostream> 9 #include <algorithm> 10 #include <math.h> 11 using namespace std; 12

Wireless Network (poj 2236 并查集)

Language: Default Wireless Network Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 17602   Accepted: 7418 Description An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wireless network wit

源哥每日一题第十八弹 poj 1182 并查集

题目链接:http://poj.org/problem?id=1182 题意:看不懂?退群吧 比平常的并查集加了一个判断集合间关系的操作: 开一个数组记录当前点所在集合的次序(第几个集合)用012表示 比较简单的思路,不过体现了并查集的精妙 #include <iostream> #include <cstring> #include <cstdio> using namespace std; int dad[500005]; int st[50005]; int fi