[2016-01-27][HDU][4607][Park Visit]

[2016-01-27][HDU][4607][ Park Visit ]
E - Park Visit
Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  HDU 4607

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
  • 时间:2016-01-21  13:57:06  星期四
  • 题目编号:HDU 4607
  • 题目大意:给出若干个旅游景点的边,要旅游k个景点(保证是树),求最短的距离
  • 方法:
    • 求树的直径,则直径对应的路径,表示可以一次走到的路,
    • 如果直径大于 k.(嗯,应该是把),那么值需要往返一次(也就是2的长度),就可以访问一个景点
    • 以此计算
  • 解题过程遇到问题:  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using  namespace  std;
const  int  maxn = 1E5 + 10;
int  n,m;
int  diameter;
int  p;
vector< int > v[maxn];
int  d[maxn];
int  vis[maxn];
void  dfs( int  cur,  int  ct)
{
     int  k = 0;
     int  size = v[cur].size();
     int  tmp;
     for ( int  i = 0;i <  size;i++)
     {
         tmp = v[cur][i];
         if (  !vis[tmp])
         {
             vis[tmp] = 1;
             dfs(tmp,ct + 1);
             k++;
         }
     }
     if (k == 0)
     {
         if (diameter < ct)
         {
             diameter = ct;
             p = cur;
         }
     
     }
}
int  main()
{
     int  t;
     scanf ( "%d" ,&t);
     while (t--)
     {
         scanf ( "%d%d" ,&n,&m);
         int  a,b;
         for ( int  i = 1; i <= n; i++) 
             v[i].clear(); 
         for ( int  i = 0;i < n - 1;i++)
         {
             scanf ( "%d%d" ,&a,&b);
             v[a].push_back(b);
             v[b].push_back(a);
         }
 
         diameter = 0;p = 1;
         memset (vis,0, sizeof (vis));
         vis[1] = 1;
         dfs(1,1);
 
         memset (vis,0, sizeof (vis));
         vis[p] = 1;
         dfs(p,1);
         int  q;
         for ( int  i = 0;i < m;i++)
         {
             scanf ( "%d" ,&q);
             if (q <= diameter)
                 printf ( "%d\n" ,q - 1);
             else  {
                 printf ( "%d\n" , diameter - 1 + (q - diameter)*2);
             }
         }
 
     }
     return  0;
}




来自为知笔记(Wiz)


你可能感兴趣的:([2016-01-27][HDU][4607][Park Visit])