# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
122428 |
2019-06-28T07:55:16 Z |
이온조(#2989) |
Beads and wires (APIO14_beads) |
C++14 |
|
8 ms |
5120 KB |
#include <bits/stdc++.h>
using namespace std;
using pii = pair<int, int>;
vector<pii> adj[200009];
int D[3][200009], V[200009];
int go(int x, int p, int pc) {
for(auto& it: adj[x]) if(it.first != p) go(it.first, x, it.second);
int s = 0, m1 = -1, m2 = -1;
for(auto& it: adj[x]) if(it.first != p) s += max(D[0][it.first], D[1][it.first]);
for(auto& it: adj[x]) if(it.first != p) {
int n, c; tie(n, c) = it;
V[n] = D[0][n] + c - max(D[0][n], D[1][n]);
if(m1 == -1 || V[m1] <= V[n]) m2 = m1, m1 = n;
else if(m2 == -1 || V[m2] <= V[n]) m2 = n;
}
D[0][x] = s;
if(m1 != -1) D[1][x] = s + pc + V[m1];
else D[1][x] = -1e9;
D[2][x] = -1e9;
if(m2 != -1) D[2][x] = s + V[m1] + V[m2];
for(auto& it: adj[x]) if(it.first != p) {
int n, c; tie(n, c) = it;
if(n == m1) D[2][x] = max(D[2][x], s - max(D[0][n], D[1][n]) + D[2][n] + c + V[m2]);
else D[2][x] = max(D[2][x], s - max(D[0][n], D[1][n]) + D[2][n] + c + V[m1]);
}
int mx = -1e9;
for(auto& it: adj[x]) if(it.first != p) {
int n, c; tie(n, c) = it;
mx = max(mx, D[2][c] - max(D[0][c], D[1][c]));
}
D[2][x] = max(D[2][x], s + mx);
// printf("x: %d, D0: %d, D1: %d, D2: %d, m1: %d, m2: %d, s: %d\n", x, D[0][x], D[1][x], D[2][x], m1, m2, s);
}
int main() {
int N; scanf("%d",&N);
for(int i=0; i<N-1; i++) {
int u, v, w; scanf("%d%d%d",&u,&v,&w);
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
go(1, 1, -1e9);
printf("%d", max(D[0][1], D[2][1]));
return 0;
}
Compilation message
beads.cpp: In function 'int go(int, int, int)':
beads.cpp:37:1: warning: no return statement in function returning non-void [-Wreturn-type]
}
^
beads.cpp: In function 'int main()':
beads.cpp:40:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int N; scanf("%d",&N);
~~~~~^~~~~~~~~
beads.cpp:42:21: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int u, v, w; scanf("%d%d%d",&u,&v,&w);
~~~~~^~~~~~~~~~~~~~~~~~~
# |
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 |
Incorrect |
8 ms |
5120 KB |
Output isn't correct |
4 |
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 |
Incorrect |
8 ms |
5120 KB |
Output isn't correct |
4 |
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 |
Incorrect |
8 ms |
5120 KB |
Output isn't correct |
4 |
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 |
Incorrect |
8 ms |
5120 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |