HDU#4607. Park Visit


题目描述


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?
 

输入格式


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.
 

输出格式


For each query, output the minimum walking distance, one per line.
 

样例输入输出


输入


1 4 2 3 2 1 2 4 2 2 4

  

输出


1 4

  

题意:


给定一棵树,从树中的任意选一个顶点出发,遍历K个点的最短距离是多少?(每条边的长度为1)
 

分析:


 
很自然的联想到求一遍最长链。设最长链长度为len
如果K < len + 1,那么答案就为K - 1,因为只需在最长链上走就行了。
如果K > len + 1;那么肯定不能不重复的遍历完K个点,一定有些点会重复遍历。这样就有些点的子树需要重复遍历,我们肯定不会去选取最长链重复遍历。
就是最长链上的点为根,不包含最长链的子树重复遍历。那么答案就变成了 len + (K - len - 1) * 2
 
 

树上最长链:


 
 求树上最长链的方法是用dp来求的。
f1表示从一个点子树里的最长链,f2表示一个点子树里的次长链。维护一下,然后答案为每个点f1 + f2的之中的最大值

int dfs(int u,int pre){
for(int i = head[u];i;i = edge[i].next){
int v = edge[i].to;
if(v == pre)continue;
dfs(v,u);
if(f1[u] < f1[v] + edge[i].dis)
{
f2[u] = f1[u];
f1[u] = f1[v] + edge[i].dis;
}
else f2[u] = max(f2[u],f1[v] + edge[i].dis);
}
ans = max(ans,f1[u] + f2[u]);
return ans;
}

贴上AC代码:


# include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
using namespace std;
const int N = 1e5 + ;
const int M = 2e5 + ;
const int INF = 0x3f3f3f3f;
int n,m,cnt,head[N];
int read()
{
int ans=,f=;
char i=getchar();
while(i<''||i>''){if(i=='-')f=-;i=getchar();}
while(i>=''&&i<=''){ans=ans*+i-'';i=getchar();}
return ans*f;
}
struct Edge{
int to,next;
int dis;
}edge[M];
void AddEdge(int u,int v,int res){
Edge E1 = {v,head[u],res};
edge[++cnt] = E1;head[u] = cnt;
Edge E2 = {u,head[v],res};
edge[++cnt] = E2;head[v] = cnt;
}
long long f1[N],f2[N],ans;
int dfs(int u,int pre){
for(int i = head[u];i;i = edge[i].next){
int v = edge[i].to;
if(v == pre)continue;
dfs(v,u);
if(f1[u] < f1[v] + edge[i].dis)
{
f2[u] = f1[u];
f1[u] = f1[v] + edge[i].dis;
}
else f2[u] = max(f2[u],f1[v] + edge[i].dis);
}
ans = max(ans,f1[u] + f2[u]);
return ans;
}
void Init(){
memset(head,,sizeof head);
memset(f1,,sizeof f1);
memset(f2,,sizeof f2);
cnt = ans = ;
}
void Getmap(){
Init();
n = read(), m = read();
int x,y,z;
for(int i = ;i < n;i++){
x = read();y = read();
AddEdge(x,y,);
}
dfs(,-);
ans += ;
for(int i = ;i <= m;i++){
x = read();
if(x <= ans)printf("%d\n",x - );
else printf("%d\n",ans - + (x - ans) * );
}
}
int main(){
int T;
T = read();
while(T--)
Getmap();
return ;
}
  

