AcWing 1207. 大臣的旅费
原题链接
中等
作者:
tire
,
2025-04-10 19:38:15
· 山西
,
所有人可见
,
阅读 2
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5+10;
const int mod = 100003;
int n,m;
int qmi(int a,int k){
int res = 1;
while(k){
if(k&1) res = res * a % mod;
a = a * a % mod;
k >>= 1;
}
return res;
}
vector<pair<int,int>> q[N];
int dp[N];
void solve(){
cin >> n;
int m = n - 1;
while(m--){
int a,b,c;
cin >> a >> b >> c;
q[a].push_back({b,c});
q[b].push_back({a,c});
}
int res = 0;
auto dfs = [&](auto&& dfs,int u,int pa) -> void{
dp[u] = 0;
for(auto [x,y]:q[u]){
if(x == pa) continue;
dfs(dfs,x,u);
res = max({res,dp[u] + dp[x] + y});
dp[u] = max({dp[u],dp[x]+y});
}
};
dfs(dfs,1,0);
cout << (11 + 11 + res - 1) * res / 2 << '\n';
}
signed main(){
int _ = 1;
while(_--){
solve();
}
return 0;
}