Poj1985 diameter of the cow marathon tree

Source: Internet
Author: User

Find the diameter of a tree.

Method:

Select a vertex as root and start DFS to find the farthest vertex, which must be an endpoint in the tree diameter (strict tree, no ring)

Then, use the point just found as the root's second DFS. The farthest distance found this time is the diameter of the tree.

 

Cow marathon
Time limit:2000 ms   Memory limit:30000 K
Total submissions:2963   Accepted:1484
Case time limit:1000 ms

Description

After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has committed to create a bovine marathon for his cows to run. the Marathon route will include a pair of farms and
Path comprised of a sequence of roads between them. since FJ wants the cows to get as much exercise as possible he wants to find the two farms on his map that are the farthest apart from each other (distance being measured in terms of total length road
On the path between the two farms). Help him determine the distances between this farthest pair of farms.

Input

* Lines 1...: same input format as "navigation nightmare ".

Output

* Line 1: an integer giving the distance between the farthest pair of farms.

Sample Input

7 61 6 13 E6 3 9 E3 5 7 S4 1 3 N2 4 20 W4 7 2 S

Sample output

52

 

#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;#define MAXN  500000struct edge{    int to,dis,next;}e[9999999];int head[MAXN],en;int dis[MAXN];void add(int u,int v ,int w){    e[en].to=v;    e[en].dis=w;    e[en].next=head[u];    head[u]=en++;}void dfs(int f,int u,int d){    dis[u]=d;    for(int i=head[u];i!=-1;i=e[i].next)        if(e[i].to!=f)            dfs(u,e[i].to,d+e[i].dis);}int u,v,c;int n,m;char s[20];int main(){    while(~scanf("%d%d",&n,&m))    {        memset(head,-1,sizeof(head));        en=0;        for(int i=0;i<m;i++)        {            scanf("%d%d%d%s",&u,&v,&c,s);            add(u,v,c);            add(v,u,c);        }        dfs(0,1,0);        int maxs,tag;        maxs=-1;tag=0;        for(int i=1;i<=n;i++)        {            if(maxs<dis[i])            {                maxs=dis[i];                tag=i;            }        }        dfs(0,tag,0);        maxs=-1;        for(int i=1;i<=n;i++)            maxs=max(maxs,dis[i]);        printf("%d\n",maxs);    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.