The diameter of the tree "bzoj3363" [Usaco2004 feb]cow Marathon Cow Marathon

Source: Internet
Author: User

3363: [Usaco2004 feb]cow Marathon Dairy Marathon
Description

? The recent obesity epidemic in the United States has been widespread, with farmer John holding a dairy marathon to make his cows more athletic. Horse

The pine route should be as long as possible, so tell your farm map (the map's description is consistent with the above), please help John Look for two

The distance between the farthest farms.

Input

? Line 1th: Two separate integers n and M.

? 2nd to m+1: Each line consists of 4 separate contents, Fi,f2,l,d describes the number of two farms, the length of the road

Degrees, F1 to the direction of F2 n,e,s,w.

Output

? An integer that represents the distance between the maximum of two clothing fields.

Look for the diameter of the tree, water.

M and opt are useless.

Code

#include <iostream> #include <cstdio> #include <cstring>using namespace std;const int wx=500017;    inline int read () {int Sum=0,f=1;char ch=getchar (); while (ch< ' 0 ' | |    Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}    while (ch>= ' 0 ' &&ch<= ' 9 ') {sum= (sum<<1) + (sum<<3) +ch-' 0 '; Ch=getchar ();} return sum*f;} struct e{int Nxt,to,dis;} Edge[wx];int n,m,k,num;int ans,maxn,pos;int head[wx],dis[wx];void Add (int from,int to,int dis) {edge[++num].nxt=head[f    ROM];    Edge[num].to=to;    Edge[num].dis=dis; Head[from]=num;}        void Dfs (int u,int fa) {for (int i=head[u];i;i=edge[i].nxt) {int v=edge[i].to;        if (V==FA) continue;        Dis[v]=dis[u]+edge[i].dis;    DFS (V,U);    }}int Main () {n=read (); Read ();        for (int i=1;i<n;i++) {int x, y, Z;        X=read (); Y=read (); Z=read (); scanf ("%s");    Add (x, y, z); add (y,x,z);    } dfs (1,0);        for (int i=1;i<=n;i++) {if (Maxn<dis[i]) {maxn=dis[i];p os=i; }    }   memset (dis,0,sizeof dis);    DFS (pos,0);    for (int i=1;i<=n;i++) {Ans=max (ans,dis[i]);    } printf ("%d\n", ans); return 0;}

Tree Diameter "bzoj3363" [Usaco2004 feb]cow Marathon Cow Marathon

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.