zoukankan      html  css  js  c++  java
  • POJ 1988 Cube Stacking

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

    Source

    http://blog.csdn.net/popopopolo/article/details/6608811

    思路:可以用并查集来解. p[x]表示x的父节点, -p[root]的值为该叠方块的数量, below[x]表示x方块下面的方块数. 当进行M操作时, 修改px(即x所在堆的根节点)的below值, 即below[px]+=-p[py], 表示它的下面增加了-p[py]个方块, 接着修改该新的方块堆的方块的数量值, 即p[py]+=p[px], 最后使X所在的堆的根节点指向新的根节点py. 这里有个很巧妙的地方就是, 方块堆Union的时候只修改相关的根节点的below值, 而暂时不理会其它节点的below值. 这个修改的工作交由Find()操作完成, 由于查操作的递归性质, 在递归返回的过程中会自根而下地修改路过的需要更新信息的节点的信息. 这样的话在进行查操作的时候就必须先进行一次Find(), 这样才能得到正确的节点信息. 这一过程非常巧妙, 需要细细体会方能领悟其要领.

    //这题真的好神奇,这样的方法我还是第一次见到、呵呵;

    //思路,把放下面的堆并到放上面的堆,f数组记录父亲节点,bl数组记录i下面个方块个数,h数组记录合并后堆的个数

    //合并时 bl[x]=h[y];h[y]+=h[x];f[x]=y;

    //计算每个节点下面节点个数个在find_r()函数中进行、这个就是巧妙所在、很好的方法

    //这题的并查集算法感觉真是代表了一种经典呀、呵呵

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #define N 30003
    using namespace std;
    int f[N],h[N],bl[N];
    int r;
    int find_r(int x)
    {
        if(x!=f[x])
        {
            f[x]=find_r(f[x]);//开始把这写到最后了,思路没清楚呀
              bl[x]+=r;   //边压缩路径、边计算bl[i]值
              r=bl[x];
            return f[x];
        }
        return x;
    }
    void union_s(int x,int y)
    {
         r=0;x=find_r(x);
         r=0;y=find_r(y);
         bl[x]=h[y];
         f[x]=y;
         h[y]+=h[x];
    }
    int main()
    {
        int P;
        char c;
        int a,b;
        while(scanf("%d",&P)!=EOF)
        {
            for(int i=1;i<=30000;i++)
             f[i]=i,bl[i]=0,h[i]=1;
           while(P--)
            {
                getchar();
                scanf("%c",&c);
                switch(c)
                {
                    case 'M':scanf("%d%d",&a,&b);union_s(a,b);break;
                    case 'C':scanf("%d",&a);r=0;find_r(a);printf("%d\n",bl[a]);break;
                }
            }

        }
        return 0;
    }


    //时间久了都快忘记了、复习下
    //up[i]记录的是在以i的父节点为根时,i上面的立方体个数,(其中包含了他的父节点,不是根节点,这个需要在纸上画出关系可以清晰看到这点,方便在压缩路径时的统计)
    //这次是把在下面的立方体归类到在上面的立方体,total[i]代表以i为根的集合的总元素个数
    #include <iostream> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm> #define N 30001 using namespace std; int P; int F[N],total[N],up[N]; int sum; int Find(int x) { if(x!=F[x]) { F[x]=Find(F[x]); int temp=up[x]; up[x]+=sum; sum+=temp; return F[x]; } return x; } int main() { int x,y; char op; while(scanf("%d",&P)!=EOF) { for(int i=1;i<N;i++) { F[i]=i; total[i]=1; up[i]=0; } while(P--) { getchar(); scanf("%c",&op); if(op=='M') { scanf("%d %d",&x,&y); sum=0; x=Find(x); sum=0; y=Find(y); F[y]=x; up[y]=total[x]; total[x]+=total[y]; } else { scanf("%d",&x); y=x; sum=0; x=Find(x); printf("%d\n",total[x]-up[y]-1); } } } return 0; }
  • 相关阅读:
    使用cwRsync实现windows下文件定时同步【转】
    asp.net负载均衡方案[转]
    SQL Server复制入门(一)----复制简介【转】
    使用ServiceStackRedis链接Redis简介 [转]
    Sqlserver事务发布实现数据同步
    C#MongoDB 分页查询的方法及性能
    C#基础-Func,Action
    OpenERP 的XML-RPC的轻度体验+many2many,one2many,many2one创建方式
    使用xml-rpc调试openerp模块中的函数
    Odoo(OpenERP)开发实践:通过XML-RPC接口访问Odoo数据库
  • 原文地址:https://www.cnblogs.com/372465774y/p/2587642.html
Copyright © 2011-2022 走看看