1 条题解

  • 0
    @ 2023-2-18 16:51:00

    这题。。。6 第一次写的超大时,这个是可以的

    #include<bits/stdc++.h>
    using namespace std;
    #define maxn 30005
    int n,f[maxn],pre[maxn],back[maxn];
    struct node{
        int x,y;
        char w;
    }b[maxn];
    int find(int x){
        if(f[x]==x) return x;
        int p=find(f[x]);
        pre[x]+=pre[f[x]];
        f[x]=p;
        return f[x];
    }
    void unit(int x,int y){
        int fx=find(x),fy=find(y);
        f[fy]=fx;
        pre[fy]=back[fx];
        back[fx]=back[fx]+back[fy];
    }
    bool jude(int x,int y){
        int fx=find(x),fy=find(y);
        return fx==fy;
    }
    int main(){
        std::ios::sync_with_stdio(false);
        cin.tie(0);
        cin>>n;
        for(int i=1;i<=30000;i++){
            f[i]=i;
            pre[i]=0;
            back[i]=1;
        }
        for(int i=1;i<=n;i++){
            char st;
            int x,y;
            cin>>st>>x>>y;
            if(st=='M'){
                unit(x,y);
            }
            if(st=='C'){
                if(!jude(x,y))
                cout<<-1<<'\n';
                else
                cout<<abs(pre[x]-pre[y])-1<<'\n';
            }
        }
        return 0;
    }
    
    • 1

    信息

    ID
    197
    时间
    1000ms
    内存
    128MiB
    难度
    4
    标签
    递交数
    37
    已通过
    15
    上传者