hdu4607—Park Visit(树的直径)

题目链接:传送门

Park Visit

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


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


解题思路:假设树中的最长路为L,需要访问的景点数为N,需要走的最短距离为D.
1.N<=L+1,D = N-1

2.N>L,D = 2*N-L-2


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

using namespace std;

const int N = 20090;
const int M = 100900;

//树的直径:树上的最长简单路
//以某个点u为根搜索,找到到u最长的距离的点,那么改点一定是树中某条最长路的端点
//再以该点为根节点,找到到改点最长的距离


struct Edge{
    int node,len;
    Edge*next;
}m_edge[M*2];
Edge*head[M];
int Ecnt,vis[M],dp[M];

void init()
{
    Ecnt = 0;
    fill( head , head+M , (Edge*)0 );
}

void mkEdge(int a,int b)
{
    m_edge[Ecnt].node = b;
    m_edge[Ecnt].len = 1;
    m_edge[Ecnt].next = head[a];
    head[a] = m_edge+Ecnt++;
}

void dfs(int u)
{
    vis[u] = 1;
    for( Edge*p = head[u] ; p ; p = p->next ){
        int v = p->node;
        if( vis[v] ) continue;
        dp[v] = dp[u]+p->len;
        dfs(v);
    }
}

int main()
{
    int T;
    scanf("%d",&T);
    while( T-- ){
        init();
        int n,m,a,b;
        scanf("%d%d",&n,&m);
        for( int i = 0 ; i < n-1 ; ++i ){
            scanf("%d%d",&a,&b);
            mkEdge(a,b);
            mkEdge(b,a);
        }

        int node,ans = 0;
        memset(dp,0,sizeof(dp));
        memset(vis,0,sizeof(vis));
        dfs(1);
        for( int i = 1 ; i <= n ; ++i ){
            if( dp[i] > ans ){
                ans = dp[i];
                node = i;
            }
        }

        memset(dp,0,sizeof(dp));
        memset(vis,0,sizeof(vis));
        dfs(node);
        ans = 0;
        for( int i = 1 ; i <= n ; ++i )
            ans = max(ans,dp[i]);

        for( int i = 0 ; i < m ; ++i ){
            scanf("%d",&a);
            if( a <= ans+1 ) printf("%d\n",a-1);
            else printf("%d\n",2*a-2-ans);
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值