博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(DP求最长路) hdu 4607
阅读量:4518 次
发布时间:2019-06-08

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

Park Visit

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2624    Accepted Submission(s): 1178

Problem Description
Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The park is beautiful - but large, indeed. N feature spots in the park are connected by exactly (N-1) undirected paths, and Claire is too tired to visit all of them. After consideration, she decides to visit only K spots among them. She takes out a map of the park, and luckily, finds that there're entrances at each feature spot! Claire wants to choose an entrance, and find a way of visit to minimize the distance she has to walk. For convenience, we can assume the length of all paths are 1.
Claire is too tired. Can you help her?
 

 

Input
An integer T(T≤20) will exist in the first line of input, indicating the number of test cases.
Each test case begins with two integers N and M(1≤N,M≤10
5), which respectively denotes the number of nodes and queries.
The following (N-1) lines, each with a pair of integers (u,v), describe the tree edges.
The following M lines, each with an integer K(1≤K≤N), describe the queries.
The nodes are labeled from 1 to N.
 

 

Output
For each query, output the minimum walking distance, one per line.
 

 

Sample Input
1 4 2 3 2 1 2 4 2 2 4
 

 

Sample Output
1 4
 

 

Source
 

 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:            
 
 
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;vector
e[100005];int n,m,dist[100005],k;void dfs(int u,int father){ for(int i=0;i
len) { len=dist[i]; u=i; } } memset(dist,0,sizeof(dist)); dfs(u,-1); len=0; for(int i=1;i<=n;i++) { if(dist[i]>len) len=dist[i]; } len++; while(m--) { scanf("%d",&k); if(len>=k) { printf("%d\n",k-1); } else { printf("%d\n",(k-len)*2+len-1); } } } return 0;}

  

转载于:https://www.cnblogs.com/water-full/p/4493956.html

你可能感兴趣的文章
Python matplot画散列图
查看>>
C#/.NET整数的三种强制类型转换(int)、Convert.ToInt32()、int.Parse()的区别
查看>>
算法复习——数位dp(不要62HUD2089)
查看>>
PhpSpreadsheet如何读取excel文件
查看>>
如何选购一款好的人事档案管理系统
查看>>
Spark2.1.0——运行环境准备
查看>>
[转载]C#异步调用四大方法详解
查看>>
在windows下添加php的Imagick扩展
查看>>
python3 爬取百合网的女人们和男人们
查看>>
kubernetes源码阅读笔记——Kubelet(之三)
查看>>
如何利用jQuery post传递含特殊字符的数据
查看>>
中国剩余定理
查看>>
Linux 攻击防护基础排查
查看>>
Codeforces 543.B Destroying Roads
查看>>
noip模拟赛 寻宝之后
查看>>
洛谷P1461 海明码 Hamming Codes
查看>>
ZOJ2833*(并查集)
查看>>
外连接简要总结
查看>>
第一次作业-准备篇
查看>>
【C++】继承时构造函数和析构函数
查看>>