Dragon Balls

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.
InputThe 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)OutputFor 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

分析:

这道题用并查集,我发现了若是在并查集还要求一些别的(比如这题要求转移次数等)find函数还是写成递归的形式比较好,方便在压缩路径的时候,进行一些操作。

int find1(int a){
    if(pre[a]==a)return a;
    int tmp=pre[a];
    pre[a]=find1(pre[a]);
    return pre[a];
}
int join(int a,int b){
    int root1=find1(a),root2=find1(b);
    if(root1!=root2){
        pre[root1]=root2;
    }
}

最初做的时候,设了city[i]表示球i的城市,然后每次移动都遍历球1到N,把城市和移动次数改变……结果就是TLE……

后来又想了一下可以这么做:虽然,说连接的时候,谁连谁都是一样的,但是我们可以让pre[i]就等于i所在的城市,比如:T 2 3

就让pre[2]=3,正好是球2所在的城市编号。

本题的关键就是在递归的时候求tra,核心代码如下:

int f(int a){
    if(pre[a]==a)return a;
    int tmp=pre[a];
    pre[a]=f(tmp);
    tra[a]+=tra[tmp];//结点移动的次数+=父结点移动的次数
    return pre[a];
}

void join(int a,int b){//把a球所在的城市的所有球移到b球所在的城市上
    int root1=f(a);//a球所在的城市
    int root2=f(b);//b球所在的城市
    if(root1!=root2){
        pre[root1]=root2;
        num[root2]+=num[root1];//root2城市的球数+=root1城市球数
        tra[root1]=1;
    }
}

解释一下join函数里的tra[root1]=1;为什么不是++,而是赋值为1,比如T 2 3,此时tra[2]=1;T 2 4,此时tra[3]=1;表示的是根节点的移动次数,只能是1,(自己往自己里面移的情况也是1)

在f中tra[a]+=tra[tmp];也就是说,每个结点的移动次数,等于自己的移动次数,加上它上面所有点的移动次数。

代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=10003;
int pre[maxn],tra[maxn],num[maxn];//表示球i所在的城市,i的移动次数,i城市有的球的数目

int f(int a){
    if(pre[a]==a)return a;
    int tmp=pre[a];
    pre[a]=f(tmp);
    tra[a]+=tra[tmp];
    return pre[a];
}

void join(int a,int b){//把a球所在的城市的所有球移到b球所在的城市上
    int root1=f(a);//a球所在的城市
    int root2=f(b);//b球所在的城市
    if(root1!=root2){
        pre[root1]=root2;
        num[root2]+=num[root1];//root2城市的球数+=root1城市球数
        tra[root1]=1;
    }
}

int main(){
    int t,n,q;
    scanf("%d",&t);
    for(int i=1;i<=t;i++){
        printf("Case %d:\n",i);
        scanf("%d%d",&n,&q);
        for(int i=1;i<=n;i++){
            pre[i]=i;
            tra[i]=0;
            num[i]=1;
        }

        char ch[5];
        int root1,root2,a,b,x;
        while(q--){
            scanf("%s",&ch);
            if(ch[0]=='T'){
                scanf("%d%d",&a,&b);
                join(a,b);
            }
            else {
                scanf("%d",&x);
                int city=f(x);
                printf("%d %d %d\n",city,num[city],tra[x]);
            }
        }
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值