首页 > 代码库 > HDU5102(树的前k路径+队列)
HDU5102(树的前k路径+队列)
The K-th Distance
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 332 Accepted Submission(s): 90
Problem Description
Given a tree, which has n node in total. Define the distance between two node u and v is the number of edge on their unique route. So we can have n(n-1)/2 numbers for all the distance, then sort the numbers in ascending order. The task is to output the sum of the first K numbers.
Input
There are several cases, first is the number of cases T. (There are most twenty cases).
For each case, the first line contain two integer n and K (2≤n≤100000,0≤K≤min(n(n?1)/2,106) ). In following there are n-1 lines. Each line has two integer u , v. indicate that there is an edge between node u and v.
For each case, the first line contain two integer n and K (
Output
For each case output the answer.
Sample Input
2 3 3 1 2 2 3 5 7 1 2 1 3 2 4 2 5
Sample Output
4 10
Source
BestCoder Round #17
题意:给出一棵树,边权为1,求前k长的路径和
思路:用一个队列维护即可,先将所有边入队,然后不停的用边的一个端点向外扩张,每扩张一次,路径长度就+1
这样求出前k个即可
<script src="https://code.csdn.net/snippets/513627.js" type="text/javascript"></script>
HDU5102(树的前k路径+队列)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。