zoukankan      html  css  js  c++  java
  • 【BZOJ 3376】[Usaco2004 Open]Cube Stacking 方块游戏 带权并查集

    这道题一开始以为是平衡树结果发现复杂度过不去,然后发现我们一直合并而且只是记录到最低的距离,那么就是带权并查集了,带权并查集的权一般是到根的距离,因为不算根要好打,不过还有一些其他的,具体的具体打。

    #include <cstdio>
    #include <cstring>
    const int N=30050;
    int h[N],f[N],size[N];
    char s[2];
    inline int find(int x){
        if(f[x]==x)return x;
        int temp=f[x];f[x]=find(f[x]);
        if(temp!=f[x])h[x]+=h[temp];
        return f[x];
    }
    inline void Unit(int x,int y){
        h[find(x)]=size[find(y)];
        size[find(y)]+=size[find(x)];
        f[find(x)]=find(y);
    }
    int main(){
        int T,x,y;scanf("%d",&T);
        for(int i=0;i<N;i++)f[i]=i,size[i]=1,h[i]=0;
        while(T--){
            scanf("%s",s);
            if(s[0]=='M')scanf("%d%d",&x,&y),Unit(x,y);
            else scanf("%d",&x),find(x),printf("%d
    ",h[x]);
        }
    }
  • 相关阅读:
    Node.js NPM 包(Package)
    Node.js NPM 作用
    Node.js NPM 介绍
    Node.js NPM 教程
    Node.js NPM 教程
    Node.js 发送Email
    Node.js 上传文件
    Node.js 事件
    Node.js NPM
    Node.js 文件系统模块
  • 原文地址:https://www.cnblogs.com/TSHugh/p/7560784.html
Copyright © 2011-2022 走看看