<题目链接>

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

解题分析:

裸的树的直径,可由树形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. centos7搭建smb服务

    1 yum install samba samba-client samba-common -y  安装smb服务 2 cp -a  /etc/samba/smb.conf /etc/samba/sm ...

  2. Hibrenate之事务的理解以及代码编写

    3 事务概念 事务(Transaction)是并发控制的单位,是用户定义的一个操作序列.这些操作要么都做,要么都不做,是一个不可分割的工作单位.通过事务,SQL Server能将逻辑相关的一组操作绑定 ...

  3. InstallUtil操作WindowsService

    要安装windows service 首先要找到 InstallUtil.exe,InstallUtil.exe位置在 C:\Windows\Microsoft.NET\Framework\v4.0. ...

  4. Confluence 6 管理站点模板

    模板是一个预先定义的页面,这个预先定义的页面可以在创建新页面的时候预先载入.模板可以由用户创建也可以通过蓝图提供.请查看 Page Templates 和 Blueprints 页面中的内容. 管理员 ...

  5. web前端识别文字转语音

    const msg = new SpeechSynthesisUtterance("hello world"); window.speechSynthesis.speak(msg) ...

  6. Metasploit one test

    1.对Metasploit的文件结构层次做一个目录结构图 2.漏洞利用的原理及其过程 攻击者发送一个附加攻击载荷的漏洞攻击代码给存在漏洞的系统.漏洞攻击代码首先执行,如果执行成功,攻击载荷中的实际代码 ...

  7. Django标签&迭代&循环&过滤

    1.{% for Person in persons %}模板标签的替换,就是利用了基础模板的底层设计,嵌套了其他显示的内容.常见的内容替换标签{% block content %}{%endbloc ...

  8. ShadingJdbc学习

    可参考:https://blog.csdn.net/jadebai/article/details/86716082 https://blog.csdn.net/jadebai/article/det ...

  9. AI-跨域、垃圾回收、content_type组见、接口处理

    AI-跨域.垃圾回收.content_type组见.接口处理 跨域 为什么有跨域?什么时候遇见的?答:由于浏览器的同源策略 阻止ajax请求 不阻止src请求:在测试时,项目上线后不会遇见跨域.源:协 ...

  10. 编写UEditor插件

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