Cube Stacking POJ1988 【并查集的应用】

http://poj.org/problem?id=1988

Description

Farmer John and Betsy are playing a game with N (1 <= N <= 30,000)identical cubes labeled 1 through N. They start with N stacks, each containing a single cube. Farmer John asks Betsy to perform P (1<= P <= 100,000) operation. There are two types of operations:

moves and counts.

* In a move operation, Farmer John asks Bessie to move the stack containing cube X on top of the stack containing cube Y.

* In a count operation, Farmer John asks Bessie to count the number of cubes on the stack with cube X that are under the cube X and report that value.

Write a program that can verify the results of the game.

Input

* Line 1: A single integer, P

* Lines 2..P+1: Each of these lines describes a legal operation. Line 2 describes the first operation, etc. Each line begins with a ‘M‘ for a move operation or a ‘C‘ for a count operation. For move operations, the line also contains two integers: X and Y.For
count operations, the line also contains a single integer: X.

Note that the value for N does not appear in the input file. No move operation will request a move a stack onto itself.

Output

Print the output from each of the count operations in the same order as the input file.

Sample Input

6
M 1 6
C 1
M 2 4
M 2 6
C 3
C 4

Sample Output

1
0
2

求出任何一个节点的下属的个数

/*
带权并查集,一堆中最顶上的方块作为父节点,用dis[X] 统计X到父亲节点的距离,num[fa[X]]表示团的大小,两者相减即为答案
*/
#include <stdio.h>
#include <string.h>
#define N 30000+100//结点个数
int num[N];//记录这个团队的人数
int dis[N];//记录有个点到根节点的距离
int per[N];//表示父节点

void init()
{
    for(int i=0;i<N;++i)
    {
        num[i]=1;
        dis[i]=0;
        per[i]=i;
    }
}

int find(int x)
{
    if(x==per[x]) return x;
    int t=per[x];
    per[x]=find(per[x]);
    dis[x]+=dis[t];//当查找一个数时,
    return per[x];
}

void join(int x,int y)
{
    int fx=find(x);
    int fy=find(y);
    if(fx!=fy)
    {
        per[fy]=fx;
        dis[fy]=num[fx];
        num[fx]+=num[fy];
    }
}
int main()
{
    int n;
    int a,b;
    char ch;
    while(~scanf("%d",&n))
    {
        init();
        while(n--)
        {
            getchar();
            scanf("%c",&ch);
            if(ch=='M')
            {
                scanf("%d%d",&a,&b);
                join(a,b);
            }
            else if(ch=='C')
            {
                scanf("%d",&a);
                int x=find(a);
                printf("%d\n",num[x]-dis[a]-1);
            }
        }
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-25 10:46:42

Cube Stacking POJ1988 【并查集的应用】的相关文章

poj1988 Cube Stacking(并查集

题目地址:http://poj.org/problem?id=1988 题意:共n个数,p个操作.输入p.有两个操作M和C.M x y表示把x所在的栈放到y所在的栈上(比如M 2 6:[2 4]放到[1 6]上为[2 4 1 6]),C x为输出x下面有几个数. 思路:并查集每个集合以栈最下面的数为根,维护两个数组num[x]表示x所在集合节点总数,count[x]表示x下方节点个数.每次查找压缩路径的时候更新count(换父节点的时候每轮都把父节点的count加给儿子,就可以一直更新到x所在栈

POJ 1988 Cube Stacking(并查集+路径压缩)

题目链接:id=1988">POJ 1988 Cube Stacking 并查集的题目 [题目大意] 有n个元素,開始每一个元素自己 一栈.有两种操作,将含有元素x的栈放在含有y的栈的顶端,合并为一个栈. 另外一种操作是询问含有x元素以下有多少个元素. 用sum数组储存每一个栈中的元素个数.每次合并的时候将sum加到 父亲节点.也就是每一个栈的最底部. 用under数组储存当前节点以下有多少元素.每次合并的时候,就能够将顶端元素的under赋值为父节点也就是栈最底部的sum. void U

【POJ-1988】Cube Stacking(并查集)

//===================================== // KinderRiven POJ 1899 //===================================== #include<cstdio> #include<cstring> using namespace std; const int maxn = 33333; const int INF = 30000; int fa[maxn]; //父亲结点的编号 int ret[maxn

POJ 1988 Cube Stacking (种类并查集)

题目地址:POJ 1988 这道题的查找合并的方法都能想的到,就是一点没想到,我一直天真的以为查询的时候,输入后能马上输出,这样的话在合并的时候就要所有的结点值都要算出来,但是经过路径压缩之后,没办法全部都处理到,如果不压缩妥妥的TLE..于是看了看网上的题解.才发现自己是多么的天真(ben,四声)..在查询的时候只要找一次跟就可以了..这样不需查询的也就没必要处理出来.反而更省时. 这题的基本思路是很好想的.另开两个数组,一个记录以此节点为根的子节点的数目(这样合并的时候只需要加另一个根的数目

POJ1988 并查集的使用

Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 21157   Accepted: 7395 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

poj1988Cube Stacking(并查集+路径压缩)

本题依然是并查集的应用,与上一个监狱的问题不同,本题计算的是距离,通过记录通往自己父节点距离的相对距离,与父节点到根节点(代表元素)的距离依次相加 路径压缩时每次都要更新节点. #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; const int maxn=30001; int p; struct { int fat

poj1988 简单并查集

B - 叠叠乐 Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:30000KB     64bit IO Format:%lld & %llu Submit Status Description Input Output Sample Input Sample Output Hint Description Farmer John and Betsy are playing a game with

加权并查集——(POJ1988)Cube Stacking

Cube Stacking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 25647   Accepted: 8975 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

POJ1988 Cube Stacking(并查集)

题目链接:http://poj.org/problem?id=1988 题意:有n个元素,开始每个元素各自在一个栈中,有两种操作,将含有元素x的栈放在含有y的栈的顶端,合并为一个栈. 第二种操作是询问含有x元素下面有多少个元素. 思路: 并查集,把每一堆看作一个栈,堆的下方看作栈顶.因为当我们知道栈中元素的总数,和某元素到“栈顶”的距离, 我们就能知道这个元素下面有多少元素.合并操作的时候,始终使用在下面栈的根来做合并之后的根,这样也就达到了栈中的根是栈中的“栈顶”元素的效果,我们只需在每个“栈