This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define Sz(x) (x).size()
const int N = 1e5 + 10;
long long dp[N][2];
vector<pair<int, int> > adj[N];
void DFS(int x, int p = 0, int cnt = 0) {
for(auto [j, t] : adj[x]) {
if(j == p) continue;
DFS(j, x, t);
}
if(Sz(adj[x]) == 1 && x != 1) {
return;
}
long long sum = 0;
for(auto [j, t] : adj[x]) {
if(j == p) continue;
sum += dp[j][1];
}
dp[x][0] = sum;
vector<int> vc;
for(auto [j, t] : adj[x]) {
if(j == p) continue;
vc.push_back(dp[j][0] + t - dp[j][1]);
}
sort(vc.begin(), vc.end(), greater<int>());
if(vc.size() > 1) {
dp[x][0] = max(dp[x][0], sum + vc[0] + vc[1]);
}
dp[x][1] = dp[x][0];
if(vc.size() > 0) {
dp[x][1] = max(dp[x][1], sum + vc[0] + cnt);
}
}
int main() {
int n; cin >>n;
for(int i = 0; i < n - 1; i++) {
int u, v, c; cin >>u >>v >>c;
adj[u].push_back({v, c}), adj[v].push_back({u, c});
}
DFS(1);
cout<<dp[1][0];
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |