1. 程式人生 > >Dragon Balls

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]);
            }
        }
    }
}