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++14 (judge verdict: Accepted)

#include "bits/stdc++.h"
using namespace std;
typedef long long int ll;
typedef vector<ll> vl;
typedef pair<ll, ll> PP;
#define rep(i,n) for(ll i = 0; i < ll(n); i++)
#define all(v) v.begin() , v.end()
#define inputv(v,n) rep(i,n){\
    ll x;\
    cin>>x;\
    v.push_back(x);\
    }
const ll INF = 999999999999999;
const ll MOD = 1000000007;
ll a, b, c, d, e, f, p, t, x, y, z, q, m, n, r, h, k, ans = 0, w, l;
vector<PP> gragh[200010];
vl visited1(200010, 0), cost(200010, INF);
void dfs1(ll x, ll k) {
	visited1[x] = 1;
	cost[x] = k;
	for (PP i : gragh[x]) {
		if (visited1[i.first] == 0) {
			dfs1(i.first, k + i.second);
		}
	}
}
void input() {
	cin >> n;
	rep(i, n-1) {
		cin >> a >> b >> c;
		gragh[a].push_back({ b,c });
		gragh[b].push_back({ a,c });
	}
}
void solve() {
	dfs1(0, 0);

	a = 0;
	rep(i, n) {
		if (a < cost[i]) {
			a = cost[i];
			b = i;
		}
	}

	visited1 = vl(n, 0);
	cost = vl(n, 0);
	dfs1(b, 0);
	sort(all(cost));
	reverse(all(cost));

	cout << cost[0] << endl;

}

int main() {
	input();
	solve();
}