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

http://poj.org/problem?

id=2631

题意:

有一个树结构, 给你树的全部边(u,v,cost), 表示u和v两点间有一条距离为cost的边. 然后问你该树上最远的两个点的距离是多少?(即树的直径)

分析:

对于树的直径问题, <<算法导论>>(22 2-7)例题有说明.

详细解法: 首先从树上随意一个点a出发, (BFS)找出到这个点距离最远的点b. 然后在从b点出发(BFS)找到距离b点最远的点c. 那么bc间的距离就是树的直径.

程序实现用的是邻接表来表示树结构.

Head[i]==j 表示与i结点连接的边组成了一条链表, 当中第j条边是这条链的头一个元素, 接着通过j能够找到剩余的(与i连接的)边.

Edge是用来表示每条边的结构.

BFS返回从s结点能走到的最远的点的编号

AC代码: C++提交才行

//C++提交才行
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=10000+5;
const int maxm=1000000+5; //边结构
struct Edge
{
Edge(){}
Edge(int to,int cost,int next):to(to),cost(cost),next(next){}
int to;
int cost;
int next;
}edges[maxm]; //全部边
int cnt; //边总数
int head[maxn];//头结点 //加入两条有向边
void AddEdge(int u,int v,int cost)
{
edges[cnt]=Edge(v,cost,head[u]);
head[u]=cnt++;
edges[cnt]=Edge(u,cost,head[v]);
head[v]=cnt++;
} //dist[i]表当前点到i的距离
int dist[maxn]; //BFS返回从s结点能走到的最远的点的编号
int BFS(int s)
{
int max_dist=0;//记录最远距离
int id=s; //记录最远点
queue<int> Q;
memset(dist,-1,sizeof(dist));
dist[s]=0;
Q.push(s);
while(!Q.empty())
{
int u=Q.front(); Q.pop();
if(dist[u]>max_dist)
{
max_dist=dist[u];
id=u;
}
for(int i=head[u];i!=-1;i=edges[i].next)
{
Edge &e=edges[i];
if(dist[e.to]==-1)//未訪问过e.to点
{
dist[e.to]=dist[u]+e.cost;
Q.push(e.to);
}
}
}
return id;
} int main()
{
int u,v,cost;
memset(head,-1,sizeof(head));
cnt=0;
while(scanf("%d%d%d",&u,&v,&cost)==3)
AddEdge(u,v,cost);
printf("%d\n",dist[BFS(BFS(u))]);
return 0;
}

POJ 2631 Roads in the North(树的直径)的更多相关文章

  1. poj 2631 Roads in the North

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

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

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

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

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

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

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

  5. POJ 2631 Roads in the North (模板题)(树的直径)

    <题目链接> 题目大意:求一颗带权树上任意两点的最远路径长度. 解题分析: 裸的树的直径,可由树形DP和DFS.BFS求解,下面介绍的是BFS解法. 在树上跑两遍BFS即可,第一遍BFS以 ...

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

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

  7. 【POJ2631】Roads in the North 树的直径

    题目大意:给定一棵 N 个节点的边权无根树,求树的直径. 代码如下 #include <cstdio> #include <algorithm> using namespace ...

  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(最长链)

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

随机推荐

  1. R-CNN论文翻译——用于精确物体定位和语义分割的丰富特征层次结构

    原文地址 我对深度学习应用于物体检测的开山之作R-CNN的论文进行了主要部分的翻译工作,R-CNN通过引入CNN让物体检测的性能水平上升了一个档次,但该文的想法比较自然原始,估计作者在写作的过程中已经 ...

  2. ⒂bootstrap组件 折叠 基础案例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. Oracle-1 - :超级适合初学者的入门级笔记,CRUD,事务,约束 ......

    Oracle 更改时间: 2017-10-25  -  21:33:49 2017-10-26  -  11:43:19 2017-10-27  -  19:06:57 2017-10-28  -  ...

  4. Spring IOC容器分析(3) -- DefaultListableBeanFactory

    上一节介绍了封装bean对象的BeanDefinition接口.从前面小结对BeanFactory的介绍中,我们知道bean对象是存储在map中,通过调用getBean方法可以得到bean对象.在接口 ...

  5. C# TabControl标签的隐藏

    当你想要隐藏的时候 if (this.tabMain.TabPages[ "tabpageThePage "] != null) { this.tabMain.TabPages.R ...

  6. NodeJS 常用模块积累

    cluster&forever cluster & forever 虽然 nodejs 原生已经提供了 cluster 模块,大部分情况下可以满足我们的基本需求,但这两个模块 clus ...

  7. javaBean实体包区分

    随着软件工程结构越来越复杂,单一的entity实体包无法满足业务需要,可以采取PO,BO,DTO结构来分类实体. PO (Persistent Object):与数据库表数据关联的实体. BO(Bus ...

  8. 入坑第二式 golang入坑系统

    史前必读: 这是入坑系列的第二式,如果错过了第一式,可以去gitbook( https://andy-zhangtao.gitbooks.io/golang/content/ )点个回放,看个重播.因 ...

  9. enote笔记法(2)——why的使用

    章节:why的使用 用法: why 概念|词汇(比概念更一般的形式的keyword)|短语|句子 用法1: why 概念|why keyword([比概念更一般的形式的keyword]) “why 概 ...

  10. PHP操作MySQL数据库之天龙八部 -- 七贱下天山 -- 六脉神剑

    天龙八部            八步操作数据库 七贱下天山        七步操作数据库  (将判断错误省略) 六脉神剑            六步操作数据库(将判断错误省略,将选择数据库添加到第一步 ...