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 pb emplace_back
#define int long long
const int N = 2e5 + 1;
typedef pair<int,int> ii;
vector<ii> g[N];
int f[N][2];
void dfs(int u,int p) {
int mx1 = -1e9;
int mx2 = -1e9;
for(ii e : g[u]) {
int v = e.first;
int c = e.second;
if (v == p) continue;
dfs(v,u);
f[u][0] += max(f[v][1] + c,f[v][0]);
if (mx2 < min(c,f[v][0] - f[v][1]))
mx2 = min(c,f[v][0] - f[v][1]);
if (mx1 < mx2)
swap(mx1,mx2);
}
f[u][1] = f[u][0] + mx1;
f[u][0] = f[u][0] + max(mx1 + mx2,0ll);
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n; cin >> n;
for(int i = 1 ; i < n ; ++i) {
int x; cin >> x;
int y; cin >> y;
int c; cin >> c;
g[x].pb(y,c);
g[y].pb(x,c);
}
dfs(1,0);
cout << f[1][0] << endl;
}
/*
5
1 2 10
1 3 40
1 4 15
1 5 20
*/
# | 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... |