博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 3635 并查集
阅读量:6241 次
发布时间:2019-06-22

本文共 3077 字,大约阅读时间需要 10 分钟。

题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=3635

Dragon Balls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2273    Accepted Submission(s): 888

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
 
 
分析:
本题主要是用到并查集。 难点为,  怎么保存转移次数。 考虑并查集的路径压缩, 就是通过路径压缩来更新转移的次数,比如每次移动时,我只需要把这个城市的根结点的转移次数+1,等到以后路径压缩时,子结点自己移动的次数加上根结点移动的次数,就是这个结点总共的移动次数。
 
 
代码如下:
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #define N 10005 8 using namespace std; 9 struct node10 {11 int parent; // 根节点12 int son; // 子节点13 int rank; // 转移次数14 }p[N];15 int n;16 void init()17 {18 for(int i=1;i<=n;i++)19 {20 p[i].parent = i;21 p[i].rank =0;22 p[i].son = 1;23 }24 }25 int Find(int x)26 {27 if(x== p[x].parent) return x;28 int temp_parent=p[x].parent;29 p[x].parent= Find(p[x].parent);30 p[x].rank+=p[temp_parent].rank; //因为,根节点连到另一个新的根节点后,根节点有变化,故需要更新x到新根节点的转移次数31 return p[x].parent;32 }33 void Union(int x, int y)34 {35 int xf=Find(x);36 int yf=Find(y);37 p[xf].parent=yf;38 p[xf].rank++; 39 p[yf].son+=p[xf].son;40 p[xf].son=0;41 }42 int main()43 {44 int t,count=1;45 scanf("%d",&t);46 while(t--)47 {48 int k,x,y;49 char c;50 printf("Case %d:\n",count++);51 scanf("%d%d",&n,&k);52 init();53 for(int i=0;i

 

转载于:https://www.cnblogs.com/zn505119020/p/3580055.html

你可能感兴趣的文章
重拾Java(8)-反射
查看>>
有没有可以共享的桌面便签?
查看>>
Mars说光场(3)— 光场采集
查看>>
24、商品列表页之数据渲染和传值
查看>>
源码分析-react3-创建dom
查看>>
C# 获取QQ好友列表信息的实现
查看>>
System.ComponentModel.Win32Exception解决方案
查看>>
设计模式之死磕策略模式(原创)
查看>>
IDEA无法导入Maven工程
查看>>
谈谈FLUX的使用
查看>>
4-Java面向对象-继承(上)
查看>>
mysql 生成随机手机号和随机中文名
查看>>
磁盘 IO 和网络 IO 该如何评估、监控、性能定位和优化?
查看>>
【iOS 开发】父视图外部子视图点击响应 - hitTest
查看>>
Android仿简书长按文章生成图片效果
查看>>
linux安装redis内存数据库
查看>>
官方实锤!微软宣布以 75 亿美元收购 GitHub
查看>>
vuejs深入浅出—基础篇
查看>>
View的绘制过程
查看>>
AI助力机器人市场,机器人升级2.0迫在眉睫
查看>>