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