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>
const int N = 2e5 + 5;
const int inf = 1e9;
using namespace std;
typedef pair <int, int> ii;
vector <ii> adj[N];
int n, f[N][2], g[N][2], p[N], a, b, c, maxxx, co[N], ans[N];
int maxx[N][2], L[N][2], R[N][2];
void dfs(int u){
int ma = -inf;
f[u][1] = -inf;
f[u][0] = 0;
for(int i = 0; i < adj[u].size(); i++){
int v = adj[u][i].first;
if (v == p[u]) continue;
p[v] = u;
dfs(v);
}
for (int i = 0; i < adj[u].size(); i++){
int v = adj[u][i].first, cost = adj[u][i].second;
if (v == p[u]) continue;
f[u][0] += max(f[v][1]+cost, f[v][0]);
co[v] = cost;
if (f[v][1]+cost > f[v][0]) ma = max(ma, f[v][0] - f[v][1]);
else ma = max(ma, cost);
}
f[u][1] = f[u][0] + ma;
}
void DFS(int u){
int cak = co[u], sz = adj[u].size();
if (g[u][1] + co[u] > g[u][0]) cak = g[u][0] - g[u][1];
ans[u] = g[u][0] + f[u][0];
L[0][0] = 0;
L[0][1] = -inf;
maxx[0][0] = -inf;
for (int i = 0; i < sz; i++){
int v = adj[u][i].first, cost = adj[u][i].second;
if (v == p[u]) {
L[i+1][0] = L[i][0];
L[i+1][1] = L[i][1];
maxx[i+1][0] = maxx[i][0];
continue;
}
L[i+1][0] = L[i][0] + max(f[v][0], f[v][1] + cost);
int z;
if (f[v][0] > f[v][1] + cost) z = cost;
else z = f[v][0] - f[v][1];
maxx[i+1][0] = max(maxx[i][0], z);
L[i+1][1] = L[i+1][0] + maxx[i+1][0];
}
R[sz+1][0] = 0;
R[sz+1][1] = -inf;
maxx[sz+1][1] = -inf;
for (int i = sz-1; i >= 0; i--){
int v = adj[u][i].first, cost = adj[u][i].second;
if (v == p[u]) {
R[i+1][0] = R[i+2][0];
R[i+1][1] = R[i+2][1];
maxx[i+1][1] = maxx[i+2][1];
continue;
}
R[i+1][0] = R[i+2][0] + max(f[v][0], f[v][1] + cost);
int z;
if (f[v][0] > f[v][1] + cost) z = cost;
else z = f[v][0] - f[v][1];
maxx[i+1][1] = max(maxx[i+2][1], z);
R[i+1][1] = R[i+1][0] + maxx[i+1][1];
}
for (int i = 0; i < sz; i++){
int v = adj[u][i].first, cost = adj[u][i].second;
if (v == p[u]) continue;
g[v][0] = max(g[v][0], g[u][0] + L[i][0] + R[i+2][0]);
g[v][0] = max(g[v][0], g[u][1] + L[i][0] + R[i+2][0] + cost);
g[v][0] = max(g[v][0], g[u][0] + L[i][1] + R[i+2][0] + cost);
g[v][0] = max(g[v][0], g[u][0] + L[i][0] + R[i+2][1] + cost);
g[v][1] = g[u][0] + L[i][0] + R[i+2][0] + cost;
}
for (int i = 0; i < sz; i++){
int v = adj[u][i].first;
if (v == p[u]) continue;
DFS(v);
}
}
int main(){
cin >> n;
for (int i = 1; i < n; i++){
cin >> a >> b >> c;
adj[a].push_back(ii(b, c));
adj[b].push_back(ii(a, c));
}
dfs(1);
g[1][1] = -inf;
DFS(1);
for (int i = 1; i <= n; i++){
maxxx = max(maxxx, ans[i]);
}
cout << maxxx;
}
/*
5
1 2 10
1 3 40
1 4 15
1 5 20
10
4 10 2
1 2 21
1 3 13
6 7 1
7 9 5
2 4 3
2 5 8
1 6 55
6 8 34
*/
Compilation message (stderr)
beads.cpp: In function 'void dfs(int)':
beads.cpp:15:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[u].size(); i++){
~~^~~~~~~~~~~~~~~
beads.cpp:21:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < adj[u].size(); i++){
~~^~~~~~~~~~~~~~~
beads.cpp: In function 'void DFS(int)':
beads.cpp:33:9: warning: variable 'cak' set but not used [-Wunused-but-set-variable]
int cak = co[u], sz = adj[u].size();
^~~
# | 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... |