银河英雄传说

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int t;
int q[99999];
int h[99999];
int f[99999];
int find(int x)
{
    if(f[x]==x)return x;
    int fx=find(f[x]);
    q[x]=q[x]+q[f[x]];
    return f[x]=fx;
    //if(f[x]!=x) f[x]=find(f[x]);
//  q[x]=q[x]+q[f[x]];
    //return f[x];
}
void work(int b,int c)
{
    int fx=find(b);
    int fy=find(c);
    //q[fx]=fy;
    q[fx]=h[fy];
    h[fy]+=h[fx];
    //q[c]=q[b]+h[b]+q[c];
    //h[b]=h[b]+h[c];
    f[fx]=fy;
}
int main()
{
    scanf("%d",&t);
    for(int i=1;i<=30000;i++) f[i]=i,h[i]=1;
    for(int i=1;i<=t;i++) 
    {
        char a;
        int b,c;
        cin>>a>>b>>c;
        if(a=='M')
        {
            work(b,c);
        }
        if(a=='C')
        {
            if(find(b)==find(c))
            printf("%d
",abs(q[b]-q[c])-1);
            else
            printf("-1
");
        }
    }
    return 0;
} 

这个题abs外减1
并且还要q+上去

原文地址:https://www.cnblogs.com/wspl98765/p/6819890.html