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个关系之间有多少个错误,所谓错误就是当前这个关系与之前的有冲突. 分析: 首 ...
随机推荐
- JAVA语言基础——类型转换
1.自动类型转换 自动类型转换又称作隐式转换,可以将一个int变量转换为float类型. public class Conver { public static void main(String[] ...
- 0(Mark)(随时添加) ubuntu的一些 终端 命令整理
MARK 1 查看cpu信息cat /proc/cpiinfo 2 查看ubuntu版本:cat /etc/issue 3 查看系统是32位还是64位方法1:#查看long的位数,返回32或64 ge ...
- Create XO Checker Game With Oracle Forms
Created XO Checker game in Oracle Forms and sharing its FMB (source code) for reference so that you ...
- 快速查看SQL Server 中各表的数据量以及占用空间大小
快速查看SQL Server 中各表的数据量以及占用空间大小. CREATE TABLE #T (NAME nvarchar(100),ROWS char(20),reserved varchar(1 ...
- Sbt的使用初步和用sbt插件生成eclipse工程
以前一直是用maven去管理java项目,现在开始写scala项目了但是在scala-ide中去编译scala项目和sbt的区别一直没弄清楚受到文章:http://my.oschina.net/yjw ...
- [poj3349]Snowflake Snow Snowflakes(hash)
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 37615 Accepted: ...
- [转]linux中如何安装软件?
Linux下软件的安装与卸载 在Windows下安装软件时,只需运行软件的安装程序(setup.install等)或者用zip等解压缩软件解开即可安装,运行反安装程序 (uninstall.u ...
- [转载] DevOps年中盘点:国外最受欢迎的10篇技术文章
本文根据高效运维系列微信群的群友投稿整理而成.“高效运维”公众号作为本系列群的官方唯一公众号,原创并独家首发. 欢迎关注“高效运维”公众号,以免费参加「运维讲坛」每月一次的线下交流活动:并抢先赏阅干货 ...
- poj3608Bridge Across Islands(凸包间最小距离)
链接 以下所有文章均转载( http://blog.csdn.net/acmaker/article/details/3176910) 转载请注明出处! 考虑如下的算法, 算法的输入是两个分别有 m ...
- golang type 和断言 interface{}转换
摘要 类型转换在程序设计中都是不可避免的问题.当然有一些语言将这个过程给模糊了,大多数时候开发者并不需要去关 注这方面的问题.但是golang中的类型匹配是很严格的,不同的类型之间通常需要手动转换,编 ...