hdu 3635 Dragon Balls

来源:互联网 发布:雷洋案 知乎 编辑:程序博客网 时间:2024/06/07 06:25
#include<stdio.h>#include<algorithm>#include<string.h>using namespace std;const int N=10000+5;int n,bin[N],tot[N],cnt[N];//tot是转移次数,cnt是龙珠次数int _find(int x){    if(x==bin[x]) return x;    else    {        int t=bin[x];        bin[x]=_find(bin[x]);//路径压缩        tot[x]+=tot[t];//将压缩过程中的转移次数相加        return bin[x];    }}void _union(int x,int y){    int fx=_find(x),fy=_find(y);    bin[fx]=fy;    cnt[fy]+=cnt[fx];    tot[fx]=1;}void init(){    for(int i=0;i<N;i++)    {        bin[i]=i;        tot[i]=0;        cnt[i]=1;    }}int main(){    int  T,i,q,cas=0,x,y;    char cmd[10];    scanf("%d",&T);    while(T--)    {        init();        scanf("%d%d",&n,&q);        printf("Case %d:\n",++cas);        for(i=0;i<q;i++)        {            scanf("%s",&cmd);            if(cmd[0]=='T')            {                scanf("%d%d",&x,&y);                _union(x,y);            }            else if(cmd[0]=='Q')            {                scanf("%d",&x);                int t=_find(x);                printf("%d %d %d\n",t,cnt[t],tot[x]);            }        }    }    return 0;}
0 0
原创粉丝点击