HDU4607 Park Visit【DFS+树】

86 篇文章 9 订阅
17 篇文章 1 订阅

 

Park Visit

 

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

 

 

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

 

 

Source

2013 Multi-University Training Contest 1

 

 

 

 

问题链接HDU4607 Park Visit

题意简述

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

  输入数据:先输入测试用例数t,每个测试用例数据包括N和M,N为节点数,M为需要查询的上述的k(参观M个景点)。

  输出数据:对于各个M,输出需要走的距离。

问题分析

  n个节点n-1条边构成无向无环树。该问题就是求树直径,树直径即树上最长路径。假设树直径包含m个节点(直径为m-1),若k<=m,则答案为k-1,否则答案为m + (k-m-1)*2。

程序说明

  解法一:

  函数dfs()用于计算树直径经过的节点数。其他都是套路。

  这个程序在穷尽搜索的基础上做了一些剪枝。相比较而言,这个程序速度上会稍微慢一些。

 

  解法二(稍微快一些):

  先随意找出一个结点(程序中是结点1),用函数dfs(),求出1结点到其他各个结点的距离,dist[i]中存放1结点到i结点的距离,从中选出距离1结点最远的结点(程序中放在变量start中)。然后,再以结点start作为起点,用函数dfs(),求出结点start到其他各个结点的距离,从中选出距离start结点最远的结点(程序中放在变量target中),那么从start到target的距离就是树直径。

 

AC的C++语言程序如下(解法二):

 

 

/* HDU4607 Park Visit */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

const int MAXN = 100000;
vector<int>tree[MAXN+1];
int maxlen, dist[MAXN+1];

void dfs(int now, int last, int d[])
{
    int u, size;
    size = tree[now].size();
    for(int i=0; i<size; i++)
        if ((u = tree[now][i]) != last) {
            d[u] = d[now] + 1;
            dfs(u, now, d);
        }
}

int main(void)
{
    int t, n, m, u, v, k;

    scanf("%d", &t);
    while(t--) {
        scanf("%d%d", &n, &m);

        for(int i=1; i<=n; i++)
            tree[i].clear();

        for(int i=1; i<n; i++) {
            scanf("%d%d", &u, &v);
            tree[u].push_back(v);
            tree[v].push_back(u);
        }

        dist[1] = 0;
        dfs(1, 0, dist);

        int start = 0;
        dist[start] = 0;
        for(int i=1; i<=n; i++)
            if(dist[i] > dist[start])
                start = i;

        dist[start] = 0;
        dfs(start, 0, dist);
        int target = 0;
        for (int i=1; i<=n; i++)
            if(dist[i] > dist[target])
                target = i;
        maxlen = dist[target];

        while(m--) {
            scanf("%d", &k);
            if(k <= maxlen)
                printf("%d\n", k-1);
            else
                printf("%d\n", maxlen + (k-maxlen-1)*2);
        }
    }

    return 0;
}

 

 

 

 

 

AC的C++语言程序如下(解法一):

 

/* HDU4607 Park Visit */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

const int MAXN = 100000;
vector<int>tree[MAXN+1];
bool visit[MAXN];

int maxlen;         // 树的最大直径
// DFS求最大树的直径
int dfs(int u)
{
   visit[u] = true;
   int max=0, lastmax=0, size;

   size = tree[u].size();
   for(int i=0; i<size; i++){
        if(visit[tree[u][i]])
            continue;
        int temp = dfs(tree[u][i]);
        if( temp + 1  > max){
            lastmax = max ;
            max = temp + 1;
        }else if( temp + 1 > lastmax)
            lastmax = temp + 1;
        if( maxlen < max + lastmax)
            maxlen = max + lastmax;
   }

   return max;
}

int main(void)
{
    int t, n, m, u, v, k;

    scanf("%d", &t);
    while(t--) {
        scanf("%d%d", &n, &m);

        for(int i=1; i<=n; i++)
            tree[i].clear();

        for(int i=1; i<n; i++) {
            scanf("%d%d", &u, &v);
            tree[u].push_back(v);
            tree[v].push_back(u);
        }

        memset(visit, false, sizeof(visit));
        maxlen = 0;
        dfs(1);

        while(m--) {
            scanf("%d", &k);
            if(k <= maxlen)
                printf("%d\n", k-1);
            else
                printf("%d\n", maxlen + (k-maxlen-1)*2);
        }
    }

    return 0;
}

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值