[原创]HDU 4366 Successor [树形转线形+线段树]【数据结构+技巧】
[原创]HDU 4366 Successor [树形转线形+线段树]【数据结构+技巧】
2017-03-03 16:05:08 Tabris_ 阅读数:265
博客爬取于2020-06-14 22:41:30
以下为正文
版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/60141601
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4366
---------------------------------------------------------------------------------------------------------------.
Successor
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 4173 Accepted Submission(s): 1032
Problem Description
Sean owns a company and he is the BOSS.The other Staff has one Superior.every staff has a loyalty and ability.Some times Sean will fire one staff.Then one of the fired man’s Subordinates will replace him whose ability is higher than him and has the highest loyalty for company.Sean want to know who will replace the fired man.
Input
In the first line a number T indicate the number of test cases. Then for each case the first line contain 2 numbers n,m (2<=n,m<=50000),indicate the company has n person include Sean ,m is the times of Sean’s query.Staffs are numbered from 1 to n-1,Sean’s number is 0.Follow n-1 lines,the i-th(1<=i<=n-1) line contains 3 integers a,b,c(0<=a<=n-1,0<=b,c<=1000000),indicate the i-th staff’s superior Serial number,i-th staff’s loyalty and ability.Every staff ‘s Serial number is bigger than his superior,Each staff has different loyalty.then follows m lines of queries.Each line only a number indicate the Serial number of whom should be fired.
Output
For every query print a number:the Serial number of whom would replace the losing job man,If there has no one to replace him,print -1.
Sample Input
1
3 2
0 100 99
1 101 100
1
2
Sample Output
2
-1
Author
FZU
Source
2012 Multi-University Training Contest 7
----------------------------------------------------------------------------------------------------------------.
题目大意:
给一个公司的上下级关系图,每个员工有一个上级,一个忠诚度,一个能力值,
现在老板(0),要开除一个员工,然后在这个员工的直接或间接能力值大于这个员工下级中找忠诚度最大的人来代替这个员工,如果有输出代替的员工的编号,没有输出-1
解题思路:
首先,要明白如何将一个数形转化为线形
其实就是从根节点进行搜索,
然后向下dfs遍历树,依次进行编号,
同时能保证子树的编号一定大于父节点的编号,
同时借用两个数组,$L[_],R[_]u\big(L[u],R[u]\big),开区间$ 内。
这样在进行对子树 进行的操作的时候 可以借助数据结构 对区间进行查找,
然后将员工按能力值排序,
依次将一个个员工插入到线段树中,单点更新即可,
找这个员工下属忠诚度的最大的,直接区间查询就行了,
因为我们已经将树形转化成了线形,所以能够确保结果的正确性
然后注意下代码细节就好了。
操他妈的 两处笔误,卡了2个小时
1 sort (+1,+n +1)
2 j打成i
怎么这么菜!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
附本题代码
----------------------------------------------------------------------------------------------------------------.
1 | # include <bits/stdc++.h> |