HDU 4607 Park Visit

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

 qwq ,先求最长链,如果 k < maxe + 1 ,那么就是k-1 , 否则没多1个加2 。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <queue>
 5 using namespace std ;
 6 const int inf = 1 << 30 , maxn = 100000 + 11 ;
 7 int t , n , m , cnt ,  head[maxn] , maxe , vis[maxn] , dis[maxn] ;
 8 struct id
 9 {
10     int to , nxt ;
11 } edge[200011] ;
12 queue< int > Q ;
13 
14 void add( int u , int v )
15 {
16     edge[++cnt].to = v , edge[cnt].nxt = head[u] , head[u] = cnt ;
17 }
18 
19 void Init( )
20 {
21     scanf( "%d%d" , &n ,&m ) ;
22     int u , v ;
23     for( int x = 1 ; x < n ; ++x )
24     {
25         scanf( "%d%d" ,&u , &v ) ;
26         add( u , v ) ; add( v , u ) ;
27     }
28 }
29 
30 void  bfs( int s )
31 {
32     memset( dis , 127/2 , sizeof(dis) ) ;
33     memset( vis , 0 , sizeof(vis) ) ; vis[s] = true ;
34     dis[s] = 0 ; 
35     Q.push( s ) ;
36     while( !Q.empty( ) )
37     {
38         int u = Q.front( ) ; Q.pop( ) ;
39         for( int i = head[u] ; ~i ; i = edge[i].nxt )
40         {
41             int v = edge[i].to ;
42             if( vis[v] ) continue ;
43             vis[v] = true ;
44             if( dis[v] <= dis[u]+1 ) continue ;
45             dis[v] = dis[u] + 1 ;
46             Q.push( v ) ;
47         }
48     }
49 }
50 
51 
52 
53 void Solve( )
54 {
55     bfs( 1 ) ; maxe = 0 ; int v = 1 , maxe = -1 ;
56     for( int x = 1 ; x <= n ; ++x )
57         if( dis[x] > maxe ) maxe = dis[x] , v = x ;
58     bfs( v ) ; 
59     for( int x = 1 ; x <= n ; ++x )
60         if( dis[x] > maxe ) maxe = dis[x] ;
61     int k ; 
62     for( int x = 1 ; x <= m ; ++x )
63     {
64         scanf( "%d" , &k ) ;
65         if( k <= maxe + 1 ) printf( "%d
" , k-1 ) ;
66         else printf( "%d
" , maxe + (k-maxe-1)*2 ) ;
67     }
68 }
69 
70 
71 int main( )
72 {
73 //    freopen( "HDU#4607.in" , "r" , stdin ) ;
74 //    freopen( "HDU#4607.out" , "w" , stdout ) ;
75     scanf( "%d" , &t ) ;
76     while( t-- )
77     {
78         memset( head , -1 , sizeof(head) ) ; 
79         memset( edge , 0 , sizeof( edge ) ) ;
80         cnt = 0 ; Init( ) ;
81         maxe = 0 ; Solve( ) ;
82     }
83 //    fclose( stdin ) ;
84 //    fclose( stdout ) ;
85     return 0 ;
86 }
原文地址:https://www.cnblogs.com/Ateisti/p/5984981.html