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. Python学习——列表

    定义:1.列表(list)是由一系列特定顺序排列的元素组成,可以包含字母,数字或者将任何东西加入列表. 2.列表的标识符号为 [ ],函数名称为list 3.列表是有序的 相关概念: 元素:列表中的值 ...

  2. UWP 返回顶部按钮

    返回顶部的按钮,like this 当用户下滑了一定的距离之后,通常是快滑倒底部的时候,出现返回顶部按钮,当用户向上快滑,滑到顶部的时候,返回顶部按钮自动消失. 在UWP中,用来滚动内容的控件是Scr ...

  3. Python学习二:词典基础详解

    作者:NiceCui 本文谢绝转载,如需转载需征得作者本人同意,谢谢. 本文链接:http://www.cnblogs.com/NiceCui/p/7862377.html 邮箱:moyi@moyib ...

  4. python调用c代码2

    1.生成动态链接库 [root@typhoeus79 c]# more head.c #include <stdio.h> #include <stdlib.h> typede ...

  5. 自己动手修改Robotium代码(下)

    public void takeScreenshot(){   View decorView = viewFetcher.getRecentDecorView(viewFetcher.getWindo ...

  6. 说说css中pt、px、em、rem都扮演了什么角色

    前段时间,终于仔仔细细的把pt.px.em.rem了解了一遍,简单整理了一下做个记录. pt.px.em.rem都是什么 pt单位名称为点(Point),绝对长度单位.现在网页中出现得很少甚至不出现, ...

  7. SSE图像算法优化系列十二:多尺度的图像细节提升。

    无意中浏览一篇文章,中间提到了基于多尺度的图像的细节提升算法,尝试了一下,还是有一定的效果的,结合最近一直研究的SSE优化,把算法的步骤和优化过程分享给大家. 论文的全名是DARK IMAGE ENH ...

  8. python实现图片批量剪裁的程序

    from PIL import Image import os fin = 'D:/test' fout = 'D:/test2' for file in os.listdir(fin): file_ ...

  9. P1132 数字生成游戏

    题目请见:传送门 以下为题解,直接从洛谷上搬过来的,还专门改了markdown,(汗) 宽搜 with 一些技巧 由于查询量很大,所以要预先处理所有答案 预处理当然是用BFS,并同时进行delete, ...

  10. Android APK反编译 apktool使用教程

    2017年棋牌游戏突然就火了,正所谓春江水暖鸭先知本猿处在软件行业中就能清晰的感受到市场的变化,最近老家那边也是玩的风生水起,于是最近闲暇时光想到反编译下这些棋牌软件,看看代码实现的思路 (注:反编译 ...