[HDU3635]Dragon Balls

题目

传送门

题解

带权并查集板题。

然而我还是不会做

考虑我们输出的东西有三个:根节点编号,并查集大小,换根的次数。

对于前两个,其实就是普通的并查集都可以处理,难点在第三个的处理。

我们想想,换根的次数的实质是什么?

显然,对于节点 \(u\),如果他路径压缩一次,其实就代表了他换了一次根。

那么,对于一个节点,我们只需要维护他路压次数就可以了。

但是对于 \(u\),如果他的父亲 \(fa[u]\) 换根了,他其实是没有被修改过的,那么我们就要在 findSet() 的递归的时候,将他父亲的修改下传到他的身上。

在代码中我使用了一个 temp 变量来记录父节点的修改次数,最后直接将他的修改次数 +=temp 即可。

#include<bits/stdc++.h>
using namespace std;

#define rep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i<=i##_end_;++i)
#define fep(i,__l,__r) for(signed i=(__l),i##_end_=(__r);i>=i##_end_;--i)
#define writc(a,b) fwrit(a),putchar(b)
#define mp(a,b) make_pair(a,b)
#define ft first
#define sd second
#define LL long long
#define ull unsigned long long
#define uint unsigned int
#define pii pair< int,int >
#define Endl putchar('\n')
// #define int long long
// #define int unsigned
// #define int unsigned long long

#define cg (c=getchar())
template<class T>inline void qread(T& x){
    char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    if(f)x=-x;
}
template<class T>inline T qread(const T sample){
    T x=0;char c;bool f=0;
    while(cg<'0'||'9'<c)f|=(c=='-');
    for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
    return f?-x:x;
}
#undef cg
// template<class T,class... Args>inline void qread(T& x,Args&... args){qread(x),qread(args...);}
template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
template<class T>inline T fab(const T x){return x>0?x:-x;}
inline int gcd(const int a,const int b){return b?gcd(b,a%b):a;}
inline void getInv(int inv[],const int lim,const int MOD){
    inv[0]=inv[1]=1;for(int i=2;i<=lim;++i)inv[i]=1ll*inv[MOD%i]*(MOD-MOD/i)%MOD;
}
template<class T>void fwrit(const T x){//just short,int and long long
    if(x<0)return (void)(putchar('-'),fwrit(-x));
    if(x>9)fwrit(x/10);
    putchar(x%10^48);
}
inline LL mulMod(const LL a,const LL b,const LL mod){//long long multiplie_mod
    return ((a*b-(LL)((long double)a/mod*b+1e-8)*mod)%mod+mod)%mod;
}

const int MAXN=10000;

int fa[MAXN+5],siz[MAXN+5],cnt[MAXN+5];

int T,n,m;

inline void makeSet(){
    rep(i,1,n)fa[i]=i,siz[i]=1,cnt[i]=0;
}

int temp;//奇怪的中间变量

int findSet(const int u){//找 u 的根
    if(u==fa[u]){
        temp=0;
        return u;
    }fa[u]=findSet(fa[u]);
    cnt[u]+=temp;
    temp=cnt[u];
    return fa[u];
}

signed main(){
    // cin>>T;
    T=qread(1);
    char opt;int x,y;
    rep(cas,1,T){printf("Case %d:\n",cas);
        // cin>>n>>m;
        n=qread(1),m=qread(1);
        makeSet();
        while(m--){
            // cin>>opt>>x;
            scanf("%c",&opt);
            x=qread(1);
            if(opt=='T'){/*cin>>y;*/
                y=qread(1);
                x=findSet(x),y=findSet(y);
                fa[x]=y;
                siz[y]+=siz[x];
                ++cnt[x];
            }else{
                y=findSet(x);
                printf("%d %d %d\n",y,siz[y],cnt[x]);
            }
        }
    }
	return 0;
}

唔,似乎这道题不能用输入输出流,不然会 TLE

posted @ 2020-05-19 16:03  Arextre  阅读(102)  评论(0编辑  收藏  举报