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()
#define All(x) (x).begin(), (x).end()
const int N = 1e5 + 10;
long long dp[N][4];
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;
}
vector<long long> vc;
long long sum = 0;
for(auto [j, t] : adj[x]) {
if(j == p) continue;
sum += dp[j][1];
vc.push_back(dp[j][2] + t - dp[j][1]);
}
sort(All(vc), greater<long long>());
dp[x][0] = sum + vc[0] + cnt;
if(Sz(vc) > 1)
dp[x][0] = max(dp[x][0], sum + vc[0] + vc[1]);
if(Sz(vc) > 1)
dp[x][2] = sum + vc[0] + vc[1];
dp[x][1] = sum + vc[0] + cnt;
dp[x][3] = sum;
}
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);
/*for(int i = 1; i <= n; i++) {
cout<<"^^" <<i <<' ' <<dp[i][0] <<' ' <<dp[i][1] <<' ' <<dp[i][2] <<' ' <<dp[i][3] <<endl;
}*/
cout<<dp[1][2] <<endl;
}
# | 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... |