hdu4607——Park Visit

62 篇文章 0 订阅
16 篇文章 2 订阅

Park Visit

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


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   |   We have carefully selected several similar problems for you:   5065  5064  5062  5061  5060 
 

好在这里的path长度都是1,如果要访问的k个点的数目少于直径上的点的数目,那么路径长度就是k - 1;如果遍历完直径也到不了k个点,那么就需要访问直径以外的点,那么需要走回头路


#include <map>  
#include <set>  
#include <list>  
#include <stack>  
#include <vector>  
#include <queue>  
#include <cmath>  
#include <cstdio>  
#include <cstring>  
#include <iostream>  
#include <algorithm>  
  
using namespace std;  
  
const int N = 100010;  
const int inf = 0x3f3f3f3f;  
  
struct node  
{   
    int next;  
    int to;  
}edge[N << 1];  
  
int n, m;  
int end_p;  
bool vis[N];  
int dist[N];  
int head[N];  
int tot;  
int maxs;  
  
void addedge(int from, int to)  
{  
    edge[tot].to = to;  
    edge[tot].next = head[from];  
    head[from] = tot++;  
}  
  
void build()  
{  
    int u, v;  
    memset( head, -1, sizeof(head) );  
    tot = 0;  
    for (int i = 0; i < n - 1; ++i)  
    {  
        scanf("%d%d", &u, &v); 
        addedge(u, v);  
        addedge(v, u);  
    }  
}  
  
void bfs(int s)  
{  
    queue<int>qu;  
    memset( vis, 0, sizeof(vis) );  
    while ( !qu.empty() )  
    {  
        qu.pop();  
    }  
    qu.push(s);  
    dist[s] = 0;  
    vis[s] = 1;  
    maxs = 0;  
    while ( !qu.empty() )  
    {  
        int u = qu.front();  
        qu.pop();  
        for (int i = head[u]; i != -1; i = edge[i].next)  
        {  
            int v = edge[i].to;  
            if (!vis[v])  
            {  
                vis[v] = 1;  
                dist[v] = dist[u] + 1;  
                qu.push(v);  
                if (maxs < dist[v])  
                {  
                    maxs = dist[v];  
                    end_p = v;  
                } 
            }  
        }  
    }  
}  

int main()
{
    int t, k;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d%d", &n, &m);
        build();
        bfs(1);
        bfs(end_p);
        int cnt = maxs + 1;
        while (m--)
        {
            scanf("%d", &k);
            if(k <= cnt)
            {
                printf("%d\n", k - 1);
                continue;
            }
            printf("%d\n", maxs + 2 * (k - cnt) );
        }
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值