Submission #499798

# Submission time Handle Problem Language Result Execution time Memory
499798 2021-12-29T13:08:29 Z zxcvbnm Race (IOI11_race) C++14
100 / 100
993 ms 35300 KB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;

constexpr int nax = 2e5 + 5;
vector<pair<int, int>> adj[nax];
int k;

struct Centroid {
    vector<int> sz;
    vector<bool> vis;
    int ans = INT_MAX;

    void init(int n) {
        sz.resize(n);
        vis.assign(n, false);
    }

    int find_size(int v, int p=-1) {
        if (vis[v]) {
            return 0;
        }

        sz[v] = 1;
        for(auto u : adj[v]) {
            if (u.first == p) continue;
            sz[v] += find_size(u.first, v);
        }
        return sz[v];
    }

    int find_centroid(int v, int p, int n) {
        for(auto u : adj[v]) {
            if (u.first == p || vis[u.first]) continue;
            if (sz[u.first] > n/2) {
                return find_centroid(u.first, v, n);
            }
        }
        return v;
    }

    map<int, int> mp;

    void dfs(int v, int p, int depth, int cost, bool filling) {
        if (cost > k) return;
        if (cost == k) {
            ans = min(ans, depth);
            return;
        }

        if (filling) {
            if (mp.find(cost) == mp.end() || mp[cost] > depth) {
                mp[cost] = depth;
            }
        }
        else {
            if (mp.find(k-cost) != mp.end()) {
                ans = min(ans, depth + mp[k-cost]);
//                cerr << "in: " << depth + mp[k-cost] << "\n";
            }
        }

        for(auto u : adj[v]) {
            if (u.first == p || vis[u.first]) continue;
            dfs(u.first, v, depth+1, cost+u.second, filling);
        }
    }

    void sol(int v) {
        mp.clear();

        for(auto u : adj[v]) {
            if (vis[u.first]) continue;
//            cout << v << "->" << u.first << " cost " << u.second << "\n";
            dfs(u.first, v, 1, u.second, false);
            dfs(u.first, v, 1, u.second, true);
        }
    }

