HDU 3635 Dragon Balls

本文介绍了一个涉及并查集数据结构的编程挑战,通过处理龙珠的位置变化和查询,实现高效的数据管理和查询操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
 

题意: 对于q次操作,如果是T开头,就表示把A球所在城市的球全部移到B球所在的城市。如果Q开头,就输出三个数,第一个是A的所在地,第二个是A所在地的球数,第三个是A移动的次数。

思路: 并查集。 对于T,A ,B 的祖先为A,B 的所在地。把A序号的祖先序号 设置为 B的祖先序号,把A所在地的球数加到B所在地的球数,把A所在地的球数清零。对于计算A的移动次数,可以这样想:用一个数组保存每个点的移动次数,根据并查集的特性,每个祖先只会被合并一次,那么可以让A的祖先的移动次数赋为1,下次再移动A的时候利用路径压缩把A的父亲节点移动过的次数加到A上,相当于A的移动次数等于A的各个祖先移动的次数之和。


#include<stdio.h>
int t,a,b,n,q,cnt[10005];
char s[5];
struct C
{
    int pre,num;
}fa[10005];
void In()
{
    scanf("%d %d%*c",&n,&q);
    for(int i=1;i<=n;i++) {
        fa[i].pre=i;
        fa[i].num=1;
        cnt[i]=0; //每个点移动次数一开始都是0
    }
}
int Find(int x)
{
    if(x!=fa[x].pre) {
        int tmp=fa[x].pre;
        fa[x].pre=Find(fa[x].pre);
        cnt[x]+=cnt[tmp]; // 加上它父亲的移动次数。
    }
    return fa[x].pre;
}
int main()
{
    scanf("%d%*c",&t);
    for(int ca=1;ca<=t;ca++) {
        In();
        printf("Case %d:\n",ca);
        for(int k=1;k<=q;k++) {
            scanf("%s ",s);
            if(s[0]=='T') {
                int fx,fy;
                scanf("%d %d%*c",&a,&b);
                fx=Find(a);
                fy=Find(b);
                if(fx!=fy) {
                    fa[fy].num+=fa[fx].num;
                    fa[fx].num=0;
                    fa[fx].pre=fy;
                    cnt[fx]=1; // 祖先节点只会移动一次。
                }
            } else {
                scanf("%d%*c",&a);
                int fx=Find(a);
                printf("%d %d %d\n",fx,fa[fx].num,cnt[a]);
            }
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值