HDU 4607 Park Visit (树的直径)

题意:

一棵树上n 个结点, 问你m个询问, 每个询问问你访问树上x 个结点最少走几步?

思路:

先求出树的直径的结点个树num。

如果x <= num, 直接走直径即可。

否则 肯定是num 个点走直径,剩下的点 离开直径在回到直径, 乘以2即可。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

const int maxn = 1e5+10;

int dp[maxn];
queue<int>q;
bool vis[maxn];
vector<int>g[maxn];
void bfs(int x){
    memset(dp,0,sizeof dp);
    memset(vis,0,sizeof vis);
    vis[x] = 1;
    q.push(x);
    while(!q.empty()){
        int u = q.front(); q.pop();
        for (int i = 0; i < g[u].size(); ++i){
            int v = g[u][i];
            if (vis[v]) continue;
            dp[v] = max(dp[v], dp[u] + 1);
            q.push(v);
            vis[v] = 1;
        }
    }
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        int n,m;
        scanf("%d %d",&n, &m);
        for (int i = 1; i <= n; ++i){
            g[i].clear();

        }
        for (int i = 1; i < n; ++i){
            int u,v;
            scanf("%d %d",&u, &v);
            g[u].push_back(v);
            g[v].push_back(u);
        }
        bfs(1);
        int ans = 0, s;
        for (int i = 1; i <= n; ++i){
            if (ans < dp[i]){
                ans = dp[i];
                s = i;
            }

        }
        bfs(s);
        ans = 0;
        for (int i = 1; i <= n; ++i){
            ans = max(ans, dp[i]);
        }

        int num = ans + 1;
        while(m--){
            int x;
            scanf("%d",&x);
            if (x <= num){
                printf("%d\n", x-1);

            }
            else printf("%lld\n", (long long)num-1LL + (long long)(x-num)*2LL );

        }
    }

    return 0;
}

Park Visit

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


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:   6032  6031  6030  6029  6028 
 

Statistic |  Submit |  Discuss |  Note

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值