[HDU4607]Park Visit(树上最长链)
HDU#4607. Park Visit
题目描述
输入格式
输出格式
样例输入输出
输入
1 4 2 3 2 1 2 4 2 2 4
输出
1 4
题意:
分析:
树上最长链:
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(树上最长链)的更多相关文章
- HDU 4607 Park Visit (DP最长链)
[题目]题意:N个城市形成一棵树,相邻城市之间的距离是1,问访问K个城市的最短路程是多少,共有M次询问(1 <= N, M <= 100000, 1 <= K <= N). [ ...
- 题解报告:hdu 4607 Park Visit(最长链)
Problem Description Claire and her little friend, ykwd, are travelling in Shevchenko's Park! The par ...
- hdu 4607 Park Visit(树上最长链)
求树上最长链:两遍搜索. 第一次从树上任意点开始,最远点必然是某一条最长链上的端点u. 第二次从u开始,最远点即该最长链的另一端点. 先在最长链上走,不足再去走支链. 把询问数m错打成n,狠狠wa了一 ...
- hdu 4612 Warm up(缩点+树上最长链)
本来就是自己负责图论,结果水了= = 题目其实很裸,就是求桥的数量,只是要新加上一条边罢了.做法:先缩点.再在树上搜最长链(第一场多校的hdu 4607Park Visit就考了最长链,小样,套个马甲 ...
- HDU4607(求树中的最长链)
题目:Park Visit 题意:给定一棵树,从树中的任意选一个顶点出发,遍历K个点的最短距离是多少?(每条边的长度为1) 解析:就是求树的最长链,假设求出的树的最长链所包含的点数为m,那么如果K&l ...
- 树上最长链 Farthest Nodes in a Tree LightOJ - 1094 && [ZJOI2007]捉迷藏 && 最长链
树上最远点对(树的直径) 做法1:树形dp 最长路一定是经过树上的某一个节点的. 因此: an1[i],an2[i]分别表示一个点向下的最长链和次长链,次长链不存在就设为0:这两者很容易求 an3[i ...
- CF1009F Dominant Indices(树上DSU/长链剖分)
题目大意: 就是给你一棵以1为根的树,询问每一个节点的子树内节点数最多的深度(相对于这个子树根而言)若有多解,输出最小的. 解题思路: 这道题用树链剖分,两种思路: 1.树上DSU 首先想一下最暴力的 ...
- hdu4607 Park Visit(树的直径)
Park Visit Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU-4607 Park Visit bfs | DP | dfs
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4607 首先考虑找一条最长链长度k,如果m<=k+1,那么答案就是m.如果m>k+1,那么最 ...
随机推荐
- QWidget标题栏双击事件
widget.h virtual bool event(QEvent *event); widget.cpp bool Widget::event(QEvent *event) { if (event ...
- 在Eclipse中通过JDBC连接MySQL步骤,非常详细!
通过JDBC连接MySQL基本步骤代码讲解步骤可能遇到的Bug基本步骤JDBC访问MySQL 1.加载JDBC驱动器—>哪个project需要,就添加到该project的jdbc文件夹下,我的j ...
- Python3基础教程(十九)—— 项目结构
本节阐述了一个完整的 Python 项目结构,你可以使用什么样的目录布局以及怎样发布软件到网络上. 创建Python项目 我们的实验项目名为 factorial,放到 /home/shiyanlou/ ...
- CPP-基础:关于私有成员的访问
a.C++的类的成员函数中,允许直接访问该类的对象的私有成员变量. b.在类的成员函数中可以访问同类型实例的私有变量. c.拷贝构造函数里,可以直接访问另外一个同类对象(引用)的私有成员. d.类的成 ...
- Sql Server 中锁的概念(2)
1.一般大家都对事务的四种隔离模式比较熟悉,从松到严依次是: - 读取未提交(Read uncommitted):处于此模式下可能会出现脏读.幻象读.不可重复读 - 读取已提交(Read commit ...
- Yii1 用commandBuilder方法往数据表中插入多条记录
$builder = Yii::app()->db->schema->commandBuilder; // 创建builder对象 $command = $builder->c ...
- 6. 将单独表空间(File-Per-Table Tablespaces)复制到另一个实例
6. 将单独表空间复制到另一个实例 本节介绍如何将单独表空间从一个MySQL实例复制 到另一个MySQL实例,也称为可传输表空间功能. 将InnoDB单独表空间复制到其他实例的原因有很多: - 在不对 ...
- 比特币 3角对冲python代码
3角对冲原理 基础货币 base, 兑换货币 quote, 中间货币 mid. 市场分为3个市场 p3: base_quote p2: quote_mid p1: quote_mid 代码逻辑 1, ...
- Postfix telnet www.azengna.com 25 Connection Refused 但是localhost连接成功
修改配置文件 vi /etc/postfix/main.cf 原先配置信息 .... inet_interfaces = all #inet_interfaces = $myhostname,loca ...
- hdu 1500 dp
/* 状态转移方程式: dp[i][j]=Min(dp[i][j-1],dp[i-1][j-2]+(a[j-1]-a[j])*(a[j-1]-a[j])); 依次求出第i个人在第j个数时的所求的最小值 ...