zoukankan      html  css  js  c++  java
  • Cube Stacking

    Cube Stacking
    Time Limit: 2000MS   Memory Limit: 30000K
         
    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
    分析:题意是支持2种操作,1是把包含x的联通块移到y上,2是询问x下面有几个物品;
       带权并查集,p[x]为x的根,cnt[x]为x联通块大小,ret[x]为答案;
       merge(i,j)时,先找到根x,y,p[x]=y,表示x移到y,ret[x]+=cnt[y],更新x答案,同时注意更新cnt[y]+=cnt[x];
       剩下路径压缩即可;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <bitset>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #define rep(i,m,n) for(i=m;i<=n;i++)
    #define mod 1000000007
    #define inf 0x3f3f3f3f
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define sys system("pause")
    const int maxn=1e5+10;
    const int N=5e4+10;
    const int M=N*10*10;
    using namespace std;
    inline ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    inline ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
    inline void umax(ll &p,ll q){if(p<q)p=q;}
    inline void umin(ll &p,ll q){if(p>q)p=q;}
    int n,m,k,t,p[maxn],ret[maxn],cnt[maxn];
    char op[2];
    int find(int x)
    {
        if(x==p[x])return x;
        int y=p[x];
        p[x]=find(p[x]);
        ret[x]+=ret[y];
        return p[x];
    }
    int main()
    {
        int i,j;
        scanf("%d",&n);
        rep(i,1,n)p[i]=i,cnt[i]=1;
        rep(i,1,n)
        {
            scanf("%s%d",op,&j);
            if(op[0]=='M')
            {
                scanf("%d",&k);
                int x=find(j),y=find(k);
                p[x]=y;
                ret[x]+=cnt[y];
                cnt[y]+=cnt[x];
            }
            else
            {
                find(j);
                printf("%d
    ",ret[j]);
            }
        }
        return 0;
    }
  • 相关阅读:
    python的类的继承和多继承
    python的类访问控制、私有属性
    python的面向对象、类方法和静态方法
    python的序列化和反序列化
    python高阶函数、柯里化、装饰器、参数注解
    python函数参数、参数解构、作用域、递归及匿名函数
    python内置函数列表解析、生成器
    python内置函数字典(dict)
    python内置函数集合(set)
    定制自己的打印函数---进阶篇
  • 原文地址:https://www.cnblogs.com/dyzll/p/6408087.html
Copyright © 2011-2022 走看看