zoukankan      html  css  js  c++  java
  • bzoj3376: [Usaco2004 Open]Cube Stacking 方块游戏

    传送门

    一个带权并查集的模板题。因为是把一堆堆到另一堆上面,除了fa(堆底,代表元)外再记录一下每个代表元的堆顶。

     1 //Achen
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cstring>
     5 #include<cstdlib>
     6 #include<vector>
     7 #include<cstdio>
     8 #include<queue>
     9 #include<cmath>
    10 #include<set>
    11 #include<map>
    12 #define Formylove return 0
    13 #define For(i,a,b) for(int i=(a);i<=(b);i++)
    14 #define Rep(i,a,b) for(int i=(a);i>=(b);i--)
    15 const int N=30007;
    16 typedef long long LL;
    17 typedef double db;
    18 using namespace std;
    19 int q,fa[N],ed[N],fdis[N];
    20 char op[10];
    21 
    22 template<typename T>void read(T &x)  {
    23     char ch=getchar(); x=0; T f=1;
    24     while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    25     if(ch=='-') f=-1,ch=getchar();
    26     for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
    27 }
    28 
    29 int find(int x) { 
    30     if(x==fa[x]) return x;
    31     int y=find(fa[x]); 
    32     fdis[x]+=fdis[fa[x]];
    33     fa[x]=y; return y;
    34 } 
    35 
    36 int main() {
    37 #ifdef ANS
    38     freopen(".in","r",stdin);
    39     freopen(".out","w",stdout);
    40 #endif
    41     For(i,1,N-7) fa[i]=i,ed[i]=i,fdis[i]=0;
    42     read(q);
    43     while(q--) {
    44         int x,y;
    45         scanf("%s",op);
    46         if(op[0]=='M') {
    47             read(x); read(y);
    48             x=find(x); y=find(y);
    49             fa[x]=ed[y]; 
    50             ed[y]=ed[x];
    51             fdis[x]=1;
    52         }
    53         else {
    54             read(x); find(x);
    55             printf("%d
    ",fdis[x]);
    56         }
    57     }
    58     Formylove;
    59 }
    60 /*
    61 6
    62 M 1 6
    63 M 2 4
    64 M 2 6
    65 C 4
    66 */
    View Code
  • 相关阅读:
    Parrot虚拟机
    JAVA数据结构二叉排序树
    mysql基本操作
    ruby数组操作
    JAVA核心技术之球体碰撞多线程版
    JAVA数据结构选择排序
    JAVA的线程让步
    Perl 与数学:一份快速参考
    科学计算软件包python(x,y)简介
    JAVA数据结构解析数学表达式
  • 原文地址:https://www.cnblogs.com/Achenchen/p/9540215.html
Copyright © 2011-2022 走看看