# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
100181 | Plurm | Beads and wires (APIO14_beads) | C++11 | 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 <bits/stdc++.h>
using namespace std;
vector<pair<int,int> > g[200005];
long long dp[200005][2];
void dfs(int u, int p = -1){
vector<pair<int,int> > vec;
dp[u][1] = 0ll;
long long smx = -1e12;
long long mx = -1e12;
int ccnt = 0;
for(auto v : g[u]){
if(v.first == p) continue;
dfs(v.first, u);
// Choice 1
long long x = max(dp[v.first][0], dp[v.first][1] + v.second);
// Choice 2
long long y = dp[v.first][0] + v.second;
if(y - x > mx){
smx = mx;
mx = y - x;
}else if(y - x > smx){
smx = y - x;
}
dp[u][0] += x;
dp[u][1] += x;
ccnt++;
}
if(ccnt >= 2){
if(mx + smx >= 0ll) dp[u][0] += mx + smx;
}
if(ccnt >= 1){
dp[u][1] += mx;
}else{
dp[u][1] = -1e12;
}
}
int main(){
int n;
scanf("%d",&n);
for(int i = 1; i < n; i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
g[a].emplace_back(b,c);
g[b].emplace_back(a,c);
}
dfs(1);
printf("%lld\n",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... |