Dragon Balls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3363    Accepted Submission(s): 1304

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(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)
 
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
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
 
Author
possessor WC
 
Source
 

题意:

每一个城市都有一颗龙珠,但是随着时间的推移,龙珠会被移到其他的城市,悟空想去收集这些龙珠,但是他需要你告知他,他要找的那颗龙珠的所在的城市,以及这个城市所拥有的龙珠数量,还有这颗龙珠迁移过多少次。

代码:

 #define LOCAL
#include<cstring>
#include<cstdio>
#define maxn 10005
/*
(1) 第i求所在的城市x city[];
(2) x城市所拥有的球数量 ball[];
(3) i球被转移的次数 cnt[];
*/
int ball[maxn];
int city[maxn];
int cnt[maxn];
int n,q;
void init(){
for(int i= ; i<=n ;i++ ){
city[i]=i;
ball[i]=;
cnt[i]=; //转移次数
}
} //搜索该龙珠所在城市的位置
int fin(int x)
{
//int tem;
if(x==city[x])
return city[x];
int tem=city[x];
city[x]=fin(city[x]);
cnt[x]+=cnt[tem];
return city[x];
} void Union(int x,int y)
{
/*
将 x城市的所有龙珠转移到y城市中
*/
x=fin(x);
y=fin(y);
if(x!=y){
city[x]=city[y];
ball[y]+=ball[x];
ball[x]=; //球全部移动到y城市中
cnt[x]=; //第一次移动
}
} int main()
{ int t,a,b,tt=;
char str[];
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&q);
init();
printf("Case %d:\n",++tt);
while(q--){
scanf("%s",str);
if(str[]=='T'){
scanf("%d%d",&a,&b);
Union(a,b);
}
else
{
scanf("%d",&a);
b=fin(a);
printf("%d %d %d\n",b,ball[b],cnt[a]);
}
}
}
return ;
}

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

  1. hdu 3635 Dragon Balls(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)

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

  2. hdu3635 Dragon Balls(带权并查集)

    /* 题意:有N个城市, 每一个城市都有一个龙珠(编号与城市的编号相同),有两个操作 T A ,B 将标号为A龙珠所在城市的所有的龙珠移动到B龙珠所在城市中! 思路:并查集 (压缩路径的时候将龙珠移动 ...

  3. hdu 5441 Travel 离线带权并查集

    Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...

  4. Hdu 2047 Zjnu Stadium(带权并查集)

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

  5. How Many Answers Are Wrong (HDU - 3038)(带权并查集)

    题目链接 并查集是用来对集合合并查询的一种数据结构,或者判断是不是一个集合,本题是给你一系列区间和,判断给出的区间中有几个是不合法的. 思考: 1.如何建立区间之间的联系 2.如何发现悖论 首先是如何 ...

  6. hdu 5441 travel 离线+带权并查集

    Time Limit: 1500/1000 MS (Java/Others)  Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...

  7. hdu 2818 Building Block (带权并查集,很优美的题目)

    Problem Description John are playing with blocks. There are N blocks ( <= N <= ) numbered ...N ...

  8. How Many Answers Are Wrong HDU - 3038 (经典带权并查集)

    题目大意:有一个区间,长度为n,然后跟着m个子区间,每个字区间的格式为x,y,z表示[x,y]的和为z.如果当前区间和与前面的区间和发生冲突,当前区间和会被判错,问:有多少个区间和会被判错. 题解:x ...

  9. hdu 3047–Zjnu Stadium(带权并查集)

    题目大意: 有n个人坐在zjnu体育馆里面,然后给出m个他们之间的距离, A B X, 代表B的座位比A多X. 然后求出这m个关系之间有多少个错误,所谓错误就是当前这个关系与之前的有冲突. 分析: 首 ...

随机推荐

  1. 关于stl string find 容易犯的一个错误

    有时候经常会判断一个字符串a中是否有子字符串b,那么有人会调用 string::find这个函数  这个函数返回子字符串首次出现的位置,那么有人会这样写 string str1 = "&qu ...

  2. node-webkit中使用sqlite3(MAC平台)

    前言 最近使用node-webkit开发一款博客发布软件,来替换难用的Windows Live Writer(主要是对Markdown标签的支持很差劲).为了解决博文信息临时保存的问题,想到了使用sq ...

  3. [51NOD1959]循环数组最大子段和(dp,思路)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1050 这道题的最大子段和有两种可能,一种是常规的子段和,另一种 ...

  4. 用Spring MVC开发简单的Web应用

    这个例子是来自于Gary Mak等人写的Spring攻略(第二版)第八章Spring @MVC中的一个例子,在此以学习为目的进行记录. 问题:想用Spring MVC开发一个简单的Web应用, 学习这 ...

  5. tomcat源码导入eclipse步骤

    1. 获取源代码 方式一:从官网http://tomcat.apache.org/download-70.cgi 直接下载,官网提供了Binary 和 Source Code两种下载方式,要研究tom ...

  6. [SAP ABAP开发技术总结]RETURN、STOP、EXIT、CHECK、LEAVE、REJECT

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  7. FZU 2212 Super Mobile Charger(超级充电宝)

    [Description] [题目描述] While HIT ACM Group finished their contest in Shanghai and is heading back Harb ...

  8. git :设置 object-c 的忽略文件

    使用 git 命令行来进行版本控制的时候, 需要设置忽略文件. 这里能找到所有语言的忽略文件的内容:https://github.com/github/gitignore OBJECT的忽略文件内容: ...

  9. Python numpy学习笔记(一)

    下边代码是关于numpy的一些基本用法,包括数组和矩阵操作等... import numpy as np print "<== print version ==>" p ...

  10. iOS - Swift Foundation 框架

    前言 框架是由许多类.方法.函数和文档按照一定的逻辑组织起来的集合,以使研发程序更容易. Foundation 框架:为所有程序开发奠定基础的框架称为 Foundation 框架. Cocoa :是指 ...