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)就会经过树直径外的一些点(有k-maxdist个),并且剩下的这些点都被访问两次,而此时经过的边数最少为maxdist+(k-maxdist-1)*2。
AC代码一(499ms):一次dfs。
 #include<iostream>
#include<string.h>
#include<cstdio>
using namespace std;
const int maxn=1e5+;
struct EDGE{int to,next;}edge[maxn<<];
int t,n,m,k,x,y,cnt,res,maxdist,head[maxn];
void add_edge(int u,int v){
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int dfs(int u,int fa,int &maxdist){
int Dmax=,Dsec=;
for(int i=head[u];~i;i=edge[i].next){
int v=edge[i].to;
if(v^fa){
int nowd=dfs(v,u,maxdist)+;
if(nowd>Dmax)Dsec=Dmax,Dmax=nowd;
else if(nowd>Dsec)Dsec=nowd;
}
}
maxdist=max(maxdist,Dmax+Dsec);
return Dmax;
}
int main(){
while(~scanf("%d",&t)){
while(t--){
scanf("%d%d",&n,&m);
memset(head,-,sizeof(head));cnt=maxdist=;
while(--n){
scanf("%d%d",&x,&y);
add_edge(x,y);
add_edge(y,x);
}
dfs(,-,maxdist);
while(m--){
scanf("%d",&k);
if(k<=maxdist)printf("%d\n",k-);
else printf("%d\n",maxdist+(k-maxdist-)*);
}
}
}
return ;
}

AC代码二(546ms):两次bfs。

 #include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+;
struct EDGE{int to,next;}edge[maxn<<];
struct node{
int u,dep;
node(int x,int y):u(x),dep(y){}
};
int t,n,m,x,y,k,cnt,maxdep,maxvex,head[maxn];bool vis[maxn];
queue<node> que;
void add_edge(int u,int v){
edge[cnt].to=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void bfs(int u,int dep,int &maxdep,int &maxvex){
while(!que.empty())que.pop();
memset(vis,false,sizeof(vis));
que.push(node(u,dep));vis[u]=true;
while(!que.empty()){
node nod=que.front();que.pop();
for(int i=head[nod.u];~i;i=edge[i].next){
int v=edge[i].to;
if(!vis[v]){
vis[v]=true;
que.push(node(v,nod.dep+));
}
}
if(maxdep<nod.dep)maxdep=nod.dep,maxvex=nod.u;
}
}
int main(){
while(~scanf("%d",&t)){
while(t--){
scanf("%d%d",&n,&m);
memset(head,-,sizeof(head));cnt=maxdep=;maxvex=;
while(--n){
scanf("%d%d",&x,&y);
add_edge(x,y);
add_edge(y,x);
}
bfs(,,maxdep,maxvex);maxdep=;
bfs(maxvex,,maxdep,maxvex);
while(m--){
scanf("%d",&k);
if(k<=maxdep)printf("%d\n",k-);
else printf("%d\n",maxdep+(k-maxdep-)*);
}
}
}
return ;
}

题解报告:hdu 4607 Park Visit(最长链)的更多相关文章

  1. HDU 4607 Park Visit (树的最长链)

    Park Visit Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  2. hdu 4607 Park Visit(树上最长链)

    求树上最长链:两遍搜索. 第一次从树上任意点开始,最远点必然是某一条最长链上的端点u. 第二次从u开始,最远点即该最长链的另一端点. 先在最长链上走,不足再去走支链. 把询问数m错打成n,狠狠wa了一 ...

  3. HDU 4607 Park Visit (DP最长链)

    [题目]题意:N个城市形成一棵树,相邻城市之间的距离是1,问访问K个城市的最短路程是多少,共有M次询问(1 <= N, M <= 100000, 1 <= K <= N). [ ...

  4. HDU 4607 Park Visit 两次DFS求树直径

    两次DFS求树直径方法见 这里. 这里的直径是指最长链包含的节点个数,而上一题是指最长链的路径权值之和,注意区分. K <= R: ans = K − 1; K > R:   ans = ...

  5. hdu 4607 Park Visit 求树的直径

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 题目大意:给你n个点,n-1条边,将图连成一棵生成树,问你从任意点为起点,走k(k<=n) ...

  6. hdu 4607 Park Visit

    http://acm.hdu.edu.cn/showproblem.php?pid=4607 先求树的直径 方法:两遍bfs ,任选一点 a  求到a点最远的一点b ,然后 求到b点最远点 c 这样 ...

  7. hdu 4607 Park Visit (dfs)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 首先如果k小于等于直径长度,那么答案为k−1.如果k大于直径长度,设直径长度为r,那么答案为r− ...

  8. HDU 4607 Park Visit(树的直径)

    题目大意:给定一棵树,让求出依次访问k个点的最小花费,每条边的权值都为1. 思路:如果能一直往下走不回来,那么这个路径肯定是最小的,这就取决于给定的k,但是怎么确定这个能一直走的长度呢,其实这个就是树 ...

  9. HDU 4607 Park visit (求树的直径)

    解题思路: 通过两次DFS求树的直径,第一次以随意点作为起点,找到距离该点距离最远的点,则能够证明这个点一定在树的直径上,然后以该点为起点进行DFS得到的最长路就是树的直径. 最后的询问,假设K &l ...

随机推荐

  1. BMP文件格式具体解释

    BMP文件格式具体解释(BMP file format) BMP文件格式,又称为Bitmap(位图)或是DIB(Device-Independent Device,设备无关位图),是Windows系统 ...

  2. 正则表达式pattern的匹配格式

    0> 匹配 -------------------------------------------------------------------------------- (pattern)  ...

  3. 两篇C++和VC++字符串的文章

    有空挨个摘录写点心得 http://www.cnblogs.com/maowang1991/p/3572304.html http://www.cnblogs.com/maowang1991/p/35 ...

  4. Do not throw System.Exception, System.SystemException, System.NullReferenceException, or System.IndexOutOfRangeException intentionally from your own source code

    sonarqube的扫描结果提示 https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/exceptions/creatin ...

  5. hdu 1400 Mondriaan's Dream 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1400 题目意思:给出一个h * w的 大 矩形,需要用 1 * 2 的砖块去填充这个大矩形,问填充的方 ...

  6. poj 1724 ROADS 解题报告

    题目链接:http://poj.org/problem?id=1724 题目意思:给出一个含有N个点(编号从1~N).R条边的有向图.Bob 有 K 那么多的金钱,需要找一条从顶点1到顶点N的路径(每 ...

  7. 分享windows自带计划任务Task schedule使用指南

    下面以“启动运行QQ程序,无限期每186秒一次”为例,演示如何使用计划任务功能. 今天IT外包 www.itwaibaow.com 就为大家分享如何使用win7"计划任务“(Task sch ...

  8. 黑客技术 —— Linux 命令行

    很多和正则表达式 re 是一致的: 1. 修改上次执行的命令 删除多余部分: % grep fooo /var/log/auth.log % ^o % grep foo /var/log/auth.l ...

  9. Prime Cryptarithm

    链接 分析:对于三位数我们限定为[100,999],两位数我们限定为[10,99],然后我们依次判断是否满足乘法式且各个数位是否在数列中,若都满足+1 /* PROB:crypt1 ID:wangha ...

  10. 【扬中集训 DAY4T3】holiday

    [题目链接] 点击打开链接 [算法] 建C棵线段树即可,注意要用前缀和优化 [代码] 这是标程 #include <bits/stdc++.h> using namespace std; ...