MENU

HDOJ3635 Dragon Balls(带权并查集)

2018 年 04 月 17 日 • 阅读: 1064 • 数据结构阅读设置

Dragon Balls

  • Time Limit: 2000/1000 MS (Java/Others)
  • Memory Limit: 32768/32768 K (Java/Others)
  • Total Submission(s): 7485
  • Accepted Submission(s): 2767

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.
img
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

2010 ACM-ICPC Multi-University Training Contest(19)——Host by HDU

Recommend

lcy

链接

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

题意

开始时有n个龙珠分布在n个城市,编号从1到n,现有两种操作:

  • T A B:将龙珠A所在城市的所有龙珠移到龙珠B所在的城市
  • Q A:问龙珠A所在的城市编号,问该城市有的龙珠总数,问龙珠A移动的次数

题解

这道题基本上和POJ1988 Cube Stacking一样,使用并查集来维护。用$sum[x]$表示龙珠$x$所在城市的龙珠总数,$cnt[x]$记录示龙珠$x$移动的次数。在合并操作时,我们总是以该城市的第一个作为父节点。

  • 合并时F[fx]=fy, sum[fy]+=sum[fx], cnt[fx]++
  • 路径压缩寻找根节点时cnt[x]+=cnt[fx]

具体参考POJ1988 Cube Stacking,写的比较详细。

代码

StatusAccepted
Time748ms
Memory1800kB
Length1195
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 10010;
int F[maxn];
int cnt[maxn];
int sum[maxn];

void init()
{
    for (int i = 0; i < maxn; ++i)
    {
        F[i] = i;
        sum[i] = 1;
        cnt[i] = 0;
    }
}

int find(int root)
{
    if (root == F[root])
        return root;
    int tmp = find(F[root]);
    cnt[root] += cnt[F[root]]; // 寻找根节点的过程中进行更新
    F[root] = tmp;
    return F[root];
}

void merge(int u, int v) // 合并
{
    int fu = find(u);
    int fv = find(v);
    if (fu != fv)
    {
        F[fu] = fv;
        sum[fv] += sum[fu]; // 龙珠总数更新
        cnt[fu]++;  // 移动次数更新
    }
}

int main()
{
    int T, n, q, u, v;
    char s[5];
    scanf("%d", &T);
    int k = 0;
    while (T--)
    {
        init();
        k++;
        scanf("%d%d", &n, &q);
        printf("Case %d:\n", k);
        for (int i = 0; i < q; ++i)
        {
            scanf("%s", s);
            if (s[0] == 'T')
            {
                scanf("%d%d", &u, &v);
                merge(u, v);
            }
            else
            {
                scanf("%d", &u);
                int fu = find(u);
                printf("%d %d %d\n", fu, sum[fu], cnt[u]);
            }
        }
    }
    return 0;
}
The end.
2018-04-17 星期二
最后编辑于: 2018 年 05 月 06 日