hdu4607

点击打开链接

Park Visit      

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


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
题意:在一个游乐园,有n个景点,景点之间有一些双向路径,长度都为1,问游玩k个景点至少要走多少路程
n个景点n-1条边,可见这个图是一个树,那么要走k个景点至少走多少路,就要尽可能少的走回头路,如果k个景点在一条直线,那是最好不过了走k-1的路程就够了,多以我们让他尽量走一个链状的路线,少走分叉的路线,那么我们从这受到启发,走树上最远距离,即树的直径,这些
边只走一次,假如树的直径为m,那么这个直径上有m+1 个景点,如果k小于等于m+1,就走k-1的路程啦,如果k>m+1,就要走分叉的路径了,这些分叉路径都要走两次,去一次回一次,因为要保证走的树的直径的路线上的路径都是只走一次,所以你去了分叉的路径,还要回来走我们
的”大路”啊. 那么要在 m 的基础上加上(k-m-1)*2的路径长度;
求树的直径: 两遍BFS :先任选一个起点BFS找到最长路的终点,再从终点进行BFS,则第二次BFS找到的最长路即为树的直径;
              原理: 设起点为u,第一次BFS找到的终点v一定是树的直径的一个端点
              证明: 1) 如果u 是直径上的点,则v显然是直径的终点(因为如果v不是的话,则必定存在另一个点w使得u到w的距离更长,则于BFS找到了v矛盾)
                      2) 如果u不是直径上的点,则u到v必然于树的直径相交(反证),那么交点到v 必然就是直径的后半段了
                       所以v一定是直径的一个端点,所以从v进行BFS得到的一定是直径长度

ac代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=1e6+10;
struct Edge
{
    int to,next,cap;
}edge[maxn*2];
int tot,head[maxn];
void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
}
void add_edge(int u,int v)
{
    edge[tot].to=v;
    edge[tot].next=head[u];
    head[u]=tot++;
}
int vis[maxn],dis[maxn];
queue<int>que;
int tree(int s)
{
    while(!que.empty())
    {
        que.pop();
    }
    int d,maxd=-9;
    int u,v;
    memset(vis,0,sizeof(vis));
    vis[s]=1;
   // for()
    dis[s]=0;

    que.push(s);
    while(!que.empty())
    {
         u=que.front();
        que.pop();
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            v=edge[i].to;
            if(!vis[v])
            {
                vis[v]=1;
                dis[v]=dis[u]+1;
                if(dis[v]>maxd)
                {
                    maxd=dis[v];
                    d=v;
                }
                 que.push(v);
            }
        }
    }

    while(!que.empty())
        que.pop();


    memset(vis,0,sizeof(vis));
    vis[d]=1;
    dis[d]=0;
    maxd=-9;
    que.push(d);
    while(!que.empty())
    {
        u=que.front();
        que.pop();
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            v=edge[i].to;
            if(!vis[v])
            {
                vis[v]=1;
                dis[v]=dis[u]+1;
                if(dis[v]>maxd)
                    maxd=dis[v];
                que.push(v);
            }
        }
    }
    return maxd;

}
int main()
{
    int t;
    int n,m,k;
    scanf("%d",&t);
    while(t--)
    {
        int u,v;
        init();
        scanf("%d%d",&n,&m);
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&u,&v);
            add_edge(u,v);
            add_edge(v,u);
        }
        int d=tree(1);
        d++;
        for(int i=0;i<m;i++)
        {
            scanf("%d",&k);
            if(k<=d)
            {
                printf("%d\n",k-1);
            }
            else
            {
                printf("%d\n",(k-d)*2+d-1);
            }
        }
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值