hdu 3635 Dragon Balls (带权并查集)
Dragon Balls
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3363 Accepted Submission(s): 1304

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.
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)
T 3 2
Q 2
3 4
T 1 2
Q 1
T 1 3
Q 1
2 3 0
Case 2:
2 2 1
3 3 2
题意:
每一个城市都有一颗龙珠,但是随着时间的推移,龙珠会被移到其他的城市,悟空想去收集这些龙珠,但是他需要你告知他,他要找的那颗龙珠的所在的城市,以及这个城市所拥有的龙珠数量,还有这颗龙珠迁移过多少次。
代码:
#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 (带权并查集)的更多相关文章
- hdu 3635 Dragon Balls(加权并查集)2010 ACM-ICPC Multi-University Training Contest(19)
这道题说,在很久很久以前,有一个故事.故事的名字叫龙珠.后来,龙珠不知道出了什么问题,从7个变成了n个. 在悟空所在的国家里有n个城市,每个城市有1个龙珠,第i个城市有第i个龙珠. 然后,每经过一段时 ...
- hdu3635 Dragon Balls(带权并查集)
/* 题意:有N个城市, 每一个城市都有一个龙珠(编号与城市的编号相同),有两个操作 T A ,B 将标号为A龙珠所在城市的所有的龙珠移动到B龙珠所在城市中! 思路:并查集 (压缩路径的时候将龙珠移动 ...
- hdu 5441 Travel 离线带权并查集
Travel Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5441 De ...
- Hdu 2047 Zjnu Stadium(带权并查集)
Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- How Many Answers Are Wrong (HDU - 3038)(带权并查集)
题目链接 并查集是用来对集合合并查询的一种数据结构,或者判断是不是一个集合,本题是给你一系列区间和,判断给出的区间中有几个是不合法的. 思考: 1.如何建立区间之间的联系 2.如何发现悖论 首先是如何 ...
- hdu 5441 travel 离线+带权并查集
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...
- hdu 2818 Building Block (带权并查集,很优美的题目)
Problem Description John are playing with blocks. There are N blocks ( <= N <= ) numbered ...N ...
- How Many Answers Are Wrong HDU - 3038 (经典带权并查集)
题目大意:有一个区间,长度为n,然后跟着m个子区间,每个字区间的格式为x,y,z表示[x,y]的和为z.如果当前区间和与前面的区间和发生冲突,当前区间和会被判错,问:有多少个区间和会被判错. 题解:x ...
- hdu 3047–Zjnu Stadium(带权并查集)
题目大意: 有n个人坐在zjnu体育馆里面,然后给出m个他们之间的距离, A B X, 代表B的座位比A多X. 然后求出这m个关系之间有多少个错误,所谓错误就是当前这个关系与之前的有冲突. 分析: 首 ...
随机推荐
- 记一次MySql入库后,文本出现乱码的问题
最近采用ADO.NET开发了一个工具,解析了一条如下的日志并入库(MySql) -- :: [INFO] roleName=♣丶伊诺,orderId=,price= 发现入库后的roleName中的♣ ...
- [UVA11464]Even Parity(状压,枚举)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- Metasploit辅助模块
msf > show auxiliary Auxiliary ========= Name Di ...
- poj 1654 Area (多边形求面积)
链接:http://poj.org/problem?id=1654 Area Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- jQuery的deferred对象
应用场景:处理异步任务 看到一篇阮一峰老师的博客挺好的讲的就是jQuery的deferred对象.坦诚讲之前没有怎么用过这个东东呢. 摘其中几点记录下 (1) $.Deferred() 生成一个def ...
- iOS - UISlider
前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UISlider : UIControl <NSCoding> @a ...
- 如何读懂 STATSPACK 报告 (转) & Toad 结合
可与 toad 相结合的内容, 用 这种颜色可以利用 toad(database->monitor->server statistics)查看到下边的很多信息, 比如 wait event ...
- C# 上传RAR文件 解压 获取解压后的文件名称
此方法适用于C盘windows文件夹中有WinRAR.exe文件 if (fileExt.ToUpper() == ".RAR") { string zpath = Server. ...
- 无限极分类,传递一个父ID,返回所有子集
方法: static public function getChildren($data,$pid){ $arr=array(); foreach ($data as $v) { if ($v['pi ...
- paramiko-客户端和服务器认证工具
required: python+pycrypto 1.安装pycrypto 726 cd /opt/ 727 wget http://ftp.dlitz.net/pub/dlitz/crypto/p ...