hdu4607 Park Visit(树的直径)

本文探讨了在Shevchenko公园中,Claire如何利用图论知识和BFS算法,选择从最少入口出发,最小化行走距离的游览路径。通过输入测试案例的数量、节点数和查询数,以及节点间的连接关系,输出每个查询对应的最小行走距离。

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≤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

 

 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

 

转载于:https://www.cnblogs.com/shijianupc/p/3272205.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值