【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 ...
随机推荐
- 20155302《网络对抗》Exp8 Web基础
20155302<网络对抗>Exp8 Web基础 实验内容 (1).Web前端HTML(0.5分) 能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个 ...
- PostgreSQL基础知识与基本操作索引页
磨砺技术珠矶,践行数据之道,追求卓越价值 返回顶级页:PostgreSQL索引页 luckyjackgao@gmail.com 本页记录所有本人所写的PostgreSQL的基础知识和基本操作相关文摘和 ...
- Codeforces 955C Sad powers (数论)
题目链接:Sad powers 题意:给出n个l和r,求出每个给出的[l,r]之间的可以使是另外一个数的k次方的数.(k>=2) 题解:题目给出的数据范围最大是1E18所以如果要直接把所有的从1 ...
- SSIS 包配置
在商业智能解决方案中,SSIS工程有两种部署模式:工程部署(project deployment)和包部署(package deployment),默认是工程部署模式,在Package的管理上,工程部 ...
- (3)学习笔记 ) ASP.NET CORE微服务 Micro-Service ---- Consul服务治理
Consul是注册中心,服务提供者.服务提供者.服务消费者等都要注册到Consul中,这样就可以实现服务提供者.服务消费者的隔离. 除了Consul之外,还有Eureka.Zookeeper等类似软件 ...
- 软件工程第三次作业(One who wants to wear the crown, Bears the crown.)
最大连续子数组和 题目 给定n个整数(可能为负数)组成的序列a[1],a[2],a[3],-,a[n],求该序列如a[i]+a[i+1]+-+a[j]的子段和的最大值.当所给的整数均为负数时定义子段和 ...
- 安装loadrunner11出现Microsoft Visual c++2005 sp1安装失败
本文转至别处,网上大神多
- ROCKETMQ——2主2从集群部署
1.压缩包准备两台服务器镜像操作cd /optmkdir softcd soft将两个压缩包复制到 soft目录unzip apache-maven-3.2.2-bin.zipunzip rocket ...
- 安装logstash及logstash的初步使用-处理DNS日志
安装logstash 需要高版本的java 使用1.4版本的java会有报错 # Can't start up: not enough memory 查询java信息 rpm -qa | grep j ...
- LeetCode 141. Linked List Cycle环形链表 (C++)
题目: Given a linked list, determine if it has a cycle in it. To represent a cycle in the given linked ...