This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// from duckindog wth depression
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 10,
M = -1e15;
int n;
vector<pair<int, int>> ad[N];
int f[N][3];
void dfs(int u, int pre = 0) {
int all = M, none = M;
int s = 0;
vector<int> best;
map<int, int> rvs;
for (auto duck : ad[u]) {
int v, w; tie(v, w) = duck;
if (v == pre) continue;
dfs(v, u);
int x = f[v][0], y = f[v][1];
all = max(0ll, all);
if (x > 0) {
all += max(x + w, y);
none = max(none, y - (x + w > y ? x : y));
} else none = max(none, y + w);
f[u][1] += max({0ll, f[v][1], f[v][0] + w});
rvs[v] = best.size();
best.push_back(max(f[v][0] + w, 0ll));
}
f[u][0] = max(M, all + none);
for (int i = 1; i < best.size(); ++i) best[i] += best[i - 1];
f[u][2] = M;
for (auto duck : ad[u]) {
int v, w; tie(v, w) = duck;
int p = rvs[v];
int lt = !p ? 0 : best[p - 1];
for (auto chik : ad[u]) {
int _v, _w; tie(_v, _w) = chik;
int _p = rvs[_v];
if (_p <= p) continue;
int rt = best[best.size() - 1] - best[_p];
f[u][2] = max(f[u][2], lt + rt + max(f[v][2] + f[_v][1], f[v][1] + f[_v][2]));
}
}
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
if (fopen("duck.inp", "r")) {
freopen("duck.inp", "r", stdin);
freopen("duck.out", "w", stdout);
}
cin >> n;
for (int i = 1; i < n; ++i) {
int u, v, w; cin >> u >> v >> w;
ad[u].push_back({v, w});
ad[v].push_back({u, w});
}
int answer = 0;
for (int i = 1; i <= n; ++i) {
memset(f, 0, sizeof f);
f[0][0] = f[0][1] = f[0][2] = M;
dfs(i);
for (int j = 1; j <= n; ++j) answer = max({answer, f[j][1], f[j][2]});
}
cout << answer;
}
Compilation message (stderr)
beads.cpp: In function 'void dfs(long long int, long long int)':
beads.cpp:37:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for (int i = 1; i < best.size(); ++i) best[i] += best[i - 1];
| ~~^~~~~~~~~~~~~
beads.cpp:17:7: warning: unused variable 's' [-Wunused-variable]
17 | int s = 0;
| ^
beads.cpp: In function 'int32_t main()':
beads.cpp:59:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | freopen("duck.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
beads.cpp:60:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | freopen("duck.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |