zoukankan      html  css  js  c++  java
  • 5.1.4 Dragon Balls

    Dragon Balls

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

    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

    思路:这题居然1A了,太神奇了。并查集嘛,对于个数这个很好求,但是对于转移次数需动下脑筋。我们可以用一个tran表示此节点距离它自己现在的祖先(f数组)的转移次数,有点难理解,也就是说如图所示

     1 #include <cstdio>
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <algorithm>
     5 #include <cstring>
     6 #include <string>
     7 using namespace std;
     8 
     9 const int maxn=100100;
    10 int f[maxn],tran[maxn],sum[maxn];
    11 int af,bf,a,b,T,n,m;
    12 char s[2];
    13 
    14 void close()
    15 {
    16     fclose(stdin);
    17     fclose(stdout);
    18     exit(0);
    19 }
    20 
    21 int getfather(int k)
    22 {
    23     if (k==f[k])
    24         return k;
    25     return getfather(f[k]);
    26 }
    27 
    28 
    29 int find(int k)
    30 {
    31     if (f[k]==k)
    32         return k;
    33     int t=f[k];
    34     f[k]=find(f[k]);
    35     if (t!=f[t])
    36         tran[k]+=tran[t];
    37     return f[k];
    38 }
    39 
    40 void work()
    41 {
    42 }
    43 
    44 void init ()
    45 {
    46 freopen("dragon.in","r",stdin);
    47 freopen("dragon.out","w",stdout);
    48     scanf("%d",&T);
    49      int cnt=0;
    50      while (T--)
    51      {
    52          cnt++;
    53          printf("Case %d:\n",cnt);
    54          memset(tran,0,sizeof(tran));
    55          memset(f,0,sizeof(f));
    56          scanf("%d %d",&n,&m);
    57          for (int i=1;i<=n;i++)
    58          {
    59              f[i]=i;
    60              sum[i]=1;
    61          }
    62          for (int i=1;i<=m;i++)
    63          {
    64              scanf("%s",s);
    65              if (s[0]=='T')
    66              {
    67                  scanf("%d %d",&a,&b);
    68                  af=getfather(a);
    69                  bf=getfather(b);
    70                  f[af]=bf;
    71                  tran[af]++;
    72                  sum[bf]+=sum[af];
    73              }
    74              else
    75              {
    76                  scanf("%d",&a);
    77                  af=find(a);
    78                  printf("%d %d %d\n",af,sum[af],tran[a]);
    79              }
    80          }
    81      }
    82 }
    83 
    84 int main ()
    85 {
    86     init();
    87     work();
    88     close();
    89     return 0;
    90 }

    把freopen那些删掉

  • 相关阅读:
    位运算学习
    C语言从文件中读取数字
    百度网盘视频加速代码
    算法思想
    递归解决全排列问题
    android studio出现offline情况
    二叉树的遍历(递归与非递归)
    (一)为什么要UML
    Logstash详解之——input模块
    解决maven项目update project会更改jdk版本问题
  • 原文地址:https://www.cnblogs.com/cssystem/p/2911196.html
Copyright © 2011-2022 走看看