Submission #914697

# Submission time Handle Problem Language Result Execution time Memory
914697 2024-01-22T14:34:00 Z asdasdqwer Race (IOI11_race) C++14
100 / 100
428 ms 97620 KB
// solution using small-to-large merging
#include <bits/stdc++.h>
using namespace std;
#include "race.h"

#define pii array<int,2>

int best_path(int N, int K, int H[][2], int L[]) {
    vector<vector<pii>> g(N);
    for (int i=0;i<N-1;i++) {
        g[H[i][0]].push_back({H[i][1], L[i]});
        g[H[i][1]].push_back({H[i][0], L[i]});
        if (L[i] == K) return 1;
    }

    vector<vector<pii>> child(N);
    function<void(int,int)> ch=[&](int node, int pv) {
        for (auto [x, w]:g[node]) {
            if (x == pv)continue;
            child[node].push_back({x, w});
            ch(x, node);
        }
    };

    ch(0, -1);

    // maps for merging: key is the length of the path and 
    // value is the minimum number of edges needed for this length
    vector<map<int,int>> sm(N);

    // candidate answer for each node
    vector<int> cand(N, 1e7);

    // value that has to be added to all weights of subtree
    vector<int> upd(N, 0);

    // value that has to be added to all path lenghts
    vector<int> len(N, 0);

    function<void(int)> dfs=[&](int node) {
        if (child[node].size() == 0) {
            return;
        }

        int mxSize=-1, mxChild=-1, mxW = 0;
        for (auto [x, w]:child[node]) {
            dfs(x);
            if ((int)sm[x].size() > mxSize) {
                mxSize = sm[x].size();
                mxChild = x;
                mxW = w;
            }
        }

        if (mxSize == 0) {
            for (auto [x, w]:child[node]) {
                if (sm[node].find(K - w) != sm[node].end()) {
                    cand[node] = min(cand[node], 2);
                    // cout<<"found a path of length 2 at node "<<node<<"\n";
                }

                sm[node][w] = 1;
            }
            return;
        }

        // check for edge to max-size subtree only
        if (sm[mxChild].find(K - upd[mxChild] - mxW) != sm[mxChild].end()) {
            cand[node] = min(cand[node], sm[mxChild][K - upd[mxChild] - mxW] + len[mxChild] + 1);
            // cout<<"found a path of length "<<sm[mxChild][K - upd[mxChild] - mxW] + len[mxChild] + 1<<" at node "<<node<<"\n";
        }

        upd[node] = upd[mxChild] + mxW;
        len[node] = len[mxChild] + 1;
        sm[node].swap(sm[mxChild]);
        sm[node][mxW - upd[node]] = 1 - len[node];

        for (auto [x, w] : child[node]) {
            if (x==mxChild) continue;
            
            // go though all children values and update if found
            for (auto [gg,hh] : sm[x]) {
                int weight = gg + upd[x] + w;
                int numEdges = hh + 1 + len[x];
                if (sm[node].find(K - weight - upd[node]) != sm[node].end()) {
                    int val = sm[node][K - weight - upd[node]] + len[node];
                    cand[node] = min(cand[node], val + numEdges);
                    // cout<<"found a path of length "<<val + numEdges<<" at node "<<node<<"\n";
                }
            }

            // update if path with down edge only exists
            if (sm[node].find(K - w - upd[node]) != sm[node].end()) {
                cand[node] = min(cand[node], sm[node][K - w - upd[node]] + len[node] + 1);
                // cout<<"found a path of length "<<sm[node][K - w - upd[node]] + len[node] + 1<<" at node "<<node<<"\n";
            }

            // insert all children values
            for (auto [gg,hh] : sm[x]) {
                int weight = gg + upd[x] + w - upd[node];
                int numEdges = hh + 1 + len[x] - len[node];
                if (sm[node].find(weight) != sm[node].end()) {
                    sm[node][weight] = min(sm[node][weight], numEdges);
                }

                else {
                    sm[node][weight] = numEdges;
                }
            }

            // insert down edge only
            sm[node][w - upd[node]] = 1 - len[node];

        }
    };

    dfs(0);
    int mn = 1e7;
    for (int i=0;i<N;i++) {
        mn = min(mn, cand[i]);
    }

    if (mn == (int)1e7) {
        return -1;
    }

    return mn;
}

Compilation message

