代码
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <bitset>
#include <vector>
using namespace std;
void fre() { system("clear"), freopen("A.txt", "r", stdin); freopen("Ans.txt","w",stdout); }
void Fre() { system("clear"), freopen("A.txt", "r", stdin);}
void Run(int x = 0) {
#ifdef ACM
if (! x) fre(); else Fre();
#endif
}
#define ios ios::sync_with_stdio(false)
#define Pi acos(-1)
#define pb push_back
#define fi first
#define se second
#define db double
#define ll long long
#define ull unsigned long long
#define Pir pair<ll, ll>
#define m_p make_pair
#define for_(i, s, e) for(ll i = (ll)(s); i <= (ll)(e); i ++)
#define rep_(i, e, s) for(ll i = (ll)(e); i >= (ll)(s); i --)
#define memset(a, b, c) memset(a, (int)b, c);
#define size() size() * 1LL
#define sc scanf
#define pr printf
#define sd(a) sc("%lld", &a)
#define ss(a) sc("%s", a)
#define __ pr( "------------------------\n" );
#define ___ pr("\n------------------------\n");
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define esp 1e-7
#define mod (ll)(1e9 + 7)
const ll mxn = 40005;
struct Edge
{
ll v, w, next;
} edge[mxn << 1];
ll head[mxn], tot;
ll c, d[mxn];
void Add(ll u, ll v, ll w)
{
edge[++ tot].v = v;
edge[tot].w = w;
edge[tot].next = head[u];
head[u] = tot;
}
void dfs(ll u, ll p)
{
for (ll i = head[u]; i; i = edge[i].next)
{
ll v = edge[i].v;
ll w = edge[i].w;
if(v == p) continue;
d[v] = d[u] + w;
if(d[v] > d[c]) c = v;
dfs(v, u);
}
}
int main()
{
Run();
ll n, m;
sc("%lld %lld", &n, &m);
ll u, v, w; char ch;
for_(i, 2, n)
{
sc("%lld %lld %lld %c ", &u, &v, &w, &ch);
Add(u, v, w);
Add(v, u, w);
}
dfs(1, 0);
d[c] = 0;
dfs(c, 0);
pr("%lld\n", d[c]);
return 0;
}