zoukankan      html  css  js  c++  java
  • 杭电3635--Dragon Balls(并查集)

    Dragon Balls

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


    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
     

     

    Recommend
    lcy   |   We have carefully selected several similar problems for you:  3172 3234 3639 3633 3638 
    题意:N个球放在N个城市(1—n); T A B  A全部球移到B ;   Q X 输出结果。
       求经处理后城市中球数, 球的ID, 球转移次数;(最要命的是找不到思路);
     1 #include <cstdio>
     2 #include <iostream>
     3 using namespace std;
     4 int father[10001], ran[10001], num[10001] ;      //位置, 节点数, 转换次数;
     5 int i, n;
     6 void init()
     7 {
     8     for(i=1; i<=n; i++)
     9     {
    10         father[i]=i; ran[i]=1; num[i]=0;
    11     }    
    12 } 
    13 int find(int a)
    14 {
    15     if(a != father[a])
    16     {
    17         int k = father[a];
    18         father[a] = find(father[a]);
    19         num[a] += num[k];                      //递归计算转化次数;(每个节点只移动一次, 然后跟随父节点一起移动) 
    20     }
    21     return father[a];
    22 }
    23 void mercy(int a, int b)
    24 {
    25     int q = find(a);
    26     int p = find(b);
    27     if(q != p)
    28     {
    29         father[q] = p;
    30         ran[p] += ran[q];
    31         num[q] = 1; 
    32     }
    33 }
    34 int main()
    35 {
    36     int t, m, a, b, c, temp=1; char str[2];
    37     scanf("%d", &t);
    38     while(t--)
    39     {
    40         printf("Case %d:
    ",temp++);
    41         scanf("%d %d", &n, &m);
    42         init();
    43         while(m--)
    44         {
    45             scanf("%s", str);
    46             if(str[0] == 'T')
    47             {
    48                 scanf("%d %d", &a, &b);
    49                 mercy(a, b);
    50             }
    51             else
    52             {
    53                 scanf("%d", &c);
    54                 int k = find(c);
    55                 printf("%d %d %d
    ", k, ran[k], num[c]);
    56             }
    57         }
    58     }
    59     return 0;    
    60 } 
     
  • 相关阅读:
    进阶篇:3.2.5)DFM钣金-常见装配和成形结构
    基础篇:3.4)3d模型绘制的好坏会影响产品合格率(注意点)
    进阶篇:2.1)DFMA实施障碍和关键
    [洛谷P2224][题解][HNOI2001]产品加工
    [洛谷P1262][题解]间谍网络
    [洛谷P3919][题解]可持久化数组&&主席树讲解
    [洛谷P5677][题解][GZOI2017]配对统计
    [洛谷P1040][题解]加分二叉树
    [校内赛3-1][题解]folder
    [校内赛3-3][题解]block
  • 原文地址:https://www.cnblogs.com/soTired/p/4694717.html
Copyright © 2011-2022 走看看