Submission #1037477

# Submission time Handle Problem Language Result Execution time Memory
1037477 2024-07-28T23:50:46 Z ArthuroWich Race (IOI11_race) C++17
100 / 100
902 ms 41296 KB
#include "race.h"
#include<bits/stdc++.h>
using namespace std;
int n, k, ans = INT_MAX, proc[200005], si[200005];
vector<pair<int, int>> adj[200005];
map<int, int> occ[2];
void dfs(int i, int p) {
    si[i] = 1;
    for (auto [j, _] : adj[i]) {
        if (proc[j] || j == p) {
            continue;
        }
        dfs(j, i);
        si[i] += si[j];
    }
}
int centroid(int i, int p, int s) {
    for (auto [j, w] : adj[i]) {
        if (proc[j] || j == p) {
            continue;
        }
        if (si[j] > s/2) {
            return centroid(j, i, s);
        }
    }
    return i;
}
void calc(int i, int p, int d, int path) {
    if (path > k) {
        return;
    }
    if (occ[1].find(path) == occ[1].end()) {
        occ[1][path] = d;
    } else {
        occ[1][path] = min(occ[1][path], d);
    }
    if (k == path) {
        ans = min(ans, d);
    } else if (occ[0].find(k-path) != occ[0].end()) {
        ans = min(ans, d+occ[0][k-path]);
    }
    for (auto [j, w] : adj[i]) {
        if (proc[j] || j == p) {
            continue;
        }
        calc(j, i, d+1, path+w);
    }
}
void decompose(int i) {
    dfs(i, -1);
    if (si[i] == 1) {
        return;
    }
    i = centroid(i, -1, si[i]);
    proc[i] = 1;
    for (auto [j, w] : adj[i]) {
        if (proc[j]) {
            continue;
        }
        calc(j, i, 1, w);
        if (occ[0].size() < occ[1].size()) {
            occ[0].swap(occ[1]);
        }
        for (auto c : occ[1]) {
            if (occ[0].find(c.first) != occ[0].end()) {
                occ[0][c.first] = min(occ[0][c.first], c.second);
            } else {
                occ[0][c.first] = c.second;
            }
        }
        occ[1].clear();
    }
    occ[0].clear();
    for (auto [j, w] : adj[i]) {
        if (proc[j]) {
            continue;
        }
        decompose(j);
    }
}
int best_path(int N, int K, int H[][2], int L[]) {
    n = N;
    k = K;
    for (int i = 0; i < n-1; i++) {
        adj[H[i][0]].push_back({H[i][1], L[i]});
        adj[H[i][1]].push_back({H[i][0], L[i]});
    }
    decompose(1);
    if (ans == INT_MAX) {
        return -1;
    } else {
        return ans;
    }
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8796 KB Output is correct
2 Correct 1 ms 8796 KB Output is correct
3 Correct 1 ms 8796 KB Output is correct
4 Correct 1 ms 8796 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 2 ms 8792 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 2 ms 8796 KB Output is correct
9 Correct 1 ms 8796 KB Output is correct
10 Correct 1 ms 8796 KB Output is correct
11 Correct 1 ms 8796 KB Output is correct
12 Correct 1 ms 8796 KB Output is correct
13 Correct 1 ms 8796 KB Output is correct
14 Correct 1 ms 8796 KB Output is correct
15 Correct 1 ms 8796 KB Output is correct
16 Correct 2 ms 8796 KB Output is correct
17 Correct 2 ms 8796 KB Output is correct
18 Correct 1 ms 8796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8796 KB Output is correct
2 Correct 1 ms 8796 KB Output is correct
3 Correct 1 ms 8796 KB Output is correct
4 Correct 1 ms 8796 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 2 ms 8792 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 2 ms 8796 KB Output is correct
9 Correct 1 ms 8796 KB Output is correct
10 Correct 1 ms 8796 KB Output is correct
11 Correct 1 ms 8796 KB Output is correct
12 Correct 1 ms 8796 KB Output is correct
13 Correct 1 ms 8796 KB Output is correct
14 Correct 1 ms 8796 KB Output is correct
15 Correct 1 ms 8796 KB Output is correct
16 Correct 2 ms 8796 KB Output is correct
17 Correct 2 ms 8796 KB Output is correct
18 Correct 1 ms 8796 KB Output is correct
19 Correct 2 ms 8796 KB Output is correct
20 Correct 1 ms 8796 KB Output is correct
21 Correct 3 ms 8792 KB Output is correct
22 Correct 3 ms 8796 KB Output is correct
23 Correct 1 ms 8796 KB Output is correct
24 Correct 3 ms 8796 KB Output is correct
25 Correct 2 ms 8796 KB Output is correct
26 Correct 2 ms 8796 KB Output is correct
27 Correct 2 ms 8796 KB Output is correct
28 Correct 2 ms 8796 KB Output is correct
29 Correct 2 ms 8796 KB Output is correct
30 Correct 2 ms 8796 KB Output is correct
31 Correct 2 ms 8796 KB Output is correct
32 Correct 2 ms 8796 KB Output is correct
33 Correct 4 ms 8796 KB Output is correct
34 Correct 2 ms 8796 KB Output is correct
35 Correct 2 ms 8796 KB Output is correct
36 Correct 2 ms 8796 KB Output is correct
37 Correct 2 ms 8796 KB Output is correct
38 Correct 4 ms 8796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8796 KB Output is correct
2 Correct 1 ms 8796 KB Output is correct
3 Correct 1 ms 8796 KB Output is correct
4 Correct 1 ms 8796 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 2 ms 8792 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 2 ms 8796 KB Output is correct
9 Correct 1 ms 8796 KB Output is correct
10 Correct 1 ms 8796 KB Output is correct
11 Correct 1 ms 8796 KB Output is correct
12 Correct 1 ms 8796 KB Output is correct
13 Correct 1 ms 8796 KB Output is correct
14 Correct 1 ms 8796 KB Output is correct
15 Correct 1 ms 8796 KB Output is correct
16 Correct 2 ms 8796 KB Output is correct
17 Correct 2 ms 8796 KB Output is correct
18 Correct 1 ms 8796 KB Output is correct
19 Correct 149 ms 15284 KB Output is correct
20 Correct 127 ms 15288 KB Output is correct
21 Correct 127 ms 15448 KB Output is correct
22 Correct 134 ms 15520 KB Output is correct
23 Correct 47 ms 15444 KB Output is correct
24 Correct 49 ms 15360 KB Output is correct
25 Correct 91 ms 21960 KB Output is correct
26 Correct 60 ms 22660 KB Output is correct
27 Correct 111 ms 20052 KB Output is correct
28 Correct 165 ms 33272 KB Output is correct
29 Correct 140 ms 28132 KB Output is correct
30 Correct 113 ms 19960 KB Output is correct
31 Correct 132 ms 20048 KB Output is correct
32 Correct 112 ms 20108 KB Output is correct
33 Correct 174 ms 18936 KB Output is correct
34 Correct 126 ms 18768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 8796 KB Output is correct
2 Correct 1 ms 8796 KB Output is correct
3 Correct 1 ms 8796 KB Output is correct
4 Correct 1 ms 8796 KB Output is correct
5 Correct 2 ms 8796 KB Output is correct
6 Correct 2 ms 8792 KB Output is correct
7 Correct 2 ms 8796 KB Output is correct
8 Correct 2 ms 8796 KB Output is correct
9 Correct 1 ms 8796 KB Output is correct
10 Correct 1 ms 8796 KB Output is correct
11 Correct 1 ms 8796 KB Output is correct
12 Correct 1 ms 8796 KB Output is correct
13 Correct 1 ms 8796 KB Output is correct
14 Correct 1 ms 8796 KB Output is correct
15 Correct 1 ms 8796 KB Output is correct
16 Correct 2 ms 8796 KB Output is correct
17 Correct 2 ms 8796 KB Output is correct
18 Correct 1 ms 8796 KB Output is correct
19 Correct 2 ms 8796 KB Output is correct
20 Correct 1 ms 8796 KB Output is correct
21 Correct 3 ms 8792 KB Output is correct
22 Correct 3 ms 8796 KB Output is correct
23 Correct 1 ms 8796 KB Output is correct
24 Correct 3 ms 8796 KB Output is correct
25 Correct 2 ms 8796 KB Output is correct
26 Correct 2 ms 8796 KB Output is correct
27 Correct 2 ms 8796 KB Output is correct
28 Correct 2 ms 8796 KB Output is correct
29 Correct 2 ms 8796 KB Output is correct
30 Correct 2 ms 8796 KB Output is correct
31 Correct 2 ms 8796 KB Output is correct
32 Correct 2 ms 8796 KB Output is correct
33 Correct 4 ms 8796 KB Output is correct
34 Correct 2 ms 8796 KB Output is correct
35 Correct 2 ms 8796 KB Output is correct
36 Correct 2 ms 8796 KB Output is correct
37 Correct 2 ms 8796 KB Output is correct
38 Correct 4 ms 8796 KB Output is correct
39 Correct 149 ms 15284 KB Output is correct
40 Correct 127 ms 15288 KB Output is correct
41 Correct 127 ms 15448 KB Output is correct
42 Correct 134 ms 15520 KB Output is correct
43 Correct 47 ms 15444 KB Output is correct
44 Correct 49 ms 15360 KB Output is correct
45 Correct 91 ms 21960 KB Output is correct
46 Correct 60 ms 22660 KB Output is correct
47 Correct 111 ms 20052 KB Output is correct
48 Correct 165 ms 33272 KB Output is correct
49 Correct 140 ms 28132 KB Output is correct
50 Correct 113 ms 19960 KB Output is correct
51 Correct 132 ms 20048 KB Output is correct
52 Correct 112 ms 20108 KB Output is correct
53 Correct 174 ms 18936 KB Output is correct
54 Correct 126 ms 18768 KB Output is correct
55 Correct 23 ms 9448 KB Output is correct
56 Correct 10 ms 9052 KB Output is correct
57 Correct 88 ms 15408 KB Output is correct
58 Correct 30 ms 15308 KB Output is correct
59 Correct 266 ms 28500 KB Output is correct
60 Correct 767 ms 41296 KB Output is correct
61 Correct 244 ms 20224 KB Output is correct
62 Correct 280 ms 20052 KB Output is correct
63 Correct 276 ms 20308 KB Output is correct
64 Correct 902 ms 26252 KB Output is correct
65 Correct 179 ms 19792 KB Output is correct
66 Correct 527 ms 32080 KB Output is correct
67 Correct 93 ms 20416 KB Output is correct
68 Correct 334 ms 23888 KB Output is correct
69 Correct 299 ms 23888 KB Output is correct
70 Correct 326 ms 23340 KB Output is correct