2014广州网络预选赛1008(树链剖分)HDU5029
来源:互联网 发布:淘宝招聘网最新招聘 编辑:程序博客网 时间:2024/04/29 17:18
Relief grain
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others)Total Submission(s): 332 Accepted Submission(s): 78
Problem Description
The soil is cracking up because of the drought and the rabbit kingdom is facing a serious famine. The RRC(Rabbit Red Cross) organizes the distribution of relief grain in the disaster area.
We can regard the kingdom as a tree with n nodes and each node stands for a village. The distribution of the relief grain is divided into m phases. For each phases, the RRC will choose a path of the tree and distribute some relief grain of a certain type for every village located in the path.
There are many types of grains. The RRC wants to figure out which type of grain is distributed the most times in every village.
We can regard the kingdom as a tree with n nodes and each node stands for a village. The distribution of the relief grain is divided into m phases. For each phases, the RRC will choose a path of the tree and distribute some relief grain of a certain type for every village located in the path.
There are many types of grains. The RRC wants to figure out which type of grain is distributed the most times in every village.
Input
The input consists of at most 25 test cases.
For each test case, the first line contains two integer n and m indicating the number of villages and the number of phases.
The following n-1 lines describe the tree. Each of the lines contains two integer x and y indicating that there is an edge between the x-th village and the y-th village.
The following m lines describe the phases. Each line contains three integer x, y and z indicating that there is a distribution in the path from x-th village to y-th village with grain of type z. (1 <= n <= 100000, 0 <= m <= 100000, 1 <= x <= n, 1 <= y <= n, 1 <= z <= 100000)
The input ends by n = 0 and m = 0.
For each test case, the first line contains two integer n and m indicating the number of villages and the number of phases.
The following n-1 lines describe the tree. Each of the lines contains two integer x and y indicating that there is an edge between the x-th village and the y-th village.
The following m lines describe the phases. Each line contains three integer x, y and z indicating that there is a distribution in the path from x-th village to y-th village with grain of type z. (1 <= n <= 100000, 0 <= m <= 100000, 1 <= x <= n, 1 <= y <= n, 1 <= z <= 100000)
The input ends by n = 0 and m = 0.
Output
For each test case, output n integers. The i-th integer denotes the type that is distributed the most times in the i-th village. If there are multiple types which have the same times of distribution, output the minimal one. If there is no relief grain in a village, just output 0.
Sample Input
2 41 21 1 11 2 22 2 22 2 15 31 23 13 45 32 3 31 5 23 3 30 0
Sample Output
1223302
题意:RT
思路:树链剖分,比赛的时候不会树链剖分,但是用LCT又yy不出来,只好哭等比赛结束,sad~
今天去看了树链剖分,才豁然开朗啊(LCT也是可做的)
用一个vector维护每条重链即可,如果要在u和v之间路径上的所有点加一个数z
那么就在u和v路径上的所有重链上都将z加上去,因为每条重链上的点转化为线性结构以后在线段上一定是连续的一段区间
那么就转化为了在一个区间里加z,这个可以用经典操作[l,r],在l这里+z,在r+1这里-z,那么前缀和就是该点的值
其中对每条路径的操作复杂度为logn,最后的查询用线段树来维护区间最优值,其中线段树维护的是z,操作只有+1或-1
具体看代码吧~~
0 0
- 2014广州网络预选赛1008(树链剖分)HDU5029
- 树链剖分+线段树+离线(广州网选赛第八题hdu5029)
- 2014上海网络预选赛1003(树链剖分)HDU5044
- HDU5029--Relief grain(树链剖分)
- HDU5029 树链剖分
- hdu5029 树链剖分
- 2014北京网络预选赛1008(线段树区间操作)HDU5039
- 2014牡丹江网络预选赛E题(线段树)zoj3813
- 2014牡丹江网络预选赛D题(状压DP)zoj3812
- 2014牡丹江网络预选赛B题(找规律)zoj3810
- 2014鞍山网络预选赛1006(LCT模板题)hdu5002
- 2014鞍山网络预选赛1004(贪心)hdu5000
- 2014鞍山网络预选赛1005(概率DP)hdu5001
- 2014西安网络预选赛1009(矩阵快速幂)hdu5015
- 2014西安网络预选赛1003(DP+剪枝)hdu5009
- 2014鞍山网络预选赛1010(缩点+高斯消元)hdu5006
- 2014西安网络预选赛1009(点分治)HDU5016
- 2014北京网络预选赛1006(贪心)HDU5037
- 嵌入式 linux(test命令)
- Set 整理
- 图的深度遍历
- 状态压缩递推(States Compressing Recursion,SCR)
- 最方便的远程控制软件有哪些?
- 2014广州网络预选赛1008(树链剖分)HDU5029
- java中int和Integer的区别
- iOS多线程相关操作
- poj 2709 Painter(贪心)
- 内存溢出和内存泄露的分别
- 39 求一个有向连通图的割点
- 搜索分词---产生新词的公式
- 从现在开始,学会长期写一个价值的博客
- android内存泄露的情况