# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
135229 | pedro_sponchiado | Beads and wires (APIO14_beads) | C++17 | 15 ms | 14456 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;
const int maxn=200000;
const int INF=1000000000;
int n;
vector<int> adj[maxn], peso[maxn];
int dp1[maxn], dp2[maxn];
int marc[maxn];
vector<pair<int, int> > filhos[maxn];
vector<int> aux;
void dfs(int u, int p_ar_ant){
marc[u]=1;
int folha=1;
for(int i=0; i<adj[u].size(); i++){
int v=adj[u][i];
if(marc[v]==0){
folha=0;
dfs(v, peso[u][i]);
filhos[u].push_back(make_pair(dp1[v]+peso[u][i], max(dp1[v], dp2[v])));
}
}
if(folha){
dp1[u]=0;
dp2[u]=-INF;
return;
}
// printf("%d:\n", u);
int t=filhos[u].size();
aux.clear();
int s=0;
for(int i=0; i<t; i++){
// if(u==7) printf("%d %d\n", filhos[u][i].first, filhos[u][i].second);
s+=filhos[u][i].second;
aux.push_back(filhos[u][i].first-filhos[u][i].second);
}
sort(aux.begin(), aux.end());
//calcula dp1[u]
if(t>=2 && (aux[t-1]+aux[t-2]>0) ) dp1[u]=s+aux[t-1]+aux[t-2];
else dp1[u]=s;
//calcula dp2[u]
if(p_ar_ant!=-1) dp2[u]=s+aux[t-1]+p_ar_ant;
return;
}
int main(){
scanf("%d", &n);
for(int a, b, p, i=1; i<=n-1; i++){
scanf("%d %d %d", &a, &b, &p);
adj[a].push_back(b);
adj[b].push_back(a);
peso[a].push_back(p);
peso[b].push_back(p);
}
dfs(1, -1);
/* printf("\n\n");
for(int i=1; i<=n; i++){
printf("%d: %d %d\n", i, dp1[i], dp2[i]);
}
*/
printf("%d\n", dp1[1]);
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... |