POJ 2631 Roads in the North (模板题)(树的直径)
<题目链接>
题目大意:
求一颗带权树上任意两点的最远路径长度。
解题分析:
裸的树的直径,可由树形DP和DFS、BFS求解,下面介绍的是BFS解法。
在树上跑两遍BFS即可,第一遍BFS以任意点为起点,此时得到的离它距离最远的点为树的直径上的端点之一,然后再以这个端点为起点,跑一遍BFS,此时离它最远的点为树直径的另一个端点,同时,它们之间的距离即为树的直径。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int M=1e4+; struct Edge{
int to,val,next;
}edge[M<<];
int vis[M],d[M];
int head[M],cnt;
int res,ans;
void init(){
cnt=;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,int w){
edge[++cnt].to=v,edge[cnt].val=w;
edge[cnt].next=head[u],head[u]=cnt;
}
void bfs(int s){
memset(vis,,sizeof(vis));
queue<int>que;
vis[s]=;d[s]=;
que.push(s);
while(!que.empty()){
int u=que.front();
que.pop();
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].to;
if(!vis[v]){
d[v]=d[u]+edge[i].val;
vis[v]=;
que.push(v);
if(d[v]>ans) //更新最远距离
ans=d[v],res=v;
}
}
}
}
int main(){
init();
int u,v,w;
while(scanf("%d%d%d",&u,&v,&w)!=EOF)
addedge(u,v,w),addedge(v,u,w);
ans=;
bfs(); //先得到离1距离最远的节点(该节点为树的直径那两个节点之一)
bfs(res); //然后再跑一遍BFS,得到直径的另一个端点
printf("%d\n",ans);
return ;
}
2018-11-07
POJ 2631 Roads in the North (模板题)(树的直径)的更多相关文章
- 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 (求树的直径)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
- 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
题目连接 http://poj.org/problem?id=2631 Roads in the North Description Building and maintaining roads am ...
- poj 2631 Roads in the North【树的直径裸题】
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2359 Accepted: 115 ...
- POJ 1985 Cow Marathon (模板题)(树的直径)
<题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...
- 题解报告:poj 2631 Roads in the North(最长链)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
- POJ 2631 Roads in the North (树的直径)
题意: 给定一棵树, 求树的直径. 分析: 两种方法: 1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径. 2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次 ...
随机推荐
- NPOI写Excel,Spire.XLS for.NET组件转换Excel为PDF
首先要引用NPOI动态库和Microsoft.Office.Interop.excel.dll (Microsoft.Office.Interop.excel.dll 最好使用使用NuGet下载 , ...
- jquery 获取和设置 select下拉框的值
获取Select : 获取select 选中的 text : $("#ddlRegType").find("option:selected").text(); ...
- css样式之属性操作
一.文本属性 1.text-align:cnter 文本居中 2.line heigth 垂直居中 :行高,和高度对应 3.设置图片与文本的距离:vertical-align 4.text-decor ...
- SQLAchemy基础知识
一.什么是SQLAchemy? SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作,简言之便是:将对象转换成SQL,然后使用数据 ...
- cf29d 深搜,dfs序
#include<bits/stdc++.h> using namespace std; #define maxn 500 ]; int n,head[maxn],tot,a[maxn], ...
- ShadingJdbc学习
可参考:https://blog.csdn.net/jadebai/article/details/86716082 https://blog.csdn.net/jadebai/article/det ...
- bzoj 5418
这是拓展crt的典型应用 在你开始做之前,我一定要告诉你一件事情:虽然这道题看着和拓展crt模板很像,但他俩是有巨大的区别的!不要直接把板子改吧改吧扔上去! 题目模型:求解模线性方程组 其中p1,p2 ...
- 获取git更新文件
#获取最后一个版本的更新日志workspace2=/testtemplogdir=/testtmprm -rf ${logdir}/change.loggit diff --stat $GIT_PRE ...
- 论文阅读笔记九:SEMANTIC IMAGE SEGMENTATION WITH DEEP CONVOLUTIONAL NETS AND FULLY CONNECTED CRFS (DeepLabv1)(CVPR2014)
论文链接:https://arxiv.org/abs/1412.7062 摘要 该文将DCNN与概率模型结合进行语义分割,并指出DCNN的最后一层feature map不足以进行准确的语义分割,DCN ...
- 用 DocumentFormat.OpenXml 和Microsoft.Office.Interop.Word 写入或者读取word文件
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...