树的直径问题poj2631

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

找出树的直径的方法:任选一个点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 6
1 4 5
6 3 9
2 6 8
6 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;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值