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)的更多相关文章

  1. HDU 3848 CC On The Tree 树形DP

    题意: 给出一棵边带权的树,求距离最近的一对叶子. 分析: 通过DFS计算出\(min(u)\):以\(u\)为根的子树中最近叶子到\(u\)的距离. 然后维护一个前面子树\(v_i\)中叶子到\(u ...

  2. 熟练剖分(tree) 树形DP

    熟练剖分(tree) 树形DP 题目描述 题目传送门 分析 我们设\(f[i][j]\)为以\(i\)为根节点的子树中最坏时间复杂度小于等于\(j\)的概率 设\(g[i][j]\)为当前扫到的以\( ...

  3. HDU 1520.Anniversary party 基础的树形dp

    Anniversary party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  4. HDU 5682 zxa and leaf 二分 树形dp

    zxa and leaf 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5682 Description zxa have an unrooted t ...

  5. hdu6035 Colorful Tree 树形dp 给定一棵树,每个节点有一个颜色值。定义每条路径的值为经过的节点的不同颜色数。求所有路径的值和。

    /** 题目:hdu6035 Colorful Tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题意:给定一棵树,每个节点有一个颜色值.定 ...

  6. 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: ...

  7. 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 ...

  8. codeforces 161D Distance in Tree 树形dp

    题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...

  9. HDU 3586 Information Disturbing(二分+树形dp)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=3586 题意: 给定一个带权无向树,要切断所有叶子节点和1号节点(总根)的联系,每次切断边的费用不能超 ...

随机推荐

  1. Lucene 个人领悟 (一)

    在上学的时候就对搜索有着极大地兴趣,图书馆也借了好多的书看过,也用过Python写过爬虫. 有好多人在初步学习Lucene的时候都以为他是一个搜索引擎,或者搜索工具. 在此我要特别强调一下,Lucen ...

  2. 匿名内部类和内部类中的this

    package test; public class A extends B { public String toString() { return "A"; } public s ...

  3. Server Library [Apache Tomcat 7.0] unbound解决方案

    问题描述: 当在MyEclipse中导入高版本Eclipse的[Eclipse Dynamic Web]项目后,会发现其Java Build Path(选定项目->Alt+Enter即可打开Pr ...

  4. Java学习笔记之linux配置java环境变量(三种环境变量)

    0x00 压安装jdk 在shell终端下进入jdk-6u14-linux-i586.bin文件所在目录, 执行命令 ./jdk-6u14-linux-i586.bin 这时会出现一段协议,连继敲回车 ...

  5. Docker学习笔记之Docker的Build 原理

    0x00 概述 使用 Docker 时,最常用的命令无非是 docker container 和 docker image 相关的子命令,当然最初没有管理类命令(或者说分组)的时候,最常使用的命令也无 ...

  6. js的匿名函数 和普通函数

    匿名函数在声明时不用带上函数名, 可以把匿名函数当作一个function类型的值来对待 声明一个普通的函数 function func() { ... } 可以认为和var func = functi ...

  7. ARM 架构、ARM7、ARM9、STM32、Cortex M3 M4 、51、AVR 之间有什么区别和联系?(转载自知乎)

    ARM架构:  由英国ARM公司设计的一系列32位的RISC微处理器架构总称,现有ARMv1~ARMv8种类. ARM7:       一类采用ARMv3或ARMv4架构的,使用冯诺依曼结构的内核. ...

  8. [c/c++] programming之路(2)、kill QQ,弹出系统对话框,吃内存等

    一.删除文件 二.盗取密码的原理 #include<stdlib.h> //杀掉QQ,然后提示网络故障,请重新登陆,弹出高仿界面,获取账号密码,然后打开QQ进行登录 void main() ...

  9. 【Python047-魔法方法:定制序列】

    一.协议是什么 1.协议(protocols)与其他编程语言中的接口很相似,它规定你那些方法必须要定义.然而在Python中协议就显的不那么正式,事实上,在Python中,协议更像是一种指南 2.容器 ...

  10. 对html标签 元素 以及css伪类和伪元素的理解

    标签:这应该都知道.<br/> .<a>.<p></p> 等都是标签. 元素:标签开始到结束.比如:<p>p之间的内容</p> ...