2013 多校联合 H Park Visit (hdu 4607)

http://acm.hdu.edu.cn/showproblem.php?pid=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≤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.

思路:我们可以这样考虑,假设我们走的起点为S,终点为T,那么路径S-T上的点我们必须都走到(因为是一棵树),那么如果路径S-T上的点大于或等于k,则我们只需要沿着路径一直走就行,所以最小长度为k-1,否则,我们一定是在路径上的一些点停下,然后转向其他分支然后再回来,此时才能满足一共走过k个点。假设从S到T的路径上一共有num个点,则最后我们要求的答案ans=num-1+?,这个?即为中间点(可以包括S和T)到别的分支再回来所要走的最小距离,因为每个分支互不影响,所以我们可以对每一个分支分开求,因为图为一棵树,所以我们即是求一棵子树从根节点到他的子节点中遍历x点再回来所要走的最短距离,我们可以有观察加数学归纳法证明这个值为2*x,即从一棵树的根结点往他的子树遍历x个点再回来的最小距离为2*x,所以?=2*(k-num),所以答案ans=num-1+(k-num)*2,要是这个值最小,则要使num最大,所以我们只要求得该树的直径即可,以下时代码,求树的直径是个很经典的问题了,这里直接给出代码:


#include <iostream>
#include <string.h>
#include <stdio.h>
#define maxn 100100
using namespace std;
struct edge
{
    int to;
    int next;
}e[maxn<<1];
int box[maxn],cnt;
void init()
{
    cnt=0;
    memset(box,-1,sizeof(box));
}
void add(int from,int to)
{
    e[cnt].to=to;
    e[cnt].next=box[from];
    box[from]=cnt++;
}
int vis[maxn],dist[maxn];
void dfs(int now)
{
    int t,v;
    for(t=box[now];t+1;t=e[t].next)
    {
        v=e[t].to;
        if(dist[v]==-1)
        {
            dist[v]=dist[now]+1;
            dfs(v);
        }
    }
}
int main()
{
   // freopen("dd.txt","r",stdin);
    int ncase;
    scanf("%d",&ncase);
    while(ncase--)
    {
       int n,m;
       scanf("%d%d",&n,&m);
       init();
       int i,j,x,y;
       for(i=1;i<n;i++)
       {
           scanf("%d%d",&x,&y);
           add(x,y);
           add(y,x);
       }
       memset(dist,-1,sizeof(dist));
       dist[1]=0;
       dfs(1);
       int root,ma=0;
       for(i=1;i<=n;i++)
       {
           if(dist[i]>ma)
           {
               ma=dist[i];
               root=i;
           }
       }
       memset(dist,-1,sizeof(dist));
       dist[root]=0;
       dfs(root);
       ma=0;
       for(i=1;i<=n;i++)
       {
           if(dist[i]>ma)
           {
               ma=dist[i];
           }
       }
       ma++;
       while(m--)
       {
            int k;
            scanf("%d",&k);
            if(ma>=k)
            printf("%d\n",k-1);
           else
           {
               printf("%d\n",ma+(k-ma)*2-1);
           }
       }
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值