Submission #122364

# Submission time Handle Problem Language Result Execution time Memory
122364 2019-06-28T05:50:43 Z 송준혁(#2992) Beads and wires (APIO14_beads) C++14
100 / 100
252 ms 26764 KB
#include <bits/stdc++.h>
#define INF (1ll<<60)
using namespace std;
typedef long long LL;
typedef pair<int, LL> pii;

int N;
LL D1[202020], D2[202020];
LL D3[202020], D4[202020];
vector<pii> adj[202020];

void dfs(int u, int p, LL w){
    LL S = 0, Max = -INF, id0, Max1=-INF, id1, Max2=-INF, Max3 = -INF;
    for (pii v : adj[u]){
        if (v.first == p) continue;
        dfs(v.first, u, v.second);
        S += D2[v.first];
        Max = max(Max, D4[v.first]-D2[v.first]);
    }
    D1[u] = D2[u] = D3[u] = D4[u] = S;
    D1[u] = max(D1[u], S + Max);

    Max = -INF;
    for (pii v : adj[u]){
        if (v.first == p) continue;
        Max = max(Max, D3[v.first]-D2[v.first]+v.second+w);
    }
    D2[u] = max(D2[u], S + Max);

    Max = -INF;
    for (pii v : adj[u]){
        if (v.first == p) continue;
        Max = max(Max, D1[v.first]-D2[v.first]+v.second+w);
    }
    D4[u] = S + Max;

    Max = -INF, id0 = -1, id1 = -1;
    for (pii v : adj[u]){
        if (v.first == p) continue;
        if (D1[v.first]-D2[v.first]+v.second > Max) Max3 = Max, Max = D1[v.first]-D2[v.first]+v.second, id0 = v.first;
        else if (D1[v.first]-D2[v.first]+v.second > Max3) Max3 = D1[v.first]-D2[v.first]+v.second;
        if (D3[v.first]-D2[v.first]+v.second > Max1) Max2 = Max1, Max1 = D3[v.first]-D2[v.first]+v.second, id1 = v.first;
        else if (D3[v.first]-D2[v.first]+v.second > Max2) Max2 = D3[v.first]-D2[v.first]+v.second;
    }
    if (id0 != id1) D1[u] = max(D1[u], S + Max + Max1);
    else {
        D1[u] = max(D1[u], S + Max + Max2);
        D1[u] = max(D1[u], S + Max1 + Max3);
    }

    D1[u] = max(D1[u], S + Max1 + Max2);
    D4[u] = max(D4[u], D1[u]);
    D4[u] = max(D4[u], D2[u]);
    D4[u] = max(D4[u], D3[u]);
}

int main(){
    int u, v, w;
    scanf("%d", &N);
    for (int i=1; i<N; i++){
        scanf("%d %d %d", &u, &v, &w);
        adj[u].push_back(pii(v, w));
        adj[v].push_back(pii(u, w));
    }
    dfs(1, 0, -INF);
    printf("%lld\n", D4[1]);
    return 0;
}

Compilation message

beads.cpp: In function 'int main()':
beads.cpp:59:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &N);
     ~~~~~^~~~~~~~~~
beads.cpp:61:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d", &u, &v, &w);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 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 5 ms 5120 KB Output is correct
6 Correct 5 ms 5120 KB Output is correct
7 Correct 6 ms 5120 KB Output is correct
8 Correct 5 ms 5120 KB Output is correct
9 Correct 5 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 5 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 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 5 ms 5120 KB Output is correct
6 Correct 5 ms 5120 KB Output is correct
7 Correct 6 ms 5120 KB Output is correct
8 Correct 5 ms 5120 KB Output is correct
9 Correct 5 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 5 ms 5120 KB Output is correct
13 Correct 5 ms 5120 KB Output is correct
14 Correct 6 ms 5120 KB Output is correct
15 Correct 6 ms 5120 KB Output is correct
16 Correct 6 ms 5248 KB Output is correct
17 Correct 6 ms 5120 KB Output is correct
18 Correct 6 ms 5120 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 6 ms 5120 KB Output is correct
21 Correct 5 ms 5120 KB Output is correct
22 Correct 6 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 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 5 ms 5120 KB Output is correct
6 Correct 5 ms 5120 KB Output is correct
7 Correct 6 ms 5120 KB Output is correct
8 Correct 5 ms 5120 KB Output is correct
9 Correct 5 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 5 ms 5120 KB Output is correct
13 Correct 5 ms 5120 KB Output is correct
14 Correct 6 ms 5120 KB Output is correct
15 Correct 6 ms 5120 KB Output is correct
16 Correct 6 ms 5248 KB Output is correct
17 Correct 6 ms 5120 KB Output is correct
18 Correct 6 ms 5120 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 6 ms 5120 KB Output is correct
21 Correct 5 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 8 ms 5504 KB Output is correct
25 Correct 9 ms 5504 KB Output is correct
26 Correct 12 ms 6016 KB Output is correct
27 Correct 13 ms 6016 KB Output is correct
28 Correct 12 ms 6012 KB Output is correct
29 Correct 12 ms 6016 KB Output is correct
30 Correct 13 ms 6016 KB Output is correct
31 Correct 12 ms 6400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 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 5 ms 5120 KB Output is correct
6 Correct 5 ms 5120 KB Output is correct
7 Correct 6 ms 5120 KB Output is correct
8 Correct 5 ms 5120 KB Output is correct
9 Correct 5 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 5 ms 5120 KB Output is correct
13 Correct 5 ms 5120 KB Output is correct
14 Correct 6 ms 5120 KB Output is correct
15 Correct 6 ms 5120 KB Output is correct
16 Correct 6 ms 5248 KB Output is correct
17 Correct 6 ms 5120 KB Output is correct
18 Correct 6 ms 5120 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 6 ms 5120 KB Output is correct
21 Correct 5 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 8 ms 5504 KB Output is correct
25 Correct 9 ms 5504 KB Output is correct
26 Correct 12 ms 6016 KB Output is correct
27 Correct 13 ms 6016 KB Output is correct
28 Correct 12 ms 6012 KB Output is correct
29 Correct 12 ms 6016 KB Output is correct
30 Correct 13 ms 6016 KB Output is correct
31 Correct 12 ms 6400 KB Output is correct
32 Correct 47 ms 9648 KB Output is correct
33 Correct 45 ms 9656 KB Output is correct
34 Correct 41 ms 9580 KB Output is correct
35 Correct 246 ms 22336 KB Output is correct
36 Correct 242 ms 22264 KB Output is correct
37 Correct 252 ms 22264 KB Output is correct
38 Correct 181 ms 21456 KB Output is correct
39 Correct 175 ms 21432 KB Output is correct
40 Correct 179 ms 21332 KB Output is correct
41 Correct 249 ms 26764 KB Output is correct