<题目链接>

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

解题分析:

裸的树的直径,可由树形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. Confluence 6 用户宏示例 - NoPrint

    这个示例演示了如何创建一个用户宏,这个宏包括了在查看页面中显示的内容,但是不被打印. Macro name noprint Visibility Visible to all users in the ...

  2. Netty沾包和拆包

    1.连着发两条,会沾在一起,这就是沾包 2.包尾添加特殊分隔符,接收方通过特殊分隔符切分报文区分,这就是拆包 在ChatServerInit类.ChatClientInit类分别加入以下代码 Byte ...

  3. mysql 安装问题一:由于找不到MSVCR120.dll,无法继续执行代码.重新安装程序可能会解决此问题。

    这种错误是由于未安装  vcredist  引起的 下载  vcredist  地址:https://www.microsoft.com/zh-CN/download/details.aspx?id= ...

  4. 用mybatis做数据库处理 代码中的字段大小写 要和mapper映射设置的大小写一致(这TM不废话么,原谅我渣!~~)

    简单描述情况:其实这个问题怎么说呢,有人给你说,你肯定能意识到,必须大小写对应的嘛.emmmm我现在才意识到是因为:自己在下边敲代码,配的mapper文件并没有把属性变量,和数据库里的段单独提出来做映 ...

  5. Dinner

    问题 : Dinner 时间限制: 1 Sec  内存限制: 32 MB 题目描述 Little A is one member of ACM team. He had just won the go ...

  6. Django中各目录文件的作用

    一般的项目结构如下(大同小异) my_site是一个项目,blog是项目下的应用之一,可以使用创建命令创建更多的应用. 最上层的django文件夹: 自己手动创建,名字随意. 第二层my_site文件 ...

  7. 批量杀掉多个pid文件中记录的pid进程, 并集成到shell脚本中

    head_files=`find ./fmsConf/ -name "*.pid"` for file in $head_files do cat $file | awk rm - ...

  8. 步步为营-74-Request,Response和server的其他成员

    Request 1 Request.UrlReferrer 获取请求的来源 2 Request.UserHostAddress 获取访问者的IP地址 3 Request.Cookies 获取浏览器发送 ...

  9. openstack 网络更改版

    Openstack环境部署 (参考文献:http://www.cnblogs.com/kevingrace/p/5707003.html 和 https://docs.openstack.org/mi ...

  10. 使用python解决算法和数据结构--使用栈实现符号匹配

    现在要自己来实现这些数据结构和常用算法了. 把基础再打牢一些. 栈的作用很大,无须多言. 我尽量看了题目要求,自己来实现代码的. # coding = utf-8 class Stack: def _ ...