race.cpp: In lambda function:
race.cpp:18:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   18 |         for (auto [x, w]:g[node]) {
      |                   ^
race.cpp: In lambda function:
race.cpp:46:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   46 |         for (auto [x, w]:child[node]) {
      |                   ^
race.cpp:56:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   56 |             for (auto [x, w]:child[node]) {
      |                       ^
race.cpp:78:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   78 |         for (auto [x, w] : child[node]) {
      |                   ^
race.cpp:82:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   82 |             for (auto [gg,hh] : sm[x]) {
      |                       ^
race.cpp:99:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   99 |             for (auto [gg,hh] : sm[x]) {
      |                       ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2428 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2392 KB Output is correct
12 Correct 1 ms 2392 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2428 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2392 KB Output is correct
12 Correct 1 ms 2392 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2492 KB Output is correct
21 Correct 2 ms 2652 KB Output is correct
22 Correct 2 ms 2652 KB Output is correct
23 Correct 1 ms 2648 KB Output is correct
24 Correct 1 ms 2652 KB Output is correct
25 Correct 2 ms 2652 KB Output is correct
26 Correct 2 ms 2652 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 2 ms 2652 KB Output is correct
30 Correct 2 ms 2652 KB Output is correct
31 Correct 2 ms 2652 KB Output is correct
32 Correct 2 ms 2652 KB Output is correct
33 Correct 2 ms 2648 KB Output is correct
34 Correct 2 ms 2652 KB Output is correct
35 Correct 1 ms 2760 KB Output is correct
36 Correct 1 ms 2652 KB Output is correct
37 Correct 1 ms 2652 KB Output is correct
38 Correct 1 ms 2752 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2428 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2392 KB Output is correct
12 Correct 1 ms 2392 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 89 ms 28404 KB Output is correct
20 Correct 92 ms 28484 KB Output is correct
21 Correct 97 ms 28352 KB Output is correct
22 Correct 85 ms 28256 KB Output is correct
23 Correct 21 ms 8268 KB Output is correct
24 Correct 93 ms 30288 KB Output is correct
25 Correct 79 ms 35668 KB Output is correct
26 Correct 57 ms 46256 KB Output is correct
27 Correct 132 ms 45396 KB Output is correct
28 Correct 254 ms 97620 KB Output is correct
29 Correct 258 ms 94848 KB Output is correct
30 Correct 132 ms 45388 KB Output is correct
31 Correct 129 ms 45516 KB Output is correct
32 Correct 179 ms 45772 KB Output is correct
33 Correct 205 ms 51024 KB Output is correct
34 Correct 328 ms 75704 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2392 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2428 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 1 ms 2392 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2392 KB Output is correct
12 Correct 1 ms 2392 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2492 KB Output is correct
21 Correct 2 ms 2652 KB Output is correct
22 Correct 2 ms 2652 KB Output is correct
23 Correct 1 ms 2648 KB Output is correct
24 Correct 1 ms 2652 KB Output is correct
25 Correct 2 ms 2652 KB Output is correct
26 Correct 2 ms 2652 KB Output is correct
27 Correct 1 ms 2652 KB Output is correct
28 Correct 1 ms 2652 KB Output is correct
29 Correct 2 ms 2652 KB Output is correct
30 Correct 2 ms 2652 KB Output is correct
31 Correct 2 ms 2652 KB Output is correct
32 Correct 2 ms 2652 KB Output is correct
33 Correct 2 ms 2648 KB Output is correct
34 Correct 2 ms 2652 KB Output is correct
35 Correct 1 ms 2760 KB Output is correct
36 Correct 1 ms 2652 KB Output is correct
37 Correct 1 ms 2652 KB Output is correct
38 Correct 1 ms 2752 KB Output is correct
39 Correct 89 ms 28404 KB Output is correct
40 Correct 92 ms 28484 KB Output is correct
41 Correct 97 ms 28352 KB Output is correct
42 Correct 85 ms 28256 KB Output is correct
43 Correct 21 ms 8268 KB Output is correct
44 Correct 93 ms 30288 KB Output is correct
45 Correct 79 ms 35668 KB Output is correct
46 Correct 57 ms 46256 KB Output is correct
47 Correct 132 ms 45396 KB Output is correct
48 Correct 254 ms 97620 KB Output is correct
49 Correct 258 ms 94848 KB Output is correct
50 Correct 132 ms 45388 KB Output is correct
51 Correct 129 ms 45516 KB Output is correct
52 Correct 179 ms 45772 KB Output is correct
53 Correct 205 ms 51024 KB Output is correct
54 Correct 328 ms 75704 KB Output is correct
55 Correct 12 ms 5468 KB Output is correct
56 Correct 6 ms 4704 KB Output is correct
57 Correct 53 ms 26436 KB Output is correct
58 Correct 36 ms 20812 KB Output is correct
59 Correct 74 ms 51024 KB Output is correct
60 Correct 254 ms 95896 KB Output is correct
61 Correct 155 ms 48192 KB Output is correct
62 Correct 135 ms 45372 KB Output is correct
63 Correct 177 ms 45772 KB Output is correct
64 Correct 428 ms 84048 KB Output is correct
65 Correct 383 ms 83540 KB Output is correct
66 Correct 273 ms 88652 KB Output is correct
67 Correct 111 ms 38728 KB Output is correct
68 Correct 265 ms 57552 KB Output is correct
69 Correct 249 ms 57940 KB Output is correct
70 Correct 234 ms 55216 KB Output is correct