<题目链接>

题目大意:
求一颗带权树上任意两点的最远路径长度。

解题分析:

裸的树的直径,可由树形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 (模板题)(树的直径)的更多相关文章

  1. POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)

    题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...

  2. poj 2631 Roads in the North (自由树的直径)

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4513   Accepted: 215 ...

  3. POJ 2631 Roads in the North (求树的直径)

    Description Building and maintaining roads among communities in the far North is an expensive busine ...

  4. POJ 2631 Roads in the North(树的直径)

    POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u ...

  5. poj 2631 Roads in the North

    题目连接 http://poj.org/problem?id=2631 Roads in the North Description Building and maintaining roads am ...

  6. poj 2631 Roads in the North【树的直径裸题】

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2359   Accepted: 115 ...

  7. POJ 1985 Cow Marathon (模板题)(树的直径)

    <题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...

  8. 题解报告:poj 2631 Roads in the North(最长链)

    Description Building and maintaining roads among communities in the far North is an expensive busine ...

  9. POJ 2631 Roads in the North (树的直径)

    题意: 给定一棵树, 求树的直径. 分析: 两种方法: 1.两次bfs, 第一次求出最远的点, 第二次求该点的最远距离就是直径. 2.同hdu2196的第一次dfs, 求出每个节点到子树的最长距离和次 ...

随机推荐

  1. SQL Server 函数之日期格式化函数

    SQL Server 函数之日期格式化函数 高文龙关注0人评论612人阅读2017-09-23 13:47:07 SQL Server 函数之日期格式化函数 对于一些经常写SQL Server执行语句 ...

  2. Confluence 6 使用 Decorator 宏

    Decorator 宏(Macros)是 Velocity  宏.这个宏可以被用来在页面编辑 Custom decorators 中创建复杂或者可变的部分,例如菜单,页面其他部分等.Decorator ...

  3. Nginx详解二:Nginx基础篇之Nginx的优点

    Nginx是一个开源且高性能.可靠的HTTP中间件.代理服务 常见的HTTP服务: HTTPD--Apache基金会 IIIS--微软 GWS--Google(不对外开放) Nginx优势: 一.IO ...

  4. Vue 添加外部的时间插件不触发v-model事件更改数据

    使用的jquery日期插件 最终问题是 在选择完成日期后并未激活 oninput事件,所以也没有激活v-model 去改变date 解决思路: 去插件js文件中,在赋值给dom的时候添加模拟输入事件便 ...

  5. js 图片转换base64 base64转换为file对象

    function getImgToBase64(url,callback){//将图片转换为Base64 var canvas = document.createElement('canvas'), ...

  6. 编写UEditor插件

    UE.registerUI('beijing', function (editor, uiName) { // 注册按钮执行时的command命令 editor.registerCommand(uiN ...

  7. Https双向认证Android客户端配置

    Https .cer证书转换为BKS证书 公式https://blog.csdn.net/zww986736788/article/details/81708967 keytool -importce ...

  8. 连接Oracle时报错ORA-12541: TNS: 无监听程序

    从开始菜单中打开“Oracle Net Configuration Assistance”,选择“监听程序配置”,如下图所示,点击下一步.   选择“重新配置”,如下图所示,点击下一步.   选择监听 ...

  9. jQuery分步步骤

    插件描述:jQuery上一步.下一步,分步步骤,兼容性如下: 使用方法 1.引入样式和脚本 <link rel="stylesheet" type="text/cs ...

  10. CCF-CSP 第三题字符串整理(模拟大法好)

    URL映射 规则的相邻两项之间用‘/’分开,所以我们先把所有项分开,然后依次把两个字符串的对应项匹配即可. 分离字符串这里用字符串流(stringstream)处理,先把所有的‘/’变为空格,然后一个 ...