HDU 3848 CC On The Tree(树形dp)
http://acm.hdu.edu.cn/showproblem.php?pid=3848
题意:
求一棵树上两个叶子结点之间的最短距离。
思路:
两个叶子节点之间一定会经过非叶子节点,除非只有两个节点。
所以我们只需要维护离每个非叶子节点最远的叶子节点距离和次远距离,两者相加即是两个叶子节点之间的距离。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const int maxn = +; int n;
int d[maxn][];
vector<pll> G[maxn]; void dfs(int u, int fa)
{
d[u][]=d[u][]=INF;
if(G[u].size()==)
{
d[u][]=;
return ;
}
for(int i=;i<G[u].size();i++)
{
int v=G[u][i].first;
int w=G[u][i].second;
if(v==fa) continue;
dfs(v,u);
if(d[u][]>d[v][]+w)
{
d[u][]=d[u][];
d[u][]=d[v][]+w;
}
else if(d[u][]>d[v][]+w)
d[u][]=d[v][]+w;
}
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d",&n) && n)
{
for(int i=;i<=n;i++) G[i].clear();
for(int i=;i<n;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
G[u].push_back(make_pair(v,w));
G[v].push_back(make_pair(u,w));
}
if(n==)
{
printf("%d\n",G[][].second);
continue;
}
int i;
for(i=;i<=n;i++) if(G[i].size()>) break;
dfs(i,-);
int ans=INF;
for(int i=;i<=n;i++)
ans=min(ans,d[i][]+d[i][]);
printf("%d\n",ans); }
return ;
}
HDU 3848 CC On The Tree(树形dp)的更多相关文章
- HDU 3848 CC On The Tree 树形DP
题意: 给出一棵边带权的树,求距离最近的一对叶子. 分析: 通过DFS计算出\(min(u)\):以\(u\)为根的子树中最近叶子到\(u\)的距离. 然后维护一个前面子树\(v_i\)中叶子到\(u ...
- 熟练剖分(tree) 树形DP
熟练剖分(tree) 树形DP 题目描述 题目传送门 分析 我们设\(f[i][j]\)为以\(i\)为根节点的子树中最坏时间复杂度小于等于\(j\)的概率 设\(g[i][j]\)为当前扫到的以\( ...
- HDU 1520.Anniversary party 基础的树形dp
Anniversary party Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 5682 zxa and leaf 二分 树形dp
zxa and leaf 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5682 Description zxa have an unrooted t ...
- hdu6035 Colorful Tree 树形dp 给定一棵树,每个节点有一个颜色值。定义每条路径的值为经过的节点的不同颜色数。求所有路径的值和。
/** 题目:hdu6035 Colorful Tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题意:给定一棵树,每个节点有一个颜色值.定 ...
- hdu-5834 Magic boy Bi Luo with his excited tree(树形dp)
题目链接: Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: ...
- CF 461B Appleman and Tree 树形DP
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
- HDU 3586 Information Disturbing(二分+树形dp)
http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 题意: 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超 ...
随机推荐
- limit小结
1. Limit接受一个或两个数字参数.参数必须是一个整数常量.如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目. 2. 初始记录行的偏移量是 0(而不是 ...
- android TextView Unicde编码转换 android中一些特殊字符Unicode码值
android TextView Unicde编码转换 android中一些特殊字符Unicode码值 android中一些特殊字符(如:←↑→↓等箭头符号,约等于号≍)的Unicode码值 Text ...
- @Transactional noRollbackFor
网上查资料看的也是云里雾里的. 比如说: @Transactional(noRollbackFor=ProcessException.class) 那他是什么意思呢? 一句话,在你声明的这个事物里如果 ...
- JavaScript使用localStorage缓存Js和css文件
对于WebApp来说,将js css文件缓存到localstorage区可以减少页面在加载时与HTTP请求的交互次数,从而优化页面的加载时间.特别是当移端信号不好高延迟时优化效果还是很显见的 下面的代 ...
- Python智能检测编码并转码
#安装包工具 $pip3 install chardet #直接打开文件,中文显示乱码 >>> import chardet >>> f = open('test. ...
- 从源码层面聊聊面试问烂了的 Spring AOP与SpringMVC
Spring AOP ,SpringMVC ,这两个应该是国内面试必问题,网上有很多答案,其实背背就可以.但今天笔者带大家一起深入浅出源码,看看他的原理.以期让印象更加深刻,面试的时候游刃有余. Sp ...
- JVM虚拟机详解
1. 什么是JVM? JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来 ...
- MediaCodec在Android视频硬解码组件的应用
https://yq.aliyun.com/articles/632892 云栖社区> 博客列表> 正文 MediaCodec在Android视频硬解码组件的应用 cheenc 201 ...
- 将GPS获得的UTC时间转换成本地时间的方法(转)
源: 将GPS获得的UTC时间转换成本地时间的方法 参考: gps时转换为utc时间方法
- django中模型详解-字段类型与约束条件
这片博文来详细说明django模型的使用,涉及到django模型的创建,字段介绍,以及django模型的crud操作,以及一对一等操作. 在使用模型之前,我们首先设置数据库选项,django的默认数据 ...