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. BIT-Reverse Pairs

    2019-12-17 11:07:02 问题描述: 问题求解: 本题可以看作是逆序数问题的强化版本,需要注意的是num[i] > 2 * num[j],这里有0和负数的情况. public in ...

  2. IO多路复用(IO Multiplexing)

    什么是IO多路复用 为什么要有IO多路复用 作者总结 遵循学习新知识的三部曲:是什么?为什么?怎么用? 作者前言:IO多路复用本质上是网络通信过程中的一个技术名词. 什么是IO多路复用 一个用机场管理 ...

  3. 「MoreThanJava」当大学选择了计算机之后应该知道的

    「MoreThanJava」 宣扬的是 「学习,不止 CODE」,本系列 Java 基础教程是自己在结合各方面的知识之后,对 Java 基础的一个总回顾,旨在 「帮助新朋友快速高质量的学习」. 当然 ...

  4. Java——类的定义

    对象和类的关系:有一个学生 ,需要在表格上填写自己的信息 ,那么这个打印机就像一个类 ,打印出的表格就是一个对象,用类创建对象,学生填的信息 ,就是我所初始化的信息. 类的组成:由 属性(也叫成员变量 ...

  5. python中的可变和不可变对象 有序和无序对象

    可变对象和不可变对象的定义:对象存放在地址的值是否可以被改变 不可变对象包括:整形int.浮点型float .字符串str .元祖tuple.布尔型boole 可变对象包括 :列表list.集合set ...

  6. jdk下httpserver源码解析

    在写这篇博客之前我查了很久发现全网都没有一篇写httpserver源码解析的 所以今天就由我来为大家解析一下httpserver的源码.(这里我会去掉其中的https部分的源码,只讲http部分,对h ...

  7. java.lang.NoSuchMethodException: java.util.List.<init>()

    报错信息如下 java.lang.NoSuchMethodException: java.util.List.<init>() at java.lang.Class.getConstruc ...

  8. 透过 ReentrantLock 分析 AQS 的实现原理

    对于 Java 开发者来说,都会碰到多线程访问公共资源的情况,这时候,往往都是通过加锁来保证访问资源结果的正确性.在 java 中通常采用下面两种方式来解决加锁得问题: synchronized 关键 ...

  9. 粘包处理现象及其解决方案——基于NewLife.Net网络库的管道式帧长粘包处理方法

    [toc] #1.粘包现象 每个TCP 长连接都有自己的socket缓存buffer,默认大小是8K,可支持手动设置.粘包是TCP长连接中最常见的现象,如下图 socket缓存中有5帧(或者说5包)心 ...

  10. Educational Codeforces Round 84 (Rated for Div. 2)

    A. Sum of Odd Integers(思维) 思路 这一题看完ans之后觉得是真简单,不过有一些地方还是要理解的. 这一题输出YES,有两个条件 kk%2 == n%2k,这个条件的意思是 k ...