• HDU 3635 Dragon Balls(带权并查集)


    http://acm.hdu.edu.cn/showproblem.php?pid=3635

    题意:

    有n颗龙珠和n座城市,一开始第i颗龙珠就位于第i座城市,现在有2种操作,第一种操作是将x龙珠所在城市的所有龙珠移至y龙珠所在城市,第二种操作是计算x龙珠所在城市y,y城市龙珠个数,以及x龙珠移动的次数。

    思路:
    num[x]用来维护x城市所包含的龙珠个数,cnt[x]维护x龙珠所移动的次数。

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<vector>
     6 #include<stack>
     7 #include<queue>
     8 #include<cmath>
     9 #include<map>
    10 #include<set>
    11 using namespace std;
    12 typedef long long ll;
    13 typedef pair<int,int> pll;
    14 const int INF = 0x3f3f3f3f;
    15 const int maxn = 10000+5;
    16 
    17 int n, m;
    18 int p[maxn], num[maxn], cnt[maxn];
    19 
    20 int finds(int x)
    21 {
    22     if(p[x]==x)  return x;
    23     int tmp=p[x];
    24     p[x]=finds(p[x]);
    25     cnt[x]+=cnt[tmp];
    26     return p[x];
    27 }
    28 
    29 int main()
    30 {
    31    //freopen("in.txt","r",stdin);
    32    int T;
    33    int kase = 0;
    34    scanf("%d",&T);
    35    while(T--)
    36    {
    37        printf("Case %d:
    ",++kase);
    38        scanf("%d%d",&n,&m);
    39        for(int i=1;i<=n;i++)  {p[i]=i;num[i]=1;cnt[i]=0;}
    40        while(m--)
    41        {
    42            char op[2];
    43            scanf("%s",op);
    44            if(op[0]=='T')
    45            {
    46                int x,y;
    47                scanf("%d%d",&x,&y);
    48                int xx = finds(x);
    49                int yy = finds(y);
    50                if(xx!=yy)
    51                {
    52                    p[xx]=yy;
    53                    cnt[xx]++;
    54                    num[yy]+=num[xx];
    55                    num[xx]=0;
    56                }
    57            }
    58            else
    59            {
    60                int x;
    61                scanf("%d",&x);
    62                int ans = finds(x);
    63                printf("%d %d %d
    ",ans,num[ans],cnt[x]);
    64            }
    65        }
    66    }
    67    return 0;
    68 }
  • 相关阅读:
    zzuli-2259 matrix
    【vlan之四种方式链路认证组网]
    【ppp-chap,pap,mp,mp-group】
    【ospf-基础配置】
    【rip-基础配置】
    【静态路由】
    【nat---basic,napt,easy ip】
    【acl-访问控制列表】
    【交换接口的-绑定-认证-隔离】
    【vlan-给予mac地址认证】
  • 原文地址:https://www.cnblogs.com/zyb993963526/p/7805800.html
Copyright © 2020-2023  润新知