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;
using ll = long long;
#define fi first
#define se second
#define mp make_pair
#define eb emplace_back
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORU(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define IOS cin.tie(0)->sync_with_stdio(false);
#define PROB "APIO14_beads"
void Fi(){
if(fopen(PROB".inp", "r")){
freopen(PROB".inp", "r", stdin);
freopen(PROB".out", "w", stdout);
}
}
const int N = 2e5 + 1;
const ll INF = 1e15;
int n;
ll dp[2][N];
using pi = pair<int, int>;
vector<pi> adj[N];
void dfs(int u, int p = -1){
ll dif[2] = {-INF, -INF}, tot = 0;
for(auto [w, v]: adj[u]) if(v != p){
dfs(v, u);
tot += max(dp[0][v], dp[1][v] + w);
ll spcl = (dp[0][v] + w) - max(dp[0][v], dp[1][v] + w);
if(spcl > dif[0]){
dif[1] = dif[0];
dif[0] = spcl;
} else dif[1] = max(dif[1], spcl);
}
dp[0][u] = max(tot, tot + dif[0] + dif[1]);
dp[1][u] = tot + dif[0];
}
int main(){
IOS;
Fi();
cin >> n;
FOR(i, 1, n){
int u, v, w;
cin >> u >> v >> w;
adj[u].eb(w, v);
adj[v].eb(w, u);
}
dfs(1);
cout << dp[0][1];
return 0;
}
Compilation message (stderr)
beads.cpp: In function 'void Fi()':
beads.cpp:20:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | freopen(PROB".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
beads.cpp:21:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
21 | freopen(PROB".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... |