jianupc

 

hdu4607 Park Visit(树的直径)

Park Visit

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1826    Accepted Submission(s): 798

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
 
Source
 
Recommend
liuyiding

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <cmath>
 6 using namespace std;
 7 const int N = 100005;
 8 int n,m;
 9 int head[N];
10 struct node {
11     int v,w,next;
12 }edge[N<<1];
13 int cnt;
14 void add(int u, int v, int w) {
15     edge[cnt].v = v;
16     edge[cnt].w = w;
17     edge[cnt].next = head[u];
18     head[u] = cnt++;
19 }
20 int vis[N];
21 int dist[N];
22 int que[N];
23 int ret;
24 int bfs(int u) {
25     memset(vis, 0, sizeof(vis));
26     int start = 0;
27     int rear = 1;
28     que[1] = u;
29     vis[u] = 1;
30     dist[u] = 0;
31     int i;
32     int ans = 0;
33     while (start < rear) {
34         start++;
35         int tmp = que[start];
36         for (i = head[tmp]; i != -1; i = edge[i].next) {
37             int v = edge[i].v;
38             if (!vis[v]) {
39                 rear++;
40                 que[rear] = v;
41                 vis[v] = 1;
42                 dist[v] = dist[tmp] + edge[i].w;
43                 if (dist[v] > ans) {
44                     ans = dist[v];
45                     ret = v;
46                 }
47             }
48         }
49     }
50     return ans;
51 }
52 int main() {
53 //    freopen("in.txt","r",stdin);
54     int i;
55     int u,v;
56     int T;
57     scanf("%d",&T);
58     while (T--) {
59         scanf("%d %d",&n,&m);
60         memset(head, -1, sizeof(head));
61         cnt = 0;
62         for (i = 1; i < n; i++) {
63             scanf("%d %d",&u,&v);
64             add(u,v,1);
65             add(v,u,1);
66         }
67         bfs(1);
68         int ans = bfs(ret);
69         for (i = 0; i < m; i++) {
70             int t;
71             scanf("%d",&t);
72             if (t <= ans + 1)
73                 printf("%d\n", t - 1);
74             else
75                 printf("%d\n", ans + (t - ans - 1) * 2);
76         }
77     }
78     return 0;
79 }
View Code

 

posted on 2013-08-21 11:04  shijianupc  阅读(195)  评论(0编辑  收藏  举报

导航