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
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
2010 ACM-ICPC Multi-University Training Contest(19)——Host by HDU
题意:
每一个城市都有一颗龙珠,但是随着时间的推移,龙珠会被移到其他的城市,悟空想去收集这些龙珠,但是他需要你告知他,他要找的那颗龙珠的所在的城市,以及这个城市所拥有的龙珠数量,还有这颗龙珠迁移过多少次。
代码:
1 #define LOCAL
2 #include<cstring>
3 #include<cstdio>
4 #define maxn 10005
5 /*
6 (1) 第i求所在的城市x city[];
7 (2) x城市所拥有的球数量 ball[];
8 (3) i球被转移的次数 cnt[];
9 */
10 int ball[maxn];
11 int city[maxn];
12 int cnt[maxn];
13 int n,q;
14 void init(){
15 for(int i=1 ; i<=n ;i++ ){
16 city[i]=i;
17 ball[i]=1;
18 cnt[i]=0; //转移次数
19 }
20 }
21
22 //搜索该龙珠所在城市的位置
23 int fin(int x)
24 {
25 //int tem;
26 if(x==city[x])
27 return city[x];
28 int tem=city[x];
29 city[x]=fin(city[x]);
30 cnt[x]+=cnt[tem];
31 return city[x];
32 }
33
34 void Union(int x,int y)
35 {
36 /*
37 将 x城市的所有龙珠转移到y城市中
38 */
39 x=fin(x);
40 y=fin(y);
41 if(x!=y){
42 city[x]=city[y];
43 ball[y]+=ball[x];
44 ball[x]=0; //球全部移动到y城市中
45 cnt[x]=1; //第一次移动
46 }
47 }
48
49 int main()
50 {
51
52 int t,a,b,tt=0;
53 char str[2];
54 scanf("%d",&t);
55 while(t--){
56 scanf("%d%d",&n,&q);
57 init();
58 printf("Case %d:n",++tt);
59 while(q--){
60 scanf("%s",str);
61 if(str[0]=='T'){
62 scanf("%d%d",&a,&b);
63 Union(a,b);
64 }
65 else
66 {
67 scanf("%d",&a);
68 b=fin(a);
69 printf("%d %d %dn",b,ball[b],cnt[a]);
70 }
71 }
72 }
73 return 0;
74 }
- 糖大夫--测量流程性能监控自动化方案设计
- ReactNative调用Android原生模块
- jvm调优的工具介绍
- Python时间序列预测案例研究:巴尔的摩年度用水量
- [一对一课程] 之 设计并实现第一个JS模块?
- React Native 使用react-native-image-picker库实现图片上传功能
- 再谈Android动态链接库
- React Native之Permissions权限适配
- React Native项目实战之fetch请求并填充界面
- CocoaPods使用详解
- 2018年伊始,系统编程语言Rust为何令程序员感到兴奋?
- React Native项目实战之搭建美团个人中心界面
- React Native动画Animated详解
- iOS动画总结
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Rman备份恢复和管理
- 重做日志和日志挖掘
- 『技术随手学』解决windows与ubuntu平台 CondaHTTPError: HTTP 000 CONNECTION FAILED 问题
- DevOps编程操练:用Jenkins流水线建立代码质量预警机制
- 『技术随手学』pip conda 替换清华源 Windows与Ubuntu通用
- 回滚段undo
- 『AI实践学』测试深度学习框架GPU版本是否正确安装方法:TensorFlow,PyTorch,MXNet,PaddlePaddle
- 使用Github管理Hexo博客的源文件
- oracle的userenv和nls_lang详解
- 打卡群刷题总结0925——最佳买卖股票时机含冷冻期
- 备忘:美化pymol作图1
- 宿舍(寝室)管理系统设计与实现 | 附 演示、源码地址
- Oracle字符集检查和修改
- Vue3 DOM Diff 核心算法解析
- PHP的LZF压缩扩展工具