# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
96168 | Retro3014 | Beads and wires (APIO14_beads) | C++17 | 6 ms | 4992 KiB |
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 <iostream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#define MAX_N 200000
#define INF 2000000000LL
using namespace std;
typedef long long ll;
int N;
int ans;
struct S{
S (int idx, ll data) : idx(idx), data(data) {}
int idx;ll data;
};
vector<S> gp[MAX_N+1];
vector<S> p;
ll dp[MAX_N+1][2];
void dfs(int x){
for(int i=0; i<gp[x].size(); i++){
if(gp[x][i].idx!=p[x].idx){
p[gp[x][i].idx].idx = x;
p[gp[x][i].idx].data = gp[x][i].data;
dfs(gp[x][i].idx);
}
}
ll mx = 0;
for(int i=0; i<gp[x].size(); i++){
if(gp[x][i].idx==p[x].idx) continue;
mx += max(dp[gp[x][i].idx][0], dp[gp[x][i].idx][1]);
}
dp[x][0] = mx;
dp[x][1] = -INF;
ll sum = mx;
bool tf = false;
mx = 0;
for(int i=0; i<gp[x].size(); i++){
if(gp[x][i].idx==p[x].idx) continue;
if(!tf){
tf = true;
mx = dp[gp[x][i].idx][0]-max(dp[gp[x][i].idx][0], dp[gp[x][i].idx][1])+gp[x][i].data;
}else{
dp[x][0] = max(dp[x][0], sum+mx+(dp[gp[x][i].idx][0]-max(dp[gp[x][i].idx][0], dp[gp[x][i].idx][1])+gp[x][i].data));
mx = max(mx, dp[gp[x][i].idx][0]-max(dp[gp[x][i].idx][0], dp[gp[x][i].idx][1])+gp[x][i].data);
}
dp[x][1] = max(dp[x][1], sum + p[x].data + (dp[gp[x][i].idx][0]-max(dp[gp[x][i].idx][0], dp[gp[x][i].idx][1])+gp[x][i].data));
}
return;
}
int main(){
scanf("%d", &N);
for(int i=0; i<=N; i++) p.push_back({0, 0});
for(int i=0; i<N-1; i++){
int a, b;ll c;
scanf("%d%d%lld", &a, &b, &c);
gp[a].push_back({b, c}); gp[b].push_back({a, c});
}
dfs(1);
printf("%lld", dp[1][0]);
return 0;
}
Compilation message (stderr)
# | 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... |