ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HDU 4607 - Park Visit

2019-02-23 16:51:18  阅读:250  来源: 互联网

标签:HDU le int visit Park Claire each 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 \le 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 \le N,M \le 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 \le K \le 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

Resume

在给定的边权值为1的树上,求出遍历M个点的最短路径。

Analysis

注意观察这棵树的特点——边权值为1。那么我们想要用最短的路径去走完这些点,就要尽量不走回头路,也就是说我们需要找出树上最长的链——树的直径
那么当要参观的点的数量K不多于直径所含点数D时,答案就是\(K-1\)。
而当\(K \gt D\)时,我们需要参观直径之外的点,既然要去,就要回来,所以每额外参观一个点,就要多走2的距离,也就是说答案应该为 \(D - 1 + 2*(K-D) = 2*K - 1 - D\)。

Code

//////////////////////////////////////////////////////////////////////
//Target: HDU 4607 - Park Visit
//@Author: Pisceskkk
//Date: 2019-2-21
//////////////////////////////////////////////////////////////////////

#include<cstdio>
#include<cstring>
#define N (int)1e5+10
using namespace std;

int t,n,m;
int q[N],d[N];
int he[N];

struct edge{
    int to,ne;
}e[N<<1];

void add_2(int x,int y,int i){
    i<<=1;
    e[i].ne = he[x];
    he[x] = i;
    e[i].to = y;
    i++;
    e[i].ne = he[y];
    he[y] = i;
    e[i].to = x;
}

int bfs(int s){
    int h=0,t=0;
    memset(d,0,sizeof(d));
    memset(q,0,sizeof(q));
    q[h++]=s;
    d[s] = 1;
    while(t<h){
        int x=q[t++],y;
        for(int i=he[x];i;i=e[i].ne){
            y = e[i].to;
            if(!d[y]){
                d[y] = d[x]+1;
                q[h++]=y;
            }
        }
    }
    return q[t-1];
}

int main(){
    scanf("%d",&t);
    while(t--){
        memset(e,0,sizeof(e));
        memset(he,0,sizeof(he));
        scanf("%d %d",&n,&m);
        int x,y;
        for(int i=1;i<n;i++){
            scanf("%d %d",&x,&y);
            add_2(x,y,i);
        }
        x = d[bfs(bfs(1))];
        while(m--){
            scanf("%d",&y);
            if(y<=x){
                printf("%d\n",y-1);
            }
            else {
                printf("%d\n",x + (y-x)*2 -1);
            }
        }
    }

}

标签:HDU,le,int,visit,Park,Claire,each,4607
来源: https://www.cnblogs.com/pisceskkk/p/10423177.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有