zoukankan      html  css  js  c++  java
  • hdu 3635 Dragon Balls (带权并查集)

    Dragon Balls

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3363    Accepted Submission(s): 1304


    Problem Description
    Five hundred years later, the number of dragon balls will increase unexpectedly, so it's too difficult for Monkey King(WuKong) to gather all of the dragon balls together. 

    His country has N cities and there are exactly N dragon balls in the world. At first, for the ith dragon ball, the sacred dragon will puts it in the ith city. Through long years, some cities' dragon ball(s) would be transported to other cities. To save physical strength WuKong plans to take Flying Nimbus Cloud, a magical flying cloud to gather dragon balls. 
    Every time WuKong will collect the information of one dragon ball, he will ask you the information of that ball. You must tell him which city the ball is located and how many dragon balls are there in that city, you also need to tell him how many times the ball has been transported so far.
     
    Input
    The first line of the input is a single positive integer T(0 < T <= 100). 
    For each case, the first line contains two integers: N and Q (2 < N <= 10000 , 2 < Q <= 10000).
    Each of the following Q lines contains either a fact or a question as the follow format:
      T A B : All the dragon balls which are in the same city with A have been transported to the city the Bth ball in. You can assume that the two cities are different.
      Q A : WuKong want to know X (the id of the city Ath ball is in), Y (the count of balls in Xth city) and Z (the tranporting times of the Ath ball). (1 <= A, B <= N)
     
    Output
    For each test case, output the test case number formated as sample output. Then for each query, output a line with three integers X Y Z saparated by a blank space.
     
    Sample Input
    2
    3 3
    T 1 2 T 3 2 Q 2 3 4 T 1 2 Q 1 T 1 3 Q 1
     
    Sample Output
    Case 1: 2 3 0 Case 2: 2 2 1 3 3 2
     
    Author
    possessor WC
     
    Source
     

     题意:

          每一个城市都有一颗龙珠,但是随着时间的推移,龙珠会被移到其他的城市,悟空想去收集这些龙珠,但是他需要你告知他,他要找的那颗龙珠的所在的城市,以及这个城市所拥有的龙珠数量,还有这颗龙珠迁移过多少次。

    代码:

     1 #define LOCAL
     2 #include<cstring>
     3 #include<cstdio>
     4 #define maxn 10005
     5 /*
     6     (1) 第i求所在的城市x  city[];
     7     (2)    x城市所拥有的球数量 ball[];
     8     (3)    i球被转移的次数     cnt[];
     9 */
    10 int ball[maxn];
    11 int city[maxn];
    12 int cnt[maxn];
    13 int n,q;
    14  void init(){
    15    for(int i=1 ; i<=n ;i++ ){
    16          city[i]=i;
    17          ball[i]=1;
    18          cnt[i]=0; //转移次数
    19    }
    20  }
    21 
    22 //搜索该龙珠所在城市的位置
    23 int  fin(int x)
    24 {
    25     //int tem;
    26     if(x==city[x])
    27       return city[x];
    28     int tem=city[x];
    29     city[x]=fin(city[x]);
    30     cnt[x]+=cnt[tem];
    31     return  city[x];
    32 }
    33 
    34 void Union(int x,int y)
    35 {
    36     /*
    37       将 x城市的所有龙珠转移到y城市中
    38     */
    39     x=fin(x);
    40     y=fin(y);
    41    if(x!=y){
    42        city[x]=city[y];
    43     ball[y]+=ball[x];
    44     ball[x]=0;  //球全部移动到y城市中
    45     cnt[x]=1;  //第一次移动
    46    }
    47 }
    48 
    49 int main()
    50 {
    51 
    52   int t,a,b,tt=0;
    53   char str[2];
    54   scanf("%d",&t);
    55   while(t--){
    56    scanf("%d%d",&n,&q);
    57    init();
    58    printf("Case %d:
    ",++tt);
    59    while(q--){
    60          scanf("%s",str);
    61          if(str[0]=='T'){
    62           scanf("%d%d",&a,&b);
    63           Union(a,b);
    64       }
    65       else
    66       {
    67           scanf("%d",&a);
    68             b=fin(a);
    69             printf("%d %d %d
    ",b,ball[b],cnt[a]);
    70       }
    71    }
    72   }
    73   return 0;
    74 }
    View Code
  • 相关阅读:
    Appium移动端UI自动化中,如果需要两个APP交互操作的实践经验
    基于Hibernate对Http接口进行全集测试实践
    Http自动跳转Https的接口测试实践
    PC端稳定性测试探索
    Batch脚本的简单应用
    Appium:中文输入的问题
    Android自动化:如何获取到APK安装包的Package以及Activity属性值
    收集一些深度学习视频
    1.1 摄像机的移动
    EF使用动态类型
  • 原文地址:https://www.cnblogs.com/gongxijun/p/4149758.html
Copyright © 2011-2022 走看看