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. hdu 5410 CRB and His Birthday(混合背包)

    Problem Description Today is CRB's birthday. His mom decided to buy many presents for her lovely son ...

  2. Android: 在WebView中获取网页源码

    1. 使能javascript: ? 1 webView.getSettings().setJavaScriptEnabled(true); 2. 编写本地接口 ? 1 2 3 4 5 final c ...

  3. css-盒模型,浮动,定位之间的关系

    网站布局属性:盒模型:调整元素间距float浮动:竖排的块级元素改成横排position定位:重叠元素,精确控制元素位置 能用盒模型,不用float,能用浮动,不用定位

  4. (转)IIS7 下部署Asp.net应用

    最近在部署一个ASP.NET的应用到IIS7中的时候,遇到了一些问题,现在把部署中的遇到的问题和部署步骤进行总结一下,本文中只涉及到ASP.NET的基本部署. 一.    部署环境 Windows 7 ...

  5. CodeFirst Fluent API

    本文转自:疯狂的我  CodeFirst Fluent API EF的好处之一就是实现了概念模型和存储模型的分离,我们可以为概念实体和存储实体起不同的名称,同时还可以将一个概念实体映射到多个存储实体, ...

  6. Oracle System密码忘记 密码修改、删除账号锁定lock

    一下转自http://www.cnblogs.com/yjhrem/articles/2340149.html 运行cmd命令行 录入 sqlplus /nolog  无用户名登录 conn /as ...

  7. .net 判断日期格式yyyy-MM-dd hh:MM:ss的正则表达式

    加上引用: using System.Text.RegularExpressions; /// <summary> /// 检查字符串是否是日期格式        /// </sum ...

  8. [转载]iOS开发:获取设备信息

    开发iOS平台的应用的时候,可以获取iOS设备的设备信息,包括设备的名称,设备的机型,设备的iOS版本等等.设备信息主要来自 UIDevice 类. UIDevice *currentDevice = ...

  9. IIS 7 支持10万并发请求

    原文链接:http://www.cnblogs.com/dudu/archive/2009/11/10/1600062.html 今天下午17点左右,博客园博客站点出现这样的错误信息: Error S ...

  10. Asp.net mvc4 + HighCharts + 曲线图

    前端代码: @{ Layout = null;}<!DOCTYPE html><html><head> <title></title> &l ...