# |
제출 시각 |
아이디 |
문제 |
언어 |
결과 |
실행 시간 |
메모리 |
122340 |
2019-06-28T05:10:34 Z |
김세빈(#2988) |
구슬과 끈 (APIO14_beads) |
C++14 |
|
252 ms |
25444 KB |
#include <bits/stdc++.h>
using namespace std;
vector <int> T[202020], K;
int V[404040], C[202020], S[404040];
int A[404040], B[404040];
int D[202020], M1[202020], M2[202020];
bool chk[404040];
int n, ans;
int dfs(int p, int r)
{
int k, ret = 1;
for(int &t: T[p]){
if(V[t ^ 1] != r){
k = dfs(V[t ^ 1], p);
S[t ^ 1] = k; S[t] = n - k;
ret += k;
}
}
return ret;
}
int main()
{
int i, u, v, x;
scanf("%d", &n);
for(i=0; i<n-1; i++){
scanf("%d%d%d", V + (i << 1), V + (i << 1 | 1), C + i);
T[V[i << 1]].push_back(i << 1);
T[V[i << 1 | 1]].push_back(i << 1 | 1);
}
dfs(1, 0);
for(i=0; i<n+n-2; i++){
K.push_back(i);
}
sort(K.begin(), K.end(), [&](int &a, int &b){
return S[a] < S[b];
});
for(i=1; i<=n; i++){
M1[i] = M2[i] = -2e9;
}
for(int &t: K){
u = V[t], v = V[t ^ 1];
chk[t] = 1;
if(chk[t ^ 1]){
A[t] = D[u] - max(A[t ^ 1], B[t ^ 1]);
B[t] = A[t] + C[t >> 1] + (A[t ^ 1] - max(A[t ^ 1], B[t ^ 1]) + C[t >> 1] != M1[u]? M1[u] : M2[u]);
}
else{
A[t] = D[u]; B[t] = A[t] + C[t >> 1] + M1[u];
}
D[v] += max(A[t], B[t]); x = A[t] - max(A[t], B[t]) + C[t >> 1];
if(M1[v] < x) M2[v] = M1[v], M1[v] = x;
else if(M2[v] < x) M2[v] = x;
}
for(i=1; i<=n; i++){
ans = max(ans, D[i]);
}
printf("%d\n", ans);
return 0;
}
Compilation message
beads.cpp: In function 'int main()':
beads.cpp:31:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &n);
~~~~~^~~~~~~~~~
beads.cpp:34:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", V + (i << 1), V + (i << 1 | 1), C + i);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5120 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
5 ms |
5120 KB |
Output is correct |
5 |
Correct |
6 ms |
5120 KB |
Output is correct |
6 |
Correct |
6 ms |
5120 KB |
Output is correct |
7 |
Correct |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
6 ms |
5120 KB |
Output is correct |
10 |
Correct |
5 ms |
5120 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
12 |
Correct |
6 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5120 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
5 ms |
5120 KB |
Output is correct |
5 |
Correct |
6 ms |
5120 KB |
Output is correct |
6 |
Correct |
6 ms |
5120 KB |
Output is correct |
7 |
Correct |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
6 ms |
5120 KB |
Output is correct |
10 |
Correct |
5 ms |
5120 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
12 |
Correct |
6 ms |
5120 KB |
Output is correct |
13 |
Correct |
6 ms |
5120 KB |
Output is correct |
14 |
Correct |
6 ms |
5148 KB |
Output is correct |
15 |
Correct |
5 ms |
5120 KB |
Output is correct |
16 |
Correct |
5 ms |
5120 KB |
Output is correct |
17 |
Correct |
6 ms |
5120 KB |
Output is correct |
18 |
Correct |
6 ms |
5120 KB |
Output is correct |
19 |
Correct |
6 ms |
5120 KB |
Output is correct |
20 |
Correct |
6 ms |
5120 KB |
Output is correct |
21 |
Correct |
6 ms |
5120 KB |
Output is correct |
22 |
Correct |
6 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5120 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
5 ms |
5120 KB |
Output is correct |
5 |
Correct |
6 ms |
5120 KB |
Output is correct |
6 |
Correct |
6 ms |
5120 KB |
Output is correct |
7 |
Correct |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
6 ms |
5120 KB |
Output is correct |
10 |
Correct |
5 ms |
5120 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
12 |
Correct |
6 ms |
5120 KB |
Output is correct |
13 |
Correct |
6 ms |
5120 KB |
Output is correct |
14 |
Correct |
6 ms |
5148 KB |
Output is correct |
15 |
Correct |
5 ms |
5120 KB |
Output is correct |
16 |
Correct |
5 ms |
5120 KB |
Output is correct |
17 |
Correct |
6 ms |
5120 KB |
Output is correct |
18 |
Correct |
6 ms |
5120 KB |
Output is correct |
19 |
Correct |
6 ms |
5120 KB |
Output is correct |
20 |
Correct |
6 ms |
5120 KB |
Output is correct |
21 |
Correct |
6 ms |
5120 KB |
Output is correct |
22 |
Correct |
6 ms |
5120 KB |
Output is correct |
23 |
Correct |
9 ms |
5632 KB |
Output is correct |
24 |
Correct |
9 ms |
5632 KB |
Output is correct |
25 |
Correct |
9 ms |
5632 KB |
Output is correct |
26 |
Correct |
12 ms |
6140 KB |
Output is correct |
27 |
Correct |
12 ms |
6144 KB |
Output is correct |
28 |
Correct |
11 ms |
6144 KB |
Output is correct |
29 |
Correct |
11 ms |
6144 KB |
Output is correct |
30 |
Correct |
11 ms |
6144 KB |
Output is correct |
31 |
Correct |
18 ms |
6272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5120 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
3 |
Correct |
6 ms |
5120 KB |
Output is correct |
4 |
Correct |
5 ms |
5120 KB |
Output is correct |
5 |
Correct |
6 ms |
5120 KB |
Output is correct |
6 |
Correct |
6 ms |
5120 KB |
Output is correct |
7 |
Correct |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
6 ms |
5120 KB |
Output is correct |
10 |
Correct |
5 ms |
5120 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
12 |
Correct |
6 ms |
5120 KB |
Output is correct |
13 |
Correct |
6 ms |
5120 KB |
Output is correct |
14 |
Correct |
6 ms |
5148 KB |
Output is correct |
15 |
Correct |
5 ms |
5120 KB |
Output is correct |
16 |
Correct |
5 ms |
5120 KB |
Output is correct |
17 |
Correct |
6 ms |
5120 KB |
Output is correct |
18 |
Correct |
6 ms |
5120 KB |
Output is correct |
19 |
Correct |
6 ms |
5120 KB |
Output is correct |
20 |
Correct |
6 ms |
5120 KB |
Output is correct |
21 |
Correct |
6 ms |
5120 KB |
Output is correct |
22 |
Correct |
6 ms |
5120 KB |
Output is correct |
23 |
Correct |
9 ms |
5632 KB |
Output is correct |
24 |
Correct |
9 ms |
5632 KB |
Output is correct |
25 |
Correct |
9 ms |
5632 KB |
Output is correct |
26 |
Correct |
12 ms |
6140 KB |
Output is correct |
27 |
Correct |
12 ms |
6144 KB |
Output is correct |
28 |
Correct |
11 ms |
6144 KB |
Output is correct |
29 |
Correct |
11 ms |
6144 KB |
Output is correct |
30 |
Correct |
11 ms |
6144 KB |
Output is correct |
31 |
Correct |
18 ms |
6272 KB |
Output is correct |
32 |
Correct |
42 ms |
9664 KB |
Output is correct |
33 |
Correct |
44 ms |
9712 KB |
Output is correct |
34 |
Correct |
44 ms |
9712 KB |
Output is correct |
35 |
Correct |
238 ms |
23124 KB |
Output is correct |
36 |
Correct |
249 ms |
23056 KB |
Output is correct |
37 |
Correct |
240 ms |
23140 KB |
Output is correct |
38 |
Correct |
176 ms |
23684 KB |
Output is correct |
39 |
Correct |
158 ms |
23644 KB |
Output is correct |
40 |
Correct |
165 ms |
23520 KB |
Output is correct |
41 |
Correct |
252 ms |
25444 KB |
Output is correct |