[HDU4607]Park Visit(树上最长链)的更多相关文章

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

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

  2. 题解报告:hdu 4607 Park Visit(最长链)

    Problem Description Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The par ...

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

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

  4. hdu 4612 Warm up(缩点+树上最长链)

    本来就是自己负责图论,结果水了= = 题目其实很裸,就是求桥的数量,只是要新加上一条边罢了.做法:先缩点.再在树上搜最长链(第一场多校的hdu 4607Park Visit就考了最长链,小样,套个马甲 ...

  5. HDU4607(求树中的最长链)

    题目:Park Visit 题意:给定一棵树,从树中的任意选一个顶点出发,遍历K个点的最短距离是多少?(每条边的长度为1) 解析:就是求树的最长链,假设求出的树的最长链所包含的点数为m,那么如果K&l ...

  6. 树上最长链 Farthest Nodes in a Tree LightOJ - 1094 && [ZJOI2007]捉迷藏 && 最长链

    树上最远点对(树的直径) 做法1:树形dp 最长路一定是经过树上的某一个节点的. 因此: an1[i],an2[i]分别表示一个点向下的最长链和次长链,次长链不存在就设为0:这两者很容易求 an3[i ...

  7. CF1009F Dominant Indices(树上DSU/长链剖分)

    题目大意: 就是给你一棵以1为根的树,询问每一个节点的子树内节点数最多的深度(相对于这个子树根而言)若有多解,输出最小的. 解题思路: 这道题用树链剖分,两种思路: 1.树上DSU 首先想一下最暴力的 ...

  8. hdu4607 Park Visit(树的直径)

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

  9. HDU-4607 Park Visit bfs | DP | dfs

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 首先考虑找一条最长链长度k,如果m<=k+1,那么答案就是m.如果m>k+1,那么最 ...

随机推荐

  1. 编译安装LAMP之php(fpm模块)

    一,准备工作实验平台为CentOS6.6,先下载所需的安装包,我使用的是php-5.4.26.tar.gz,下载地址 http://mirrors.sohu.com/php/ 编译安装的目录:/usr ...

  2. Java Web项目,Android和微信小程序的初始页面配置

    Java Web项目 我们在Eclipse里开了Java Web项目之后,Run As Tomcat或者Apache服务器,本地运行,如果直接用http://localhost:8080访问项目,会发 ...

  3. Git搭建自己的网站服务器(Linux)

    git服务器弄了半天终于搞定了,还是记录下吧,不然下次有得忘了 流程: 服务器 构建git目录 git用户,git组作为仓库管理 ssh授权(远程无需密码接入) hook(post-receive)自 ...

  4. JVM的异常体系

    任何程序都追求正确有效的运行,除了保证我们代码尽可能的少出错之外,我们还要考虑如何有效的处理异常,一个良好的异常框架对于系统来说是至关重要的.最近在采集框架的时候系统的了解一边,收获颇多,特此记录相关 ...

  5. struct获取不到值的小错误

    struct2 get set 这两个方法一定要用双骆驼命名法:getA() setA(),        而geta() seta()不行 我找了好久的错误,只能说框架这东西快捷方便,找起错误要人命

  6. vue 父子组件的加载顺序

    一.加载渲染过程 父beforeCreate->父created->父beforeMount->子beforeCreate->子created->子beforeMount ...

  7. 【2018 CCPC网络赛】1003 - 费马小定理

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6440 这题主要是理解题意: 题意:定义一个加法和乘法,使得 (m+n)p = mp+np; 其中给定 ...

  8. Linux 系统内存分析

    1. 内存基本介绍 1.计算机基本结构: 电脑之父--冯·诺伊曼提出了计算机的五大部件:输入设备.输出设备.存储器.运算器和控制器 如图: 输入设备:键盘鼠标等 CPU:是计算机的运算核心和控制核心, ...

  9. bzoj3336 Uva10572 Black and White

    题目描述: 数据范围:2<=n,m<=8 题解: 很明显需要状压.但是怎么压不知道,压什么不知道. 然后从条件下手. 条件1要求黑色在一起白色在一起,记录轮廓线很容易做到. 条件2要求不能 ...

  10. 实现验证的vsftpd虚拟用户

    实现基于文件验证的vsftpd虚拟用户--(一台) 一.创建用户数据库文件 vim /etc/vsftpd/vuser cd /etc/vsftpd/ db_load -T -t hash -f vu ...