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+;
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,,sizeof(ran));
for(int i=;i<=x;i++){
fa[i]=i;
son[i]=;
}
}
int main(){
int t,kk=;
scanf("%d",&t);
while(t--){
kk++;
printf("Case %d:\n",kk);
int n,m;
scanf("%d%d",&n,&m);
inint(n);
for(int i=;i<=m;i++){
char a[];
scanf("%s",a);
if(a[]=='T'){
int x,y;
scanf("%d%d",&x,&y);
join(x,y);
}
else if(a[]=='Q'){
int z;
scanf("%d",&z);
int m=find(z);
printf("%d %d %d\n",m,son[m],ran[z]);
}
}
}
return ;
}
												

F - Dragon Balls的更多相关文章

  1. HDU 3635:Dragon Balls(并查集)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  2. Dragon Balls[HDU3635]

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. hdu 3635 Dragon Balls(并查集)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. hdu 3635 Dragon Balls (带权并查集)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  5. hdu 3635 Dragon Balls

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

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

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  7. hdu 3635 Dragon Balls(并查集应用)

    Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...

  8. Dragon Balls

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. HDU 3635 Dragon Balls(超级经典的带权并查集!!!新手入门)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

随机推荐

  1. ubuntu在github上传项目

    GitHub是一个面向开源及私有软件项目的托管平台,因为只支持git 作为唯一的版本库格式进行托管,故名GitHub. 作为开源代码库以及版本控制系统,Github拥有超过900万开发者用户.随着越来 ...

  2. fastText 训练和使用

    2019-09-09 16:33:11 问题描述:fastText是如何进行文本分类的. 问题求解: fastText是一种Facebook AI Research在16年开源的一个文本分类器. 其特 ...

  3. [单调栈] 2018-2019 ACM-ICPC, China Multi-Provincial Collegiate Programming Contest-Maximum Element In A Stack

    题目:https://codeforces.com/gym/102222/problem/A Maximum Element In A Stack time limit per test 10.0 s ...

  4. 【bug】table重新加载数据,页面滚动条下沉到底部,记录scrollTop后将其恢复scrollTop出现闪烁

    1.table数据请求前记录scrollTop $scope.scrollPos = document.documentElement.scrollTop; 2.html中添加指令repeat-fin ...

  5. sql学习~pivot和unpivot用法

    pivot  可以把列值转换为输出中的多个列. pivot 可以在其他剩余的列的值上执行聚合函数. unpivot 将列转换为列值 语法 SELECT <non-pivoted column&g ...

  6. matplotlib.pyplot库函数关于坐标轴显示的支持

    matplotlib.pyplot库函数关于坐标轴显示的支持 https://blog.csdn.net/Stark_595/article/details/80787005?depth_1-utm_ ...

  7. 最小生成树(次小生成树)(最小生成树不唯一) 模板:Kruskal算法和 Prim算法

    Kruskal模板:按照边权排序,开始从最小边生成树 #include<algorithm> #include<stdio.h> #include<string.h> ...

  8. 一篇漫画故事带你理解透HTTPS(上)

    2020年蝙蝠纪元,二毛一如往常的呆在家中,不敢外出去浪. 为排解心中之闷,二毛抽了一口老烟,熟练的打开了全球最大的同性交友网站,准备假装了解下最近流行的项目... 只听啪的一声回车键,哪知浏览器蹦出 ...

  9. Git 命令实战入门 ,奶妈级教程

    我不会用*官方*的语言告诉你Git 是什么,对此我表示深深得歉意--在我看来像CSDN.博客园.掘金等博客交流平台就是小的“GitHub”,只不过在这里更多的是一些零零散散的笔记或者文章,其实Gihu ...

  10. mysql 5.7.18安装教程

    安装之前 确认是否已安装旧版mysql.如有,则卸载(注意需要的数据备份). /etc/init.d/mysqld stop yum remove mysql mysql-* rm -rf /var/ ...