Problem:
Diameter of a Tree

Given a tree T with non-negative weight, find the diameter of the tree.

The diameter of a tree is the maximum distance between two nodes in a tree.

Input

n
s1 t1 w1
s2 t2 w2
:
sn-1 tn-1 wn-1

The first line consists of an integer n which represents the number of nodes in the tree. Every node has a unique ID from 0 to n-1 respectively.

In the following n-1 lines, edges of the tree are given.

si and ti represent end-points of the i-th edge (undirected) and wi represents the weight (distance) of the i-th edge.

Output

Print the diameter of the tree in a line.

Constraints

1 ≤ n ≤ 100,000

0 ≤ wi ≤ 1,000

Sample Input 1

4
0 1 2
1 2 1
1 3 3

Sample Output 1

5

Sample Input 2

4
0 1 1
1 2 2
2 3 4

Sample Output 2

7

Solution in C (judge verdict: Runtime Error)

#include <stdio.h>
#include <string.h>
#define INF 999999
struct node{
    int to,next,w;
}e[100100];
int cnt=0,head[100100],n,d[100100],q[100100];
void addedge(int u,int v,int w){//?接表 
    cnt++,e[cnt].to=v,e[cnt].next=head[u],e[cnt].w=w,head[u]=cnt;
}
void bfs(int x){
    int h,t,i,u,v,b;
    for(i=0;i<n;i++)d[i]=INF;
    h=t=1;
    q[t]=x;
    t++;
    d[x]=0;
    while(h<t){
        u=q[h];
        for(b=head[u];b;b=e[b].next){
            v=e[b].to;
            if(d[v]==INF){
                d[v]=d[u]+e[b].w;
                q[t]=v;
                t++;
            }
        }
        h++;
    }
}
int main(){
    int i,u,v,w,k,maxv;
    memset(head,0,sizeof(head));
    scanf("%d",&n);
    for(i=1;i<n;i++){
        scanf("%d%d%d",&u,&v,&w);
        addedge(u,v,w);//无向? 
        addedge(v,u,w);
    }
    bfs(0);
    maxv=-1;
    for(i=0;i<n;i++)
        if(d[i]!=INF&&maxv<d[i]){
            maxv=d[i];
            k=i;
        }
    bfs(k);
    maxv=-1;
    for(i=0;i<n;i++)
        if(d[i]!=INF&&maxv<d[i])
            maxv=d[i];
    printf("%d\n",maxv);//1 一个想不到的??，此?写成 printf("%d\n",d[i]); 竟然?了会 
    return 0;
}