【POJ2631】Roads in the North 树的直径
题目大意:给定一棵 N 个节点的边权无根树,求树的直径。
代码如下
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=1e4+10;
struct node{
int nxt,to,w;
}e[maxn<<1];
int tot=1,head[maxn];
inline void add_edge(int from,int to,int w){
e[++tot].nxt=head[from],e[tot].to=to,e[tot].w=w,head[from]=tot;
}
int d1[maxn],d2[maxn],ans;
void dfs(int u,int fa){
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to,w=e[i].w;if(v==fa)continue;
dfs(v,u);
if(d1[v]+w>d1[u])d2[u]=d1[u],d1[u]=d1[v]+w;
else d2[u]=max(d2[u],d1[v]+w);
}
}
void solve(){
dfs(1,0);
for(int i=1;i<=10000;i++)ans=max(ans,d1[i]+d2[i]);
printf("%d\n",ans);
}
int main(){
int from,to,w;
while(scanf("%d%d%d",&from,&to,&w)!=EOF){
add_edge(from,to,w),add_edge(to,from,w);
}
solve();
return 0;
}
【POJ2631】Roads in the North 树的直径的更多相关文章
- POJ 2631 Roads in the North(树的直径)
POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u ...
- poj--2631--Roads in the North(树的直径 裸模板)
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2389 Accepted: 117 ...
- Codeforces 1413F - Roads and Ramen(树的直径+找性质)
Codeforces 题目传送门 & 洛谷题目传送门 其实是一道还算一般的题罢--大概是最近刷长链剖分,被某道长链剖分与直径结合的题爆踩之后就点开了这题. 本题的难点就在于看出一个性质:最长路 ...
- poj 2631 Roads in the North【树的直径裸题】
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2359 Accepted: 115 ...
- POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)
题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...
- poj 2631 Roads in the North (自由树的直径)
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4513 Accepted: 215 ...
- POJ 2631 Roads in the North (模板题)(树的直径)
<题目链接> 题目大意:求一颗带权树上任意两点的最远路径长度. 解题分析: 裸的树的直径,可由树形DP和DFS.BFS求解,下面介绍的是BFS解法. 在树上跑两遍BFS即可,第一遍BFS以 ...
- POJ 2631 Roads in the North (树的直径)
题意: 给定一棵树, 求树的直径. 分析: 两种方法: 1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径. 2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次 ...
- poj1985 / poj2631(树的直径)
poj1985 Cow Marathon 树的直径裸题 树的直径的一般求法: 任意一点为起点,dfs/bfs找出与它最远的点$u$ 以$u$为起点,dfs/bfs找出与它最远的点$v$ 则$d(u,v ...
随机推荐
- 解决 配置springmvc拦截所有请求后请求静态资源404的问题
<servlet-mapping> <servlet-name>spring-servlet</servlet-name> <url-pattern>/ ...
- Java Web应用中支持跨域请求
转载:https://blog.csdn.net/lmy86263/article/details/51724221 由于工程合作开发的需要,后台的应用要能支持跨域访问,但是在这个跨域访问“时好时坏” ...
- mongodump备份小量分片集群数据
1.使用mongodump备份小量分片集群数据 如果一个分片集群的数据集比较小,可以直接使用mongodump连接到mongos实例进行数据备份.默认情况下,mongodump到非primary的节点 ...
- 自动化部署-Jenkins+SVN+MSBuild
这篇文章主要介绍下使用Jenkins实现自动化部署 下载 https://jenkins.io/download/ 安装 按步骤安装即可,下载的是windows版本,安装完成后,会看到这样一个正在运行 ...
- c语言数字图像处理(一):bmp图片格式及灰度图片转换
本篇文章首先介绍了bmp图片格式,主要参考wiki上的内容,包括bmp文件的存储方式,对于一些常见的bmp文件格式都给了例子,并且对8位 16位RGB555 16位RGB565格式的bmp文件进行了简 ...
- ssm整合各配置文件
ssm整合 1.配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns ...
- strongSwan配置、运行及测试
版本信息:strongSwan v5.7.2 1. 编译 tar xvf strongswan-5.7.2.tar.gz ./configure --prefix=/usr/ --sysco ...
- 剑指offer——滑动窗口的最大值
给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值.例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6, ...
- PAT甲题题解-1058. A+B in Hogwarts (20)-大水题
无语,这种水题还出,浪费时间,但又不得不A... #include <iostream> #include <cstdio> #include <algorithm> ...
- PAT甲题题解-1060. Are They Equal (25)-字符串处理(科学计数法)
又是一道字符串处理的题目... 题意:给出两个浮点数,询问它们保留n位小数的科学计数法(0.xxx*10^x)是否相等.根据是和否输出相应答案. 思路:先分别将两个浮点数转换成相应的科学计数法的格式1 ...