# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
96167 | 2019-02-06T15:46:05 Z | Retro3014 | Beads and wires (APIO14_beads) | C++17 | 6 ms | 5120 KB |
#include <iostream> #include <vector> #include <algorithm> #include <stdio.h> #define MAX_N 200000 #define INF 2000000000 using namespace std; int N; int ans; struct S{ S (int idx, int data) : idx(idx), data(data) {} int idx, data; }; vector<S> gp[MAX_N+1]; vector<S> p; int 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); } } int 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; int 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, c; scanf("%d%d%d", &a, &b, &c); gp[a].push_back({b, c}); gp[b].push_back({a, c}); } dfs(1); printf("%d", dp[1][0]); return 0; }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 6 ms | 4992 KB | Output is correct |
2 | Correct | 6 ms | 4992 KB | Output is correct |
3 | Correct | 6 ms | 5120 KB | Output is correct |
4 | Correct | 6 ms | 4992 KB | Output is correct |
5 | Correct | 6 ms | 4992 KB | Output is correct |
6 | Incorrect | 6 ms | 4992 KB | Output isn't correct |
7 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 6 ms | 4992 KB | Output is correct |
2 | Correct | 6 ms | 4992 KB | Output is correct |
3 | Correct | 6 ms | 5120 KB | Output is correct |
4 | Correct | 6 ms | 4992 KB | Output is correct |
5 | Correct | 6 ms | 4992 KB | Output is correct |
6 | Incorrect | 6 ms | 4992 KB | Output isn't correct |
7 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 6 ms | 4992 KB | Output is correct |
2 | Correct | 6 ms | 4992 KB | Output is correct |
3 | Correct | 6 ms | 5120 KB | Output is correct |
4 | Correct | 6 ms | 4992 KB | Output is correct |
5 | Correct | 6 ms | 4992 KB | Output is correct |
6 | Incorrect | 6 ms | 4992 KB | Output isn't correct |
7 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 6 ms | 4992 KB | Output is correct |
2 | Correct | 6 ms | 4992 KB | Output is correct |
3 | Correct | 6 ms | 5120 KB | Output is correct |
4 | Correct | 6 ms | 4992 KB | Output is correct |
5 | Correct | 6 ms | 4992 KB | Output is correct |
6 | Incorrect | 6 ms | 4992 KB | Output isn't correct |
7 | Halted | 0 ms | 0 KB | - |