Submission #639762

# Submission time Handle Problem Language Result Execution time Memory
639762 2022-09-11T14:49:33 Z gozonite Race (IOI11_race) C++14
100 / 100
813 ms 38444 KB
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <algorithm>
#include <climits>
#include <cstdlib>
#include <cstdio>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <bitset>
#include <deque>
#include <queue>
#include <tuple>
#include <cmath>
#include <cctype>
#include <stack>
#include <cassert>
#include <iomanip>
#include <complex>
using namespace std;
using ll = long long;

vector<pair<int, int>> adj[200000];
int subs[200000];
bool rm[200000];
int ans;
map<int, int> mh; // minimum number of highways to construct path of length idx
vector<pair<int, int>> cand;

int rsubs(int v, int p) {
    subs[v] = 1;
    for (auto u : adj[v])
        if (u.first != p && !rm[u.first]) subs[v] += rsubs(u.first, v);
    return subs[v];
}
int find_centroid(int v, int p, int sz) {
    for (auto u : adj[v])
        if (u.first != p && !rm[u.first] && subs[u.first] > sz/2) return find_centroid(u.first, v, sz);
    return v;
}
void dfs(int v, int p, int len, int d) {
    cand.push_back({len, d});
    for (auto u : adj[v])
        if (u.first != p && !rm[u.first]) dfs(u.first, v, len+u.second, d+1);
}

void build(int v, int K) {
    rsubs(v, -1);
    v = find_centroid(v, -1, subs[v]);

    // consider all paths passing through node v
    mh[0] = 0;
    for (auto u: adj[v]) {
        if (rm[u.first]) continue;
        dfs(u.first, v, u.second, 1);
        for (auto pp : cand) {
            int len = pp.first, d = pp.second;
            if (mh.find(K-len) != mh.end()) ans = min(ans, mh[K-len]+d);
        }
        for (auto pp : cand) {
            if (mh.find(pp.first) == mh.end()) mh[pp.first] = pp.second;
            else mh[pp.first] = min(mh[pp.first], pp.second);
        }
        cand.clear();
    }
    mh.clear();

    rm[v] = true;
    for (auto u : adj[v]) {
        if (rm[u.first]) continue;
        build(u.first, K);
    }
}

int best_path(int N, int K, int H[][2], int L[]) {
    for (int i = 0; i < N; i++) {
        adj[i].clear();
        rm[i] = false;
    }
    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]});
    }
    ans = 1e9;
    build(1, K);
    return ans==1e9 ? -1 : ans;
}

/*int main() {
    //int N = 4, K = 3;
    //int H[][2] = {{0, 1}, {1, 2}, {1, 3}};
    //int L[] = {1, 2, 4};
    //cout << best_path(N, K, H, L) << endl;

    //int N = 3, K = 3;
    //int H[][2] = {{0, 1}, {1, 2}};
    //int L[] = {1, 1};
    //cout << best_path(N, K, H, L) << endl;

    int N = 11, K = 12;
    int H[][2] = {{0, 1}, {0, 2}, {2, 3}, {3, 4}, {4, 5}, {0, 6}, {6, 7}, {6, 8}, {8, 9}, {8, 10}};
    int L[] = {3, 4, 5, 4, 6, 3, 2, 5, 6, 7};
    cout << best_path(N, K, H, L) << endl;
}*/
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 4 ms 5076 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 6 ms 5076 KB Output is correct
24 Correct 4 ms 5076 KB Output is correct
25 Correct 4 ms 5124 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 4 ms 5076 KB Output is correct
29 Correct 4 ms 5076 KB Output is correct
30 Correct 4 ms 5076 KB Output is correct
31 Correct 4 ms 5076 KB Output is correct
32 Correct 4 ms 5128 KB Output is correct
33 Correct 4 ms 5076 KB Output is correct
34 Correct 4 ms 5076 KB Output is correct
35 Correct 4 ms 5076 KB Output is correct
36 Correct 4 ms 5076 KB Output is correct
37 Correct 4 ms 5076 KB Output is correct
38 Correct 4 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 216 ms 10976 KB Output is correct
20 Correct 207 ms 10952 KB Output is correct
21 Correct 189 ms 10944 KB Output is correct
22 Correct 174 ms 11004 KB Output is correct
23 Correct 255 ms 11216 KB Output is correct
24 Correct 126 ms 10828 KB Output is correct
25 Correct 174 ms 16032 KB Output is correct
26 Correct 105 ms 16720 KB Output is correct
27 Correct 269 ms 15992 KB Output is correct
28 Correct 755 ms 36284 KB Output is correct
29 Correct 761 ms 35564 KB Output is correct
30 Correct 277 ms 19020 KB Output is correct
31 Correct 264 ms 18892 KB Output is correct
32 Correct 366 ms 19172 KB Output is correct
33 Correct 420 ms 18704 KB Output is correct
34 Correct 672 ms 28444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 3 ms 4948 KB Output is correct
4 Correct 3 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 2 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 2 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 2 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 2 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 4 ms 5076 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 6 ms 5076 KB Output is correct
24 Correct 4 ms 5076 KB Output is correct
25 Correct 4 ms 5124 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 3 ms 5076 KB Output is correct
28 Correct 4 ms 5076 KB Output is correct
29 Correct 4 ms 5076 KB Output is correct
30 Correct 4 ms 5076 KB Output is correct
31 Correct 4 ms 5076 KB Output is correct
32 Correct 4 ms 5128 KB Output is correct
33 Correct 4 ms 5076 KB Output is correct
34 Correct 4 ms 5076 KB Output is correct
35 Correct 4 ms 5076 KB Output is correct
36 Correct 4 ms 5076 KB Output is correct
37 Correct 4 ms 5076 KB Output is correct
38 Correct 4 ms 5076 KB Output is correct
39 Correct 216 ms 10976 KB Output is correct
40 Correct 207 ms 10952 KB Output is correct
41 Correct 189 ms 10944 KB Output is correct
42 Correct 174 ms 11004 KB Output is correct
43 Correct 255 ms 11216 KB Output is correct
44 Correct 126 ms 10828 KB Output is correct
45 Correct 174 ms 16032 KB Output is correct
46 Correct 105 ms 16720 KB Output is correct
47 Correct 269 ms 15992 KB Output is correct
48 Correct 755 ms 36284 KB Output is correct
49 Correct 761 ms 35564 KB Output is correct
50 Correct 277 ms 19020 KB Output is correct
51 Correct 264 ms 18892 KB Output is correct
52 Correct 366 ms 19172 KB Output is correct
53 Correct 420 ms 18704 KB Output is correct
54 Correct 672 ms 28444 KB Output is correct
55 Correct 21 ms 5920 KB Output is correct
56 Correct 15 ms 5664 KB Output is correct
57 Correct 138 ms 12492 KB Output is correct
58 Correct 46 ms 11652 KB Output is correct
59 Correct 266 ms 21220 KB Output is correct
60 Correct 754 ms 34664 KB Output is correct
61 Correct 314 ms 20296 KB Output is correct
62 Correct 259 ms 19016 KB Output is correct
63 Correct 310 ms 19012 KB Output is correct
64 Correct 813 ms 24360 KB Output is correct
65 Correct 700 ms 29660 KB Output is correct
66 Correct 744 ms 38444 KB Output is correct
67 Correct 123 ms 19560 KB Output is correct
68 Correct 427 ms 28192 KB Output is correct
69 Correct 453 ms 28544 KB Output is correct
70 Correct 392 ms 27300 KB Output is correct