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>
#pragma GCC optimize(2)
using namespace std;
int dp[10010][2];
vector<pair<int, int>> adj[10010];
void dfs(int n, int p) {
dp[n][0] = 0;
dp[n][1] = -1000000000;
int m1=-1000000000;
for(auto [i, j]: adj[n]) {
if(i!=p) {
dfs(i, n);
int X = max(dp[i][1]+j, dp[i][0]);
dp[n][0]+=X;
int x = dp[i][0] + j - X;
m1 = max(m1, x);
}
}
dp[n][1]=dp[n][0]+m1;
}
int main() {
cin.sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
for(int i = 1; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
adj[a].push_back({b, c});
adj[b].push_back({a, c});
}
int mx = 0;
for(int i = 1; i <= n; i++) {
dfs(i, 0);
mx = max(mx, dp[i][0]);
}
cout << mx << '\n';
}
# | 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... |