HDU 4607 Park Visit(树的直径)

个人博客→TanJX的自留地

Park Visit

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

题意

莱克尔和她的朋友到公园玩,公园很大也很漂亮。公园包含n个景点通过n-1条边相连。克莱尔太累了,所以不能去参观所有点景点。经过深思熟虑,她决定只访问其中的k个景点。她拿出地图发现所有景点的入口都很特殊。所以她想选择一个入口,并找到一条最短的路来参观k个景点。假设景点之间的距离为1。

题解

树的直径,两遍dfs跑出来的最大距离一定是直径。虽然说正在学树形DP,而且这题可以用,但是不自觉的就去敲了DFS…

代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>

using namespace std;
#define me(x,y) memset(x,y,sizeof x);

typedef long long ll;
const int maxn = 100100;
const int INF = 0x3f3f3f3f;
const int MOD = 100;
int n,m,cnt;
int head[maxn],d[maxn];
struct Edge{
    int to,next;
} e[maxn*2];

void add(int u,int v){
    e[++cnt].to = v;
    e[cnt].next = head[u];
    head[u] = cnt;
}

void dfs(int u,int last){
    for(int i = head[u]; i != -1; i = e[i].next){
        int v = e[i].to;            //智障的我手快把这里的i写成了u找bug找了一个小时
        if(v != last){
            d[v] = d[u] + 1;
            dfs(v,u);
        }
    }
}

int main()
{
    int t;
    cin>>t;
    while(t--){
        cin>>n>>m;
        me(head,-1);
        cnt = 0;
        for(int i = 1; i < n; i++){
            int u,v;
            scanf("%d%d",&u,&v);
            add(u,v);
            add(v,u);
        }
        d[1] = 0;
        dfs(1,0);
        int root = 0;
        d[root] = 0;
        for(int i = 1; i <= n; i++){
            if(d[root] < d[i]){
                root = i;
            }
        }
        d[root] = 0;
        dfs(root,0);
        int len = 0;
        for(int i = 1; i <= n; i++){
            len = max(len,d[i]);
        }
        for(int i = 1; i <= m; i++){
            int k;
            scanf("%d",&k);
            printf("%d\n",(k <= len) ? k-1 : len+(k-1-len)*2);
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值