zoukankan      html  css  js  c++  java
  • 并查集+路径压缩(poj1988)

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

    Cube Stacking
    Time Limit: 2000MS   Memory Limit: 30000K
    Total Submissions: 19122   Accepted: 6664
    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 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
    题意:可以把题目中的意思抽象为栈集合的合并,题目中有两个操作

    M 把包含x的栈放在包含y的栈的上面;

    C统计包含x的栈中x下面有多少个元素;

    分析:此题是并查集路径压缩的典型题目,与前面的种类并查集还有些不同;题目中用到f[],num[],dis[]三个数组,f数组记录i的父节点,num数组记录以i为根的集合有多少个元素,dis记录i到根节点的距离,那么i下面的元素就是x=finde(i);num[x]-dis[i]-1个元素;

    程序:

    #include"stdio.h"
    #include"string.h"
    #include"iostream"
    #include"map"
    #include"string"
    #include"queue"
    #include"stdlib.h"
    #include"math.h"
    #define M 30009
    #define eps 1e-10
    #define inf 1000000000
    #define mod 2333333
    using namespace std;
    int f[M],num[M],dis[M];
    int finde(int x)
    {
        if(x!=f[x])
        {
            int t=f[x];
            f[x]=finde(f[x]);
            dis[x]+=dis[t];
        }
        return f[x];
    }
    void make(int a,int b)
    {
        int x=finde(a);
        int y=finde(b);
        if(x!=y)
        {
            f[y]=x;
            dis[y]+=num[x];
            num[x]+=num[y];
        }
    }
    int main()
    {
        int n,i,a,b;
        char str[3];
        while(scanf("%d",&n)!=-1)
        {
            for(i=1;i<=M+1;i++)
            {
                f[i]=i;
                num[i]=1;
                dis[i]=0;
            }
            while(n--)
            {
                scanf("%s",str);
                if(str[0]=='M')
                {
                    scanf("%d%d",&a,&b);
                    make(a,b);
                }
                else
                {
                    scanf("%d",&a);
                    int x=finde(a);
                    printf("%d
    ",num[x]-dis[a]-1);
                }
            }
        }
    }
    


  • 相关阅读:
    C数据结构2.1-线性表抽象数据类型
    转载的内容
    转载springboot的内容
    jQuery中的load()Failed to load resource: the server responded with a status of 404 Maven框架遇到的问题
    java代码发送邮箱源代码
    Error:(1, 10) java: 需要class, interface或enum的错误
    性能测试系列五 压测常见的关注指标以及监控分析工具
    面试官常考的Selenium Web自动化面试题总结(上篇)
    性能测试系列四 压测指标的来源
    性能测试系列三 压测方式简单总结
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348172.html
Copyright © 2011-2022 走看看