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], p[N], a, b, c, maxx;
void dfs(int u){
int maxx = -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]);
if (f[v][1]+cost > f[v][0]) maxx = max(maxx, f[v][0] - f[v][1]);
else maxx = max(maxx, cost);
}
f[u][1] = f[u][0] + maxx;
}
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));
}
for (int i = 1; i <= n; i++){
p[i] = 0;
dfs(i);
maxx = max(maxx, f[i][0]);
}
cout << maxx;
}
/*
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:14:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < adj[u].size(); i++){
~~^~~~~~~~~~~~~~~
beads.cpp:20:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < adj[u].size(); i++){
~~^~~~~~~~~~~~~~~
# | 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... |