hdu 3635 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.
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)
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.
T
T
Q T
Q
T
Q
Case :
Case :
主要是记录移动次数,其实每个根结点都是最多移动一次的,所以记录移动次数把自己的加上父亲结点的就是移动总数了
#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(并查集应用)的更多相关文章
- hdu 3635 Dragon Balls(并查集)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 3635 Dragon Balls (带权并查集)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 3635 Dragon Balls(超级经典的带权并查集!!!新手入门)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 3635 Dragon Balls(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)
这道题说,在很久很久以前,有一个故事.故事的名字叫龙珠.后来,龙珠不知道出了什么问题,从7个变成了n个. 在悟空所在的国家里有n个城市,每个城市有1个龙珠,第i个城市有第i个龙珠. 然后,每经过一段时 ...
- hdu 3635 Dragon Balls (MFSet)
Problem - 3635 切切水题,并查集. 记录当前根树的结点个数,记录每个结点相对根结点的转移次数.1y~ 代码如下: #include <cstdio> #include < ...
- hdu 3635 Dragon Balls
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- HDU 3635 Dragon Balls(带权并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=3635 题意: 有n颗龙珠和n座城市,一开始第i颗龙珠就位于第i座城市,现在有2种操作,第一种操作是将x龙珠所在城 ...
- hdu 3635 Dragon Balls(并查集)
题意: N个城市,每个城市有一个龙珠. 两个操作: 1.T A B:A城市的所有龙珠转移到B城市. 2.Q A:输出第A颗龙珠所在的城市,这个城市里所有的龙珠个数,第A颗龙珠总共到目前为止被转移了多少 ...
- HDU 1811 拓扑排序 并查集
有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...
随机推荐
- Html中版权符号的字体选择问题(如何让版权符号更美观)
一.发现问题 ©是html的中版权的符号,但是字体选择的不对会带来一些问题.如果是宋体,这个符号显示的就是很奇怪的一个符号. 二.解决问题 复制代码 代码如下: <span style=&quo ...
- AVL旋转树
执行插入操作可能出现不平衡的情况,当平衡二叉树.AVL这树是一种自平衡二叉树,使二叉树又一次保持平衡.而且查找.插入和删除操作在平均和最坏情况下时间复杂度都是O(log n) AVL树的旋转一共同拥有 ...
- crtmpserver通常使用基本类演示
以前我们做了分析过程,这一次,我们都参与了类做梳子,两个可以一起关注一下一起合并,整个方案的实施是有帮助. BaseClientApplication APP基类,一切APP都基于这个类 Stream ...
- SELECT--UNION,UNION ALL,MINUS, INTERSECT,EXISTS
SELECT--UNION,UNION ALL,MINUS, INTERSECT返回两个查询结果的集合操作,两个查询结果集必须字段相同.UNION和UNION ALL并集操作,UNION并集后去掉重复 ...
- My way on Linux - [虚拟化&云计算] - 云计算概述&KVM虚拟化基础
思维导图
- MySql查看表信息
SELECT TABLE_NAME, TABLE_COMMENT -- 指定信息列 FROM `information_schema`.`tables` A WHERE A.`TABLE_SCHEMA ...
- Action重定向总结
[HttpPost] public ActionResult StudentList( string StudName, string studName, DateTime BirthDay, For ...
- IOS 错误集合以及解决办法(持续整理中)
1 . 如下错误: app:resource fork, Finder information, or similar detritus not al site:forums.developer.ap ...
- Light oj 1030 概率DP
D - Discovering Gold Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:32768 ...
- sdsdd
while(scanf("%d",&n)!=EOF) { res=-; level(tmp,n,res,); printf("%d/n",res); }