zoukankan      html  css  js  c++  java
  • hdu 3635 并查集

    题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=3635

    Dragon Balls

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


    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
     
     
    分析:
    本题主要是用到并查集。 难点为,  怎么保存转移次数。 考虑并查集的路径压缩,就是通过路径压缩来更新转移的次数,比如每次移动时,我只需要把这个城市的根结点的转移次数+1,等到以后路径压缩时,子结点自己移动的次数加上根结点移动的次数,就是这个结点总共的移动次数。
     
     
    代码如下:
     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string>
     4 #include<string.h>
     5 #include<map>
     6 #include<math.h>
     7 #define N 10005
     8 using namespace std;
     9 struct node
    10 {
    11     int parent;  // 根节点
    12     int son;    // 子节点
    13     int rank; // 转移次数
    14 }p[N];
    15 int n;
    16 void init()
    17 {
    18     for(int i=1;i<=n;i++)
    19     {
    20         p[i].parent = i;
    21         p[i].rank =0;
    22         p[i].son = 1;
    23     }
    24 }
    25 int Find(int x)
    26 {
    27     if(x== p[x].parent) return x;
    28     int temp_parent=p[x].parent;
    29     p[x].parent= Find(p[x].parent);
    30     p[x].rank+=p[temp_parent].rank;  //因为,根节点连到另一个新的根节点后,根节点有变化,故需要更新x到新根节点的转移次数
    31     return p[x].parent;
    32 }
    33 void Union(int x, int y)
    34 {
    35      int xf=Find(x);
    36      int yf=Find(y);
    37      p[xf].parent=yf;
    38      p[xf].rank++;  
    39      p[yf].son+=p[xf].son;
    40      p[xf].son=0;
    41 }
    42 int main()
    43 {
    44     int t,count=1;
    45     scanf("%d",&t);
    46     while(t--)
    47     {
    48         int k,x,y;
    49         char c;
    50         printf("Case %d:
    ",count++);
    51         scanf("%d%d",&n,&k);
    52         init();
    53         for(int i=0;i<k;i++)
    54         {
    55             scanf("%*c%c",&c);
    56             if(c== 'T')
    57             {
    58                 scanf("%d%d",&x,&y);;
    59                 if(Find(x)!= Find(y))
    60                     Union(x,y);
    61             }
    62             else
    63             {
    64                 scanf("%d",&x);
    65                 int tmp=Find(x);
    66                 printf("%d %d %d
    ",tmp,p[tmp].son,p[x].rank);
    67             }
    68         }
    69     }
    70     return 0 ;
    71 }
  • 相关阅读:
    mysql笔记05 优化服务器设置
    mysql笔记04 MySQL高级特性
    mysql笔记03 查询性能优化
    MVC模型
    mysql笔记02 创建高性能的索引
    实现服务端功能之——Java实体对象
    leetcode解题: First Unique Character in a String (387)
    vim的使用心得
    Java多线程(五)--volatile详解
    七 生成式深度学习
  • 原文地址:https://www.cnblogs.com/zn505119020/p/3580055.html
Copyright © 2011-2022 走看看