Submission #442503

# Submission time Handle Problem Language Result Execution time Memory
442503 2021-07-08T04:59:49 Z izhang05 Race (IOI11_race) C++17
100 / 100
927 ms 47096 KB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;
//#define DEBUG

const int maxn = 2e5 + 5, inf = 1e9;
vector<pair<int, int>> adj[maxn];
long long sol = inf, k, sub[maxn];
bool visited[maxn];
unordered_map<long long, long long> occ;

int find_centroid(int c, int p, long long s) {
    for (auto &i : adj[c]) {
        if (i.first != p && !visited[i.first] && sub[i.first] > s / 2) {
            return find_centroid(i.first, c, s);
        }
    }
    return c;
}

int find_size(int c, int p) {
    sub[c] = 1;
    for (auto &i : adj[c]) {
        if (i.first != p && !visited[i.first]) {
            sub[c] += find_size(i.first, c);
        }
    }
    return sub[c];
}

void dfs(int c, int p, bool add, long long dist, long long depth) {
    if (dist > k) {
        return;
    }
    if (add) {
        if (occ.find(k - dist) != occ.end()) {
#ifdef DEBUG
            cout << c << " " << p << " " << dist << " " << k - dist << " " << depth << " " << occ[k - dist] << "\n";
#endif
            sol = min(sol, occ[k - dist] + depth);
        }
    } else {
        if (occ.find(dist) == occ.end()) {
            occ[dist] = depth;
        } else {
            occ[dist] = min(occ[dist], depth);
        }
    }
    for (auto &i : adj[c]) {
        if (i.first != p && !visited[i.first]) {
            dfs(i.first, c, add, dist + i.second, depth + 1);
        }
    }
}


void solve(int v) {
    find_size(v, -1);
    int c = find_centroid(v, -1, sub[v]);
#ifdef DEBUG
    cout << "c: " << c << endl;
#endif
    visited[c] = true;
    for (auto &i : adj[c]) {
        if (!visited[i.first]) {
            dfs(i.first, c, true, i.second, 1);
            dfs(i.first, c, false, i.second, 1);
        }
    }
    if (occ.find(k) != occ.end()) {
        sol = min(sol, occ[k]);
    }
    unordered_map<long long, long long> empty;
    occ.swap(empty);
    for (auto &i : adj[c]) {
        if (!visited[i.first]) {
            solve(i.first);
        }
    }
}

int best_path(int N, int K, int H[][2], int L[]) {
    k = K;
    for (int i = 0; i < N - 1; ++i) {
        adj[H[i][0]].emplace_back(H[i][1], L[i]);
        adj[H[i][1]].emplace_back(H[i][0], L[i]);
    }
    solve(0);
    return sol == inf ? -1 : sol;
}

#ifdef LOCAL
int h[maxn][2], l[maxn];
int main() {
    freopen("grader.in.5", "r", stdin);
    freopen("out.txt", "w", stdout);
    int n, K;
    cin >> n >> K;
    for (int i = 0; i < n - 1; ++i) {
        cin >> h[i][0] >> h[i][1] >> l[i];
    }
    cout << best_path(n, K, h, l) << "\n";
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4984 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4992 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4984 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4992 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 3 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 4 ms 5068 KB Output is correct
22 Correct 4 ms 5068 KB Output is correct
23 Correct 4 ms 5012 KB Output is correct
24 Correct 4 ms 5068 KB Output is correct
25 Correct 4 ms 5068 KB Output is correct
26 Correct 4 ms 5068 KB Output is correct
27 Correct 4 ms 5068 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 4 ms 5068 KB Output is correct
30 Correct 4 ms 5068 KB Output is correct
31 Correct 4 ms 5068 KB Output is correct
32 Correct 4 ms 5068 KB Output is correct
33 Correct 4 ms 5068 KB Output is correct
34 Correct 4 ms 5068 KB Output is correct
35 Correct 4 ms 5068 KB Output is correct
36 Correct 4 ms 5068 KB Output is correct
37 Correct 4 ms 5068 KB Output is correct
38 Correct 4 ms 5068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4984 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4992 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 182 ms 10736 KB Output is correct
20 Correct 191 ms 10748 KB Output is correct
21 Correct 191 ms 10820 KB Output is correct
22 Correct 174 ms 10948 KB Output is correct
23 Correct 104 ms 10900 KB Output is correct
24 Correct 78 ms 12208 KB Output is correct
25 Correct 178 ms 14920 KB Output is correct
26 Correct 143 ms 17808 KB Output is correct
27 Correct 232 ms 19652 KB Output is correct
28 Correct 354 ms 30808 KB Output is correct
29 Correct 330 ms 30092 KB Output is correct
30 Correct 234 ms 19616 KB Output is correct
31 Correct 230 ms 19512 KB Output is correct
32 Correct 263 ms 19616 KB Output is correct
33 Correct 333 ms 18372 KB Output is correct
34 Correct 196 ms 19232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 3 ms 4984 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4992 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 3 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 4 ms 5068 KB Output is correct
22 Correct 4 ms 5068 KB Output is correct
23 Correct 4 ms 5012 KB Output is correct
24 Correct 4 ms 5068 KB Output is correct
25 Correct 4 ms 5068 KB Output is correct
26 Correct 4 ms 5068 KB Output is correct
27 Correct 4 ms 5068 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 4 ms 5068 KB Output is correct
30 Correct 4 ms 5068 KB Output is correct
31 Correct 4 ms 5068 KB Output is correct
32 Correct 4 ms 5068 KB Output is correct
33 Correct 4 ms 5068 KB Output is correct
34 Correct 4 ms 5068 KB Output is correct
35 Correct 4 ms 5068 KB Output is correct
36 Correct 4 ms 5068 KB Output is correct
37 Correct 4 ms 5068 KB Output is correct
38 Correct 4 ms 5068 KB Output is correct
39 Correct 182 ms 10736 KB Output is correct
40 Correct 191 ms 10748 KB Output is correct
41 Correct 191 ms 10820 KB Output is correct
42 Correct 174 ms 10948 KB Output is correct
43 Correct 104 ms 10900 KB Output is correct
44 Correct 78 ms 12208 KB Output is correct
45 Correct 178 ms 14920 KB Output is correct
46 Correct 143 ms 17808 KB Output is correct
47 Correct 232 ms 19652 KB Output is correct
48 Correct 354 ms 30808 KB Output is correct
49 Correct 330 ms 30092 KB Output is correct
50 Correct 234 ms 19616 KB Output is correct
51 Correct 230 ms 19512 KB Output is correct
52 Correct 263 ms 19616 KB Output is correct
53 Correct 333 ms 18372 KB Output is correct
54 Correct 196 ms 19232 KB Output is correct
55 Correct 19 ms 5964 KB Output is correct
56 Correct 16 ms 5708 KB Output is correct
57 Correct 142 ms 12276 KB Output is correct
58 Correct 47 ms 11944 KB Output is correct
59 Correct 253 ms 23892 KB Output is correct
60 Correct 927 ms 47096 KB Output is correct
61 Correct 287 ms 20012 KB Output is correct
62 Correct 311 ms 19832 KB Output is correct
63 Correct 381 ms 19860 KB Output is correct
64 Correct 827 ms 29912 KB Output is correct
65 Correct 283 ms 20540 KB Output is correct
66 Correct 649 ms 29348 KB Output is correct
67 Correct 141 ms 20348 KB Output is correct
68 Correct 389 ms 26328 KB Output is correct
69 Correct 403 ms 26628 KB Output is correct
70 Correct 361 ms 25552 KB Output is correct