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个关系之间有多少个错误,所谓错误就是当前这个关系与之前的有冲突. 分析: 首 ...
随机推荐
- block 实现原理详解(一)
对于大多数人来讲,block内部到底是怎样实现的呢?我们可以借助clang将其编译成为c++的代码,就可以看出,block到底是什么东西, 先来看这样一个问题, <!-- lang: cpp - ...
- Maven 3.3.9在Windows上的安装
开始学Maven了,可是我一个项目都木有做过.听过Maven 的大名,用来构建项目的. 下面记录下我安装Maven的过程 1.确认电脑上安装了JDK 在cmd下执行下列命令: java –versio ...
- FineReport: 参数为空选出全部值(按条件查询,空条件时直接过滤,不进行查询。。)
在Java报表软件FineReport中,选择特定的参数(如下图中的姓名.身份证号等)后,会返回我们要查询的数据,然而假如没有输入参数值,我们却仍需要返回数据时该怎样处理呢?应该过滤掉这个条件,不按这 ...
- python_way day15 HTML-DAY2 HTML-DAY2、JS
python_way day15 HTML-DAY2 html-css回顾 javascript 一.html-css回顾 1.input与+,-号的写法 <!DOCTYPE html> ...
- STORM_0008_Structure-of-the-codebase_Storm的代码库的结构
http://storm.apache.org/releases/1.0.1/Structure-of-the-codebase.html Structure of the codebase 源码分成 ...
- Windows Internals学习笔记(三)Procdump的使用
参考资料: 1. 下载地址 2. 使用示例
- 对称加密DES和TripleDES
一. 对称加密 对称加密,是一种比较传统的加密方式,其加密运算.解密运算使用的是同样的密钥,信息的发送者和信息的接收者在进行信息的传输与处理时,必须共同持有该密码(称为对称密码).因此,通信双方都必 ...
- LINQ之路 3:C# 3.0的语言功能(下)
在LINQ介绍一篇中,我们已经看到了隐式类型变量var,扩展方法(Extension method)和Lambda表达式的身影.没错,他们正是LINQ技术的基石,是他们让LINQ的实现成为可能,并且简 ...
- 如何设置table中<tr>和<td>的高度
//-----------------自定义表格table的行和列的宽和高----------------------// 先设置一个样式 如下: <style type="text/ ...
- 教你如何精通Struts:Tiles框架
Tiles框架特性和内容 Tiles框架为创建Web页面提供了一种模板机制,它能将网页的布局和内容分离.它允许先创建模板,然后在运行时动态地将内容插入到模板中.Tiles 框架建立在JSP的inclu ...