http://poj.org/problem?id=1985 (Topic link)
Test instructions
Find the longest distance between two points on the tree. Please see poj1984 for the topic background and input description.
Solution
The diameter of the tree.
Code
poj1985#include<algorithm> #include <iostream> #include <cstdlib> #include <cstring># include<cstdio> #include <cmath> #define LL long long#define MOD 100000000#define inf 2147483640#define Pi ACOs ( -1.0) #define FREE (a) freopen (a ".", "R", stdin), Freopen (a ". Out", "w", stdout); using namespace Std;const int maxn= 40010;struct Edge {int to,next,w;} E[maxn<<1];int n,m,rt,ans,cnt,head[maxn];void Link (int u,int v,int W) {e[++cnt].to=v;e[cnt].next=head[u];head[ U]=cnt;e[cnt].w=w;e[++cnt].to=u;e[cnt].next=head[v];head[v]=cnt;e[cnt].w=w;} void Dfs (int x,int fa,int d) {if (ans<d) ans=d,rt=x;for (int i=head[x];i;i=e[i].next) if (E[I].TO!=FA) DFS (e[i].to,x,d+ E[I].W);} int main () {scanf ("%d%d", &n,&m), char ch;for (int u,v,w,i=1;i<=m;i++) {scanf ("%d%d%d%c", &u,&v, &W,&CH); link (u,v,w);} Rt=ans=0;dfs (1,0,0);d FS (rt,0,0);p rintf ("%d", ans); return 0;}
"poj1985" Cow Marathon