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 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.
 
Input
The first line of the input is a single positive integer T( < T <= ).
For each case, the first line contains two integers: N and Q ( < N <= , < Q <= ).
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). ( <= A, B <= N)
 
Output
For 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

T
T
Q T
Q
T
Q
 
Sample Output
Case :
Case :
 
Author
possessor WC
 
Source
 
并查集,和前面的hdu2818很相似

主要是记录移动次数,其实每个根结点都是最多移动一次的,所以记录移动次数把自己的加上父亲结点的就是移动总数了

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<stdlib.h>
using namespace std;
#define N 10006
int n,q;
int fa[N];
int mov[N];
int num[N];
void init(){
for(int i=;i<N;i++){
fa[i]=i;
num[i]=;
mov[i]=;
}
}
int find(int son){
if(fa[son]!=son){
int t=find(fa[son]);
mov[son]+=mov[fa[son]];
fa[son]=t;
}
return fa[son];
//return fa[x]==x?x:fa[x]=find(fa[x]);
}
void merge(int x,int y){
int root1=find(x);
int root2=find(y);
if(root1==root2)return;
fa[root1]=root2;
mov[root1]=;
num[root2]+=num[root1];
}
int main()
{
int ac=;
int t;
scanf("%d",&t);
while(t--){
init();
scanf("%d%d",&n,&q); char s[];
int x,y;
printf("Case %d:\n",++ac);
for(int i=;i<q;i++){
scanf("%s",s);
if(s[]=='T'){
scanf("%d%d",&x,&y);
merge(x,y);
}
else{
scanf("%d",&x);
int city=find(x);
printf("%d %d %d\n",city,num[city],mov[x]);
}
}
}
return ;
}

hdu 3635 Dragon Balls(并查集应用)的更多相关文章

  1. hdu 3635 Dragon Balls(并查集)

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

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

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

  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(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)

    这道题说,在很久很久以前,有一个故事.故事的名字叫龙珠.后来,龙珠不知道出了什么问题,从7个变成了n个. 在悟空所在的国家里有n个城市,每个城市有1个龙珠,第i个城市有第i个龙珠. 然后,每经过一段时 ...

  5. hdu 3635 Dragon Balls (MFSet)

    Problem - 3635 切切水题,并查集. 记录当前根树的结点个数,记录每个结点相对根结点的转移次数.1y~ 代码如下: #include <cstdio> #include < ...

  6. hdu 3635 Dragon Balls

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

  7. HDU 3635 Dragon Balls(带权并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=3635 题意: 有n颗龙珠和n座城市,一开始第i颗龙珠就位于第i座城市,现在有2种操作,第一种操作是将x龙珠所在城 ...

  8. hdu 3635 Dragon Balls(并查集)

    题意: N个城市,每个城市有一个龙珠. 两个操作: 1.T A B:A城市的所有龙珠转移到B城市. 2.Q A:输出第A颗龙珠所在的城市,这个城市里所有的龙珠个数,第A颗龙珠总共到目前为止被转移了多少 ...

  9. HDU 1811 拓扑排序 并查集

    有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...

随机推荐

  1. Python字典的操作与使用

    字典的描述 字典是一种key-value的数据类型,使用就像我们上学用的字典,通过拼音(key)来查对应字的详细内容(value). 字典的特性 1.字典是无序的(不像列表一样有下标,它通过key来获 ...

  2. MVVM架构~knockoutjs系列之表单添加(验证)与列表操作源码开放

    返回目录 本文章应该是knockoutjs系列的最后一篇了,前几篇中主要讲一些基础知识,这一讲主要是一个实际的例子,对于一个对象的添加与编辑功能的实现,并将项目源代码公开了,共大家一起学习! knoc ...

  3. web请求的处理流程

    web请求的处理流程如下: 1.客户发起请求到服务器网卡:2.服务器网卡接受到请求后转交给内核处理:3.内核根据请求对应的套接字,将请求交给工作在用户空间的Web服务器进程4.Web服务器进程根据用户 ...

  4. CentOS6.6(单用户模式)重设root密码

    1.开机时手要快按任意键,因为默认时间5s 2.grub菜单,只有一个内核,没什么好上下选的,按e键.不过如果你升级了系统或安装了Xen虚拟化后,就会有多个显示了. 3.接下来显示如下,选择第二项,按 ...

  5. 关闭归档提示:ORA-38774: cannot disable media recovery - flashback database is enabled

    SQL> select * from v$version; Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit P ...

  6. My way on Linux - [虚拟化&云计算] - 云计算概述&KVM虚拟化基础

    思维导图

  7. p标签里面不要放div标签(块元素)

    最好不要在p标签里面嵌套块级元素(如div Ul): <p>我来测试下<div>块元素</div>放在p标签的情况</p> <p>我来测试下 ...

  8. pfile,spfile 初始化参数文件顺序【weber出品】

    一.初始化参数文件 启动实例时会读取初始化参数文件.参数文件有两种类型: 1.服务器参数文件:这是首选类型的初始化参数文件.这是一个由数据库服务器写入或读取的二进制文件,不得手动进行编辑.此文件驻留在 ...

  9. 用Java来比较JavaScript的一些特性

    由于是从java做到JavaScript,所以对强弱语言类型,还是比较敏感的.JavaScript是弱语言,只严格区分数据和指令.简单描述下自己对两者之间的一些区别吧. 1.JavaScript变量的 ...

  10. HDU 5900 - QSC and Master [ DP ]

    题意: 给n件物品,有key和value 每次可以把相邻的 GCD(key[i], key[i+1]) != 1 的两件物品,问移除的物品的总value最多是多少 key : 1 3 4 2  移除3 ...