F - 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并查集带权问题
//这道题感觉有一个坑,,,就是当一个城市的龙珠被挪走,那么这个城市就没用了,不会再有龙珠移动过来了
//因为T A B (A和B都是龙珠),所以根节点的龙珠的下标就对应着他所在的城市,
//所以寻找某一个龙珠所在的城市我们只需要求它的根节点就好了。求一个城市中龙珠的个数,
//无非就是求这个根节点这棵树上有多少个子节点(自己也算)。主要是求龙珠移动的次数。
//我们开一个数组记录,先初始化为0,当连接某两个龙珠时,我们连接的是这两个龙珠的根节点,
//然后我们先让根节点移动,然后让根节点的上一个节点移动,,,以此类推,,。
#include<cstdio>
#include<cstring>
using namespace std;
const int N=1E5+7;
int fa[N];//记录父节点
int son[N];//记录树的大小
int ran[N];//记录移动次数
int find(int x){
    if(x==fa[x])
        return fa[x];
    else {
        int k=fa[x];
        fa[x]=find(fa[x]);
        ran[x]+=ran[k];//这里主要是用来传递根节点的移动。
        return fa[x];
    }
}

void join(int x,int y){
    int fx=find(x),fy=find(y);
    if(fx!=fy){
        fa[fx]=fy;
        son[fy]+=son[fx];
        ran[fx]++;//先让根节点移动
    }
}
//初始化
void inint(int x){
    memset(ran,0,sizeof(ran));
    for(int i=1;i<=x;i++){
        fa[i]=i;
        son[i]=1;
    }
}
int main(){
    int t,kk=0;
    scanf("%d",&t);
    while(t--){
        kk++;
        printf("Case %d:\n",kk);
        int n,m;
        scanf("%d%d",&n,&m);
        inint(n);
        for(int i=1;i<=m;i++){
            char a[10];
            scanf("%s",a);
            if(a[0]==‘T‘){
                int x,y;
                scanf("%d%d",&x,&y);
                join(x,y);
            }
            else if(a[0]==‘Q‘){
                int z;
                scanf("%d",&z);
                int m=find(z);
                printf("%d %d %d\n",m,son[m],ran[z]);
            }
        }
    }
    return 0;
} 


原文地址:https://www.cnblogs.com/Accepting/p/11306384.html

时间: 2024-10-13 12:52:32

F - Dragon Balls的相关文章

测试赛F - Dragon Balls(并查集)

F - Dragon Balls Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Description Five hundred years later, the number of dragon balls will increase unexpectedly, so it's too difficult for Monkey King(WuKong) to

hdu3635 Dragon Balls(带权并查集)

1 /* 2 题意:有N个城市, 每一个城市都有一个龙珠(编号与城市的编号相同),有两个操作 3 T A ,B 将标号为A龙珠所在城市的所有的龙珠移动到B龙珠所在城市中! 4 5 思路:并查集 (压缩路径的时候将龙珠移动的次数进行更新) 6 */ 7 #include<iostream> 8 #include<cstring> 9 #include<cstdio> 10 #include<algorithm> 11 #define M 10005 12 us

hdoj 3635 Dragon Balls

Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4517    Accepted Submission(s): 1719 Problem Description Five hundred years later, the number of dragon balls will increase unexpect

HDU 3635 Dragon Balls(并查集)

Dragon Balls Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 64   Accepted Submission(s) : 26 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Five hundred years later

HDU 3635 Dragon Balls (并查集)

Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3360    Accepted Submission(s): 1303 Problem Description Five hundred years later, the number of dragon balls will increase unexpect

Dragon Balls

Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 784 Accepted Submission(s): 317   Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly,

hdoj 3635 Dragon Balls【并查集求节点转移次数+节点数+某点根节点】

Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4384    Accepted Submission(s): 1673 Problem Description Five hundred years later, the number of dragon balls will increase unexpecte

HDU 3635 Dragon Balls 七龙珠 Union Find算法

孙悟空要寻找七龙珠,这回是七龙珠的增强版了,因为这些龙珠会衍生,最后不止七颗龙珠了. 悟空带着布玛的龙珠雷达探测器出发了,却发现布玛的龙珠雷达探测器的程序太垃圾了,所以找到我们这些ACM高手为龙珠雷达探测器写个程序,要求可以显示某颗龙珠所在的城市的位置,该龙珠所在的城市共有多少颗龙珠,龙珠移动过的次数. 布玛是个有钱人啊,写个程序我要价5百万,不算过分吧.因为本程序需要用到Union Find(并查集)算法,而且最困难的部分是如何压缩路径,不压缩路径自然容易做到,要压缩路径可以使得程序加快很多,

Dragon Balls HDU杭电3635 【并查集,递归的方法找根节点】

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 bal