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;
const int N = 200010;
const int inf = 2e9 + 12345;
int n;
int f[N][2];
vector < pair<int,int> > G[N];
int ans;
void dfs(int u, int p) {
vector < pair<int,int> > ch;
for (auto edge: G[u]) if (edge.second != p) {
dfs(edge.second, u);
ch.push_back(edge);
}
f[u][1] = -inf;
f[u][0] = 0;
for (auto edge: ch) {
f[u][0] += max(f[edge.second][0], f[edge.second][1] + edge.first);
}
for (auto edge: ch) {
int mx = f[u][0];
mx -= max(f[edge.second][0], f[edge.second][1] + edge.first);
mx += (f[edge.second][0] + edge.first);
f[u][1] = max(f[u][1], mx);
}
}
void re_dfs(int u, int p) {
for (auto edge: G[u]) if (edge.second != p) {
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v, w; cin >> u >> v >> w;
G[u].push_back(make_pair(w,v)); G[v].push_back(make_pair(w,u));
}
for (int i = 1; i <= n; ++i) {
dfs(i,i);
ans = max(ans, f[i][0]);
}
cout << ans << 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... |