# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
135082 | 2019-07-23T15:42:17 Z | ly20 | Beads and wires (APIO14_beads) | C++17 | 11 ms | 10232 KB |
#include<bits/stdc++.h> using namespace std; const int MAXN=212345,INF=1123456789; vector<int> grafo[MAXN],peso[MAXN]; int dp[MAXN][5],marc[MAXN]; void dfs(int v,int p) { marc[v]=1; if(v!=0 && grafo[v].size()==1) { dp[v][0]=0;dp[v][1]=p;dp[v][2]=0; return; } int d1=-INF,d2=-INF,tot=0; for(int i=0;i<grafo[v].size();i++) { int viz=grafo[v][i],ps=peso[v][i]; if(marc[viz]==1)continue; dfs(viz,ps); if(dp[viz][1]-max(dp[viz][0],dp[viz][2])>d1)d1=dp[viz][1]-max(dp[viz][0],dp[viz][2]); if(d1>d2)swap(d1,d2); tot+=max(dp[viz][0],dp[viz][2]); } dp[v][0]=max(tot+d1+d2,tot); dp[v][2]=tot+d2+p; dp[v][1]=max(tot+d1+d2+p,tot+p); } int cn[MAXN]; int main() { int n; scanf("%d",&n); for(int i=0;i<n-1;i++) { int a,b,p; scanf("%d %d %d",&a,&b,&p);a--;b--; grafo[a].push_back(b);grafo[b].push_back(a); peso[a].push_back(p);peso[b].push_back(p); } int resp=0; for(int i=0;i<n;i++) { if(marc[i]==0) { dfs(i,0); resp+=max(dp[i][0],dp[i][2]); } } printf("%d\n",resp); /*for(int i=0;i<n;i++) { printf("%d %d %d %d\n",i,dp[i][0],dp[i][1],dp[i][2]); }*/ return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 11 ms | 10232 KB | Output is correct |
2 | Incorrect | 11 ms | 10232 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 11 ms | 10232 KB | Output is correct |
2 | Incorrect | 11 ms | 10232 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 11 ms | 10232 KB | Output is correct |
2 | Incorrect | 11 ms | 10232 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 11 ms | 10232 KB | Output is correct |
2 | Incorrect | 11 ms | 10232 KB | Output isn't correct |
3 | Halted | 0 ms | 0 KB | - |