树的直径问题poj2631

来源:互联网 发布:淘宝优惠网站有哪些 编辑:程序博客网 时间:2024/05/21 09:46

树的直径是指在一棵树中(不能是图),任意两点之间最远的距离。

找出树的直径的方法:任选一个点p出发,运用bfs找到距离点p最远的点q,然后再从q点出发,再次使用bfs找到距离q点最远的点m,那么q和m之间的距离就是树的直径(也就是通过两次bfs来找出树的直径)

在做关于树或图的题目时,树或图的存储应该使用邻接表。

poj2631

Description

Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village twice. 
Given is an area in the far North comprising a number of villages and roads among them such that any village can be reached by road from any other village. Your job is to find the road distance between the two most remote villages in the area. 

The area has up to 10,000 villages connected by road segments. The villages are numbered from 1. 

Input

Input to the problem is a sequence of lines, each containing three positive integers: the number of a village, the number of a different village, and the length of the road segment connecting the villages in kilometers. All road segments are two-way.

Output

You are to output a single integer: the road distance between the two most remote villages in the area.

Sample Input

5 1 61 4 56 3 92 6 86 1 7

Sample Output

22

我的题解:

//这道题中邻接表的建立是用链表,但最好的做法是用数组建立邻接表#include<stdio.h>#include<string.h>#include<stdlib.h>bool flag[10005];int queue[10005];int length[10005];//用来记录当前点到起点的距离typedef struct Point//邻接表的结点{    int num;    int l;    struct Point* iNext;}ArcP;struct VNode//邻接表的表头{ArcP* first;}AdjList[10005];void init()//初始化邻接表表头{    for(int i=1;i<10005;i++)    {        AdjList[i].first=(ArcP*)malloc(sizeof(ArcP));        AdjList[i].first->num=i;        AdjList[i].first->l=0;        AdjList[i].first->iNext=NULL;    }}int bfs(){    int Max=1;int top=0;queue[top++]=1;flag[1]=false;length[1]=0;for(int i=0;i<top;i++)    {        ArcP* p=AdjList[queue[i]].first;        while(p->iNext!=NULL)        {            if(flag[p->iNext->num])            {                flag[p->iNext->num]=false;                queue[top++]=p->iNext->num;                length[p->iNext->num]=length[queue[i]]+p->iNext->l;//用bfs统计各点到起点距离                p=p->iNext;                continue;            }            p=p->iNext;        }    }    for(int i=2;i<=top;i++)//找出第一个最远点        if(length[Max]<length[i])            Max=i;    memset(flag,true,sizeof(flag));    top=0;    queue[top++]=Max;//以第一个最远点为起点    flag[Max]=false;    length[Max]=0;    for(int i=0;i<top;i++)//第二次bfs找最远点    {        ArcP* p=AdjList[queue[i]].first;        while(p->iNext!=NULL)        {            if(flag[p->iNext->num])            {                flag[p->iNext->num]=false;                queue[top++]=p->iNext->num;                length[p->iNext->num]=length[queue[i]]+p->iNext->l;                p=p->iNext;                continue;            }            p=p->iNext;        }    }    Max=1;    for(int i=2;i<=top;i++)        if(length[Max]<length[i])            Max=i;    return length[Max];//树的直径}int main(){int a,b,l,is_max=1,i=0;ArcP* p=(ArcP*)malloc(sizeof(ArcP));init();memset(flag,true,sizeof(flag));while(scanf("%d%d%d",&a,&b,&l)!=EOF)//把数据写入邻接表{    p=AdjList[a].first;        while(p->iNext!=NULL)        {            p=p->iNext;        }        p->iNext=(ArcP*)malloc(sizeof(ArcP));        p->iNext->l=l;        p->iNext->num=b;        p->iNext->iNext=NULL;        p=AdjList[b].first;        while(p->iNext!=NULL)        {            p=p->iNext;        }        p->iNext=(ArcP*)malloc(sizeof(ArcP));        p->iNext->l=l;        p->iNext->num=a;        p->iNext->iNext=NULL;}printf("%d\n",bfs());return 0;}




0 0
原创粉丝点击