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号节点(总根)的联系,每次切断边的费用不能超 ...
随机推荐
- Hive 中的复合数据结构简介以及一些函数的用法说明
参见下面这篇博客: Hive 中的复合数据结构简介以及一些函数的用法说明
- intelj idea安装和配置
1|0优势 intellij idea 是目前公认的java最好的开发工具之一,商业版的IntelliJ应该包含了对 HTML5.CSS3.SASS.LESS.JavaScript.CoffeeScr ...
- css的优先级 和 权重
之前写页面样式时,有时会遇到 用多条样式定义规则对同一个元素进行样式设置的时候,当时想到的就是 按css选择器的优先级来搞定这个问题,说实话当时也就只记得 内嵌样式 > id > cla ...
- 和風いろはちゃんイージー / Iroha and Haiku (ABC Edition) (水水)
题目链接:http://abc042.contest.atcoder.jp/tasks/abc042_a Time limit : 2sec / Memory limit : 256MB Score ...
- docker log directory
Ubuntu - /var/log/upstart/docker.log Boot2Docker - /var/log/docker.log Debian GNU/Linux - /var/log/d ...
- GO语言学习笔记之Linux环境下安装GO语言
0x00 安装环境和GO版本 本篇是源码安装,非使用包管理工具安装. # Centos 7.4 # GO v1.11.2 0x01 下载GO安装包 # wget https://dl.google.c ...
- Java中五种遍历HashMap的方式
import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class Java8Templat ...
- RHEL6/7 x86_64下cachefilesd占用cpu达到100%
昨天,有个测试环境cachedfilesd CPU 100%,一直在跑了挺久,经查 1. CacheFiles介绍NFS是一种经常使用到的网络共享文件系统,在分布式环境下,多台服务器的文件共享是一个问 ...
- [c/c++] programming之路(8)、汇编、求模、自增自减
一.插入汇编 #include<stdio.h> void main(){ ; num=num+; //插入汇编语言 _asm{ mov eax,num;//eax是一个存储器,将num的 ...
- centos7 install fastdfs nginx
https://github.com/judasn/Linux-Tutorial/blob/master/markdown-file/FastDFS-Nginx-Lua-GraphicsMagick. ...