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 MAXN 200005
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
int N;
int D[MAXN], E[MAXN], F[MAXN], G[MAXN];
int up[MAXN], V[MAXN];
vector <int> con[MAXN], conv[MAXN];
void dfs(int n, int from)
{
vector <int> arr;
for (int i=sz(con[n]);i--;){
int t = con[n][i], v = conv[n][i];
if (t == from) continue;
up[t] = v; dfs(t, n);
arr.pb(t);
}
D[n] = E[n] = F[n] = G[n] = 0;
for (int t: arr){
V[t] = max(max(D[t], E[t]), max(F[t], G[t]));
D[n] += V[t];
}
int mx1 = -2e9, mx2 = -2e9;
for (int t: arr){
if (mx1 > -2e9){
E[n] = max(E[n], D[n] + mx1 + E[t] - V[t] + up[t]);
E[n] = max(E[n], D[n] + max(mx1, mx2) + D[t] - V[t] + up[t]);
}
mx1 = max(mx1, -V[t]+D[t]+up[t]);
mx2 = max(mx2, -V[t]+E[t]+up[t]);
}
for (int t: arr){
F[n] = max(F[n], D[n] - V[t] + D[t] + up[t] + up[n]);
G[n] = max(G[n], D[n] - V[t] + E[t] + up[t] + up[n]);
}
}
int main()
{
scanf("%d", &N);
for (int i=1;i<N;i++){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
con[a].pb(b); conv[a].pb(c);
con[b].pb(a); conv[b].pb(c);
}
dfs(1, 0);
printf("%d\n", max(D[1], E[1]));
}
Compilation message (stderr)
beads.cpp: In function 'int main()':
beads.cpp:45:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
beads.cpp:48:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &a, &b, &c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | 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... |