Title Description
V8 super like to exercise, especially like to climb trees, in order to catch more insects, he hopes that his own from a leaf node to the root node of the longest path, now give you a tree, output V8 required path length.
Input
Multiple sets of data
The first line, a T, represents the number of data groups
For each set of data that follows
The first row is an N, which represents the number of sides.
The next n rows, each line three number u,v,w, indicate that there is a son with V is U, and there is a right side between the W.
1 is the root node.
All data is less than 10000.
Output
The longest path from the root node to a leaf.
--the body difficulty in storing this tree. Tried C's structure, was distracted by free and had to use a vector of water
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cstdlib>#include<vector>using namespaceStd;typedefLong LongLL;structEdgenode {intNext; intweight;}; typedefstructEdgenode Edge;vector<Edge> tree[10001];intDfsintnode) { intI,sum =0; for(i=0; I<tree[node].size (); i++) {sum= Max (Sum,dfs (tree[node][i].next) +tree[node][i].weight); } returnsum;}intMain () {inttime,t,i,j; scanf ("%d",&T); for(time=1; time<=t;time++){ intN; scanf ("%d",&N); for(i=1; i<=n;i++) tree[i].clear (); for(i=1; i<=n;i++){ intu,v,w; scanf (" %d%d%d",&u,&v,&W); Edge l; L.next= V; L.weight =W; Tree[u].push_back (l); } intRoot; printf ("%d\n", DFS (1)); } return 0; }
Xidianoj 1090 Climbing the V8