题解报告:hdu 4607 Park Visit(最长路+规律)

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

解题思路:看到这题肯定会想到用树的直径来求解,题目要求从某一点出发,访问连续k个点走过的最少边数。显然这个时候如果k个点都在树的直径上,那么经过的边数一定是最少的且为k-1,否则(即k>maxdist)就会经过树直径外的一些点,每个点被访问两次,因此此时经过的边数最少为maxdist+(k-maxdist-1)*2。

AC代码(499ms):

 1 #include<iostream>
 2 #include<string.h>
 3 #include<cstdio>
 4 using namespace std;
 5 const int maxn=1e5+5;
 6 struct EDGE{int to,next;}edge[maxn<<1];
 7 int t,n,m,k,x,y,cnt,res,maxdist,head[maxn];
 8 void add_edge(int u,int v){
 9     edge[cnt].to=v;
10     edge[cnt].next=head[u];
11     head[u]=cnt++;
12 }
13 int dfs(int u,int fa,int &maxdist){
14     int Dmax=0,Dsec=0;
15     for(int i=head[u];~i;i=edge[i].next){
16         int v=edge[i].to;
17         if(v^fa){
18             int nowd=dfs(v,u,maxdist)+1;
19             if(nowd>Dmax)Dsec=Dmax,Dmax=nowd;
20             else if(nowd>Dsec)Dsec=nowd;
21         }
22     }
23     maxdist=max(maxdist,Dmax+Dsec);
24     return Dmax;
25 }
26 int main(){
27     while(~scanf("%d",&t)){
28         while(t--){
29             scanf("%d%d",&n,&m);
30             memset(head,-1,sizeof(head));cnt=maxdist=0;
31             while(--n){
32                 scanf("%d%d",&x,&y);
33                 add_edge(x,y);
34                 add_edge(y,x);
35             }
36             dfs(1,-1,maxdist);
37             while(m--){
38                 scanf("%d",&k);
39                 if(k<=maxdist)printf("%d\n",k-1);
40                 else printf("%d\n",maxdist+(k-maxdist-1)*2);
41             }
42         }
43     }
44     return 0;
45 }

原文地址:https://www.cnblogs.com/acgoto/p/9572853.html

时间: 2024-08-01 11:35:21

题解报告:hdu 4607 Park Visit(最长路+规律)的相关文章

HDU 4607 Park Visit 求树的直径

Park Visit 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

hdu 4607 Park Visit(树的直径)

Park Visit Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2506    Accepted Submission(s): 1128 Problem Description Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The

HDU 4607 Park visit (求树的直径)

解题思路: 通过两次DFS求树的直径,第一次以任意点作为起点,找到距离该点距离最远的点,则可以证明这个点一定在树的直径上,然后以该点为起点进行DFS得到的最长路就是树的直径. 最后的询问,如果K <= D + 1则可以沿着直径走,距离为K  -  1, 如果K >= D + 1,则需要走直径旁边的分支,每访问一个点距离为2(从直径到这个点,再返回到直径上). #include <iostream> #include <cstring> #include <cstd

HDU 4607 Park Visit

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

HDU 4607 Park Visit(树的直径)

题目大意:给定一棵树,让求出依次访问k个点的最小花费,每条边的权值都为1. 思路:如果能一直往下走不回来,那么这个路径肯定是最小的,这就取决于给定的k,但是怎么确定这个能一直走的长度呢,其实这个就是树的直径,也叫作最长简单路径.找出来这个直径之后,只需和k比较一下就能确定走多少步.设直径为maxx, 如果maxx + 1== k的话,说明刚好不用回来走完最长的这个路,所以当k小于等于maxx + 1的时候就是k-1,当k大于maxx + 1的时候,除了要走完不用回来的路,肯定还要走那些用回来的,

HDU 2196 Computer (树上最长路)【树形DP】

<题目链接> 题目大意: 输出树上每个点到其它点的最大距离. 解题分析: 下面的做法是将树看成有向图的做法,计算最长路需要考虑几种情况. dp[i][0] : 表示以i为根的子树中的结点与i的最大距离 dp[i][1] : 表示以i为根的子树中的结点与u的次大距离 dp[i][2] : 表示i往父亲节点方向走的最大距离 第一就是点 i 在以点 i 为根的子树中的最长距离,这个可以直接在点 i 的子树中求得: 第二就是点 i 朝父亲节点方向的最长距离,这个距离分为三种: 1) 点 i 在以 fa

HDU - 1317 XYZZY (floyd + 最长路)

题目大意:有一种游戏,游戏里面有N个房间,每个房间有相应的能量值,走入该房间就可以得到相应的能量值 现在你要从房间1出发,走到房间N,如果中途能量耗尽了,就表示输了,反之,则为赢 解题思路:首先得判断一下能不能到达N,这可以用Floyd去判断 如果能直接走到N的话,就算赢,否则判断一下,看是否有正环,且正环中有点能到N #include <cstdio> #include <cstring> #include <algorithm> #include <vecto

POJ2240:Arbitrage(最长路+正环)

Arbitrage Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29374   Accepted: 12279 题目链接:http://poj.org/problem?id=2240 Description: Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into

题解报告:hdu 1162 Eddy&#39;s picture

Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result i