zoukankan      html  css  js  c++  java
  • hdu 3635 Dragon Balls

    #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:
    ",++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
    ",t,cnt[t],tot[x]);
                }
            }
    
        }
        return 0;
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/

  • 相关阅读:
    css(一)
    Html table
    Html
    jQuery事件和效果
    jQuery基础
    ajax和http状态码
    点击数组选中某一项,存入数组,再次点击删除
    单行两行,多余显示省略号
    git的使用
    产生滚动效果
  • 原文地址:https://www.cnblogs.com/xryz/p/4847983.html
Copyright © 2011-2022 走看看