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>
#define FR(i, N) for (int i = 0; i < int(N); i++)
#define all(x) begin(x), end(x)
#define f first
#define s second
using namespace std;
using ll = long long;
const int MAXN = (int)(2e5) + 5;
vector<pair<int, int>> adj[MAXN];
int dp[MAXN][2];
void dfs(int n, int p, int w) {
vector<int> tot;
int allSum = 0;
for (auto& e : adj[n]) {
if (e.f != p) {
dfs(e.f, n, e.s);
tot.push_back(e.s + dp[e.f][0] - dp[e.f][1]);
allSum += dp[e.f][1];
}
}
sort(all(tot));
reverse(all(tot));
dp[n][0] = allSum;
dp[n][1] = allSum;
for (int i = 0; i < min(2, int(tot.size())); i++) {
allSum += tot[i];
if (i%2 == 0) {
dp[n][1] = max(dp[n][1], allSum + w);
}
else {
dp[n][0] = max(dp[n][0], allSum);
}
}
dp[n][1] = max(dp[n][1], dp[n][0]);
}
int main() {
cin.tie(0);
cin.sync_with_stdio(0);
int N;
cin >> N;
FR(i, N-1) {
int u, v, w;
cin >> u >> v >> w;
u--, v--;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
dfs(0, 0, 0);
cout << dp[0][0] << '\n';
return 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... |