码迷,mamicode.com
首页 > 其他好文 > 详细

题解报告:hdu 4607 Park Visit(最长路+规律)

时间:2018-09-02 11:10:43      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:main   red   you   head   要求   memset   tran   input   nes   

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≤105), 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
解题思路:看到这题肯定会想到用树的直径来求解,题目要求从某一点出发,访问连续k个点走过的最少边数。显然这个时候如果k个点都在树的直径上,那么经过的边数一定是最少的且为k-1,否则(即k>maxdist)就会经过树直径外的一些点,每个点被访问两次,因此此时经过的边数最少为maxdist+(k-maxdist-1)*2。
AC代码(499ms):
 1 #include<iostream>
 2 #include<string.h>
 3 #include<cstdio>
 4 using namespace std;
 5 const int maxn=1e5+5;
 6 struct EDGE{int to,next;}edge[maxn<<1];
 7 int t,n,m,k,x,y,cnt,res,maxdist,head[maxn];
 8 void add_edge(int u,int v){
 9     edge[cnt].to=v;
10     edge[cnt].next=head[u];
11     head[u]=cnt++;
12 }
13 int dfs(int u,int fa,int &maxdist){
14     int Dmax=0,Dsec=0;
15     for(int i=head[u];~i;i=edge[i].next){
16         int v=edge[i].to;
17         if(v^fa){
18             int nowd=dfs(v,u,maxdist)+1;
19             if(nowd>Dmax)Dsec=Dmax,Dmax=nowd;
20             else if(nowd>Dsec)Dsec=nowd;
21         }
22     }
23     maxdist=max(maxdist,Dmax+Dsec);
24     return Dmax;
25 }
26 int main(){
27     while(~scanf("%d",&t)){
28         while(t--){
29             scanf("%d%d",&n,&m);
30             memset(head,-1,sizeof(head));cnt=maxdist=0;
31             while(--n){
32                 scanf("%d%d",&x,&y);
33                 add_edge(x,y);
34                 add_edge(y,x);
35             }
36             dfs(1,-1,maxdist);
37             while(m--){
38                 scanf("%d",&k);
39                 if(k<=maxdist)printf("%d\n",k-1);
40                 else printf("%d\n",maxdist+(k-maxdist-1)*2);
41             }
42         }
43     }
44     return 0;
45 }

 

题解报告:hdu 4607 Park Visit(最长路+规律)

标签:main   red   you   head   要求   memset   tran   input   nes   

原文地址:https://www.cnblogs.com/acgoto/p/9572853.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!