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号节点(总根)的联系,每次切断边的费用不能超 ...
随机推荐
- STM32 定时器级联
根据参考手册给出的主/ 从定时器的例子 其实就是主定时器产生一个触发信号,让从定时器去接收这个触发信号,通过这个触发信号来让从定时器工作. 下面我们来看看我设置的从定时器 只需要配置 TIMx-> ...
- 关于Ajax无法下载文件到浏览器本地的问题
最近在做网站的时候遇到这样一个功能,在如图所示的页面中,需要用户点击链接的时候,能够以异步Ajax的方式判断服务器中是否存储有相应的Excel文件,如果没有的话就提示用户没有找到,如果有的话就下载到用 ...
- [转载]Oracle 游标使用全解
这个文档几乎包含了oracle游标使用的方方面面,全部通过了测试 -- 声明游标:CURSOR cursor_name IS select_statement --For 循环游标--(1)定义游标- ...
- Step1:SQL Server 复制介绍
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 前言(Introduction) 复制逻辑结构图(Construction) 系列文章索引(Catalog) 总结&am ...
- c#测试执行时间的方法
获取当前实例测量出来的总的运行时间 Stopwatch sp = new Stopwatch(); sp.Start(); //要测试的代码块 sp.Stop(); Console.WriteLine ...
- DOS下读取spd信息的汇编程序(通过SMBus)
汇编程序编写的读取spd信息的代码: ;----------------------------------------------------------- ;功能: 通过SMbus 读取内存的SP ...
- linux 搭建svn(待完成)
http://blog.csdn.net/lazy_cc/article/details/8726500搭建仓库 http://blog.csdn.net/xocoder/article/detail ...
- Docker学习笔记之通过 Dockerfile 创建镜像
0x00 概述 由于 Docker 镜像的结构优势,使它的占用空间远小于普通的虚拟机镜像,而这就大幅减少了 Docker 镜像在网络或者其他介质中转移所花费的时间,进而提高了我们进行迁移部署的效率.不 ...
- 详解centos6和centos7防火墙
CentOS6.5查看防火墙的状态: ? 1 [zh@localhost ~]$service iptable status 显示结果: ? 1 2 3 4 5 6 7 8 9 [zh@localho ...
- UI简述
UI的全称是user interface,即是用户界面.UI设计是指对软件的人机交互,操作编辑,界面美观的整体设计,从简单的角度来说就是,UI是视觉上的东西,包括logo.软件.网页的按钮.网页导航, ...