    void init_centroid(int v=0, int p=-1) {
        find_size(v);

        int c = find_centroid(v, p, sz[v]);
        vis[c] = true;
        sol(c);
//        cout << c << " " << ans << "\n";

        for(auto u : adj[c]) {
            if (!vis[u.first]) {
                init_centroid(u.first, c);
            }
        }
    }
};

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]].push_back({H[i][1], L[i]});
        adj[H[i][1]].push_back({H[i][0], L[i]});
    }

    Centroid ct;
    ct.init(N+6);
    ct.init_centroid();
    return ct.ans == INT_MAX ? -1 : ct.ans;
}
# Verdict Execution time Memory Grader output
1 Correct 3 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 2 ms 4996 KB Output is correct
5 Correct 2 ms 4940 KB Output is correct
6 Correct 2 ms 4940 KB Output is correct
7 Correct 3 ms 5000 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 4 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 5068 KB Output is correct
13 Correct 3 ms 5000 KB Output is correct
14 Correct 4 ms 4940 KB Output is correct
15 Correct 2 ms 5000 KB Output is correct
16 Correct 3 ms 5004 KB Output is correct
17 Correct 2 ms 4940 KB Output is correct
18 Correct 2 ms 4940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 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 2 ms 4996 KB Output is correct
5 Correct 2 ms 4940 KB Output is correct
6 Correct 2 ms 4940 KB Output is correct
7 Correct 3 ms 5000 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 4 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 5068 KB Output is correct
13 Correct 3 ms 5000 KB Output is correct
14 Correct 4 ms 4940 KB Output is correct
15 Correct 2 ms 5000 KB Output is correct
16 Correct 3 ms 5004 KB Output is correct
17 Correct 2 ms 4940 KB Output is correct
18 Correct 2 ms 4940 KB Output is correct
19 Correct 2 ms 4940 KB Output is correct
20 Correct 3 ms 5060 KB Output is correct
21 Correct 3 ms 5068 KB Output is correct
22 Correct 4 ms 5068 KB Output is correct
23 Correct 3 ms 5008 KB Output is correct
24 Correct 3 ms 5068 KB Output is correct
25 Correct 5 ms 5068 KB Output is correct
26 Correct 4 ms 5008 KB Output is correct
27 Correct 3 ms 4992 KB Output is correct
28 Correct 6 ms 5012 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 5008 KB Output is correct
32 Correct 4 ms 5068 KB Output is correct
33 Correct 4 ms 5068 KB Output is correct
34 Correct 3 ms 5068 KB Output is correct
35 Correct 3 ms 5000 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 3 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 2 ms 4996 KB Output is correct
5 Correct 2 ms 4940 KB Output is correct
6 Correct 2 ms 4940 KB Output is correct
7 Correct 3 ms 5000 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 4 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 5068 KB Output is correct
13 Correct 3 ms 5000 KB Output is correct
14 Correct 4 ms 4940 KB Output is correct
15 Correct 2 ms 5000 KB Output is correct
16 Correct 3 ms 5004 KB Output is correct
17 Correct 2 ms 4940 KB Output is correct
18 Correct 2 ms 4940 KB Output is correct
19 Correct 178 ms 10328 KB Output is correct
20 Correct 172 ms 11532 KB Output is correct
21 Correct 205 ms 11604 KB Output is correct
22 Correct 182 ms 11704 KB Output is correct
23 Correct 78 ms 11824 KB Output is correct
24 Correct 93 ms 11708 KB Output is correct
25 Correct 118 ms 13048 KB Output is correct
26 Correct 94 ms 15676 KB Output is correct
27 Correct 200 ms 18500 KB Output is correct
28 Correct 233 ms 25352 KB Output is correct
29 Correct 229 ms 24832 KB Output is correct
30 Correct 212 ms 18528 KB Output is correct
31 Correct 197 ms 18684 KB Output is correct
32 Correct 213 ms 18556 KB Output is correct
33 Correct 291 ms 17316 KB Output is correct
34 Correct 213 ms 18268 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 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 2 ms 4996 KB Output is correct
5 Correct 2 ms 4940 KB Output is correct
6 Correct 2 ms 4940 KB Output is correct
7 Correct 3 ms 5000 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 4 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 5068 KB Output is correct
13 Correct 3 ms 5000 KB Output is correct
14 Correct 4 ms 4940 KB Output is correct
15 Correct 2 ms 5000 KB Output is correct
16 Correct 3 ms 5004 KB Output is correct
17 Correct 2 ms 4940 KB Output is correct
18 Correct 2 ms 4940 KB Output is correct
19 Correct 2 ms 4940 KB Output is correct
20 Correct 3 ms 5060 KB Output is correct
21 Correct 3 ms 5068 KB Output is correct
22 Correct 4 ms 5068 KB Output is correct
23 Correct 3 ms 5008 KB Output is correct
24 Correct 3 ms 5068 KB Output is correct
25 Correct 5 ms 5068 KB Output is correct
26 Correct 4 ms 5008 KB Output is correct
27 Correct 3 ms 4992 KB Output is correct
28 Correct 6 ms 5012 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 5008 KB Output is correct
32 Correct 4 ms 5068 KB Output is correct
33 Correct 4 ms 5068 KB Output is correct
34 Correct 3 ms 5068 KB Output is correct
35 Correct 3 ms 5000 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 178 ms 10328 KB Output is correct
40 Correct 172 ms 11532 KB Output is correct
41 Correct 205 ms 11604 KB Output is correct
42 Correct 182 ms 11704 KB Output is correct
43 Correct 78 ms 11824 KB Output is correct
44 Correct 93 ms 11708 KB Output is correct
45 Correct 118 ms 13048 KB Output is correct
46 Correct 94 ms 15676 KB Output is correct
47 Correct 200 ms 18500 KB Output is correct
48 Correct 233 ms 25352 KB Output is correct
49 Correct 229 ms 24832 KB Output is correct
50 Correct 212 ms 18528 KB Output is correct
51 Correct 197 ms 18684 KB Output is correct
52 Correct 213 ms 18556 KB Output is correct
53 Correct 291 ms 17316 KB Output is correct
54 Correct 213 ms 18268 KB Output is correct
55 Correct 19 ms 5768 KB Output is correct
56 Correct 13 ms 5636 KB Output is correct
57 Correct 122 ms 11740 KB Output is correct
58 Correct 42 ms 11580 KB Output is correct
59 Correct 278 ms 19220 KB Output is correct
60 Correct 809 ms 35300 KB Output is correct
61 Correct 257 ms 18872 KB Output is correct
62 Correct 276 ms 18932 KB Output is correct
63 Correct 330 ms 18736 KB Output is correct
64 Correct 993 ms 23424 KB Output is correct
65 Correct 217 ms 19452 KB Output is correct
66 Correct 505 ms 23808 KB Output is correct
67 Correct 116 ms 19508 KB Output is correct
68 Correct 358 ms 22936 KB Output is correct
69 Correct 385 ms 23036 KB Output is correct
70 Correct 427 ms 22308 KB Output is correct