银河英雄传说

来源:互联网 发布:人口老龄化数据 编辑:程序博客网 时间:2024/04/28 06:55
#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\n",abs(q[b]-q[c])-1);            else            printf("-1\n");        }    }    return 0;} 

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

1 0
原创粉丝点击