답안 #94228

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
94228 2019-01-16T22:46:20 Z arknave 경주 (Race) (IOI11_race) C++14
100 / 100
1723 ms 53160 KB
#include "race.h"
#include <bits/stdc++.h>

#define all(x) begin(x), end(x)

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

void dfs_sz(const vector<vector<pii>>& tree, vi& vis, vi& size, int u, int p = -1) {
    size[u] = 1;
    for (auto& e : tree[u]) {
        int v = e.first;
        if (v == p or vis[v])
            continue;
        dfs_sz(tree, vis, size, v, u);
        size[u] += size[v];
    }
}

int find_center(const vector<vector<pii>>& tree, vi& vis, vi& size, int tot, int u, int p = -1) {
    for (auto& e : tree[u]) {
        int v = e.first;
        if (v == p or vis[v]) 
            continue;
        if (2 * size[v] >= tot)
            return find_center(tree, vis, size, tot, v, u);
    }

    return u;
}

template <typename F>
void centroid(F& f, const vector<vector<pii>>& tree, vi& vis, vi& size, int u, int p = -1) {
    dfs_sz(tree, vis, size, u, p);
    int c = find_center(tree, vis, size, size[u], u, p);
    // do some shit here
    f(c);

    vis[c] = true;
    for (auto& e : tree[c]) {
        int v = e.first;
        if (v != p and !vis[v]) {
            centroid(f, tree, vis, size, v, c);
        }
    }
}

void dfs_path(const vector<vector<pii>>& tree, const vi& vis, int u, int p, ll path_len, int edges, map<ll, int>& m) {
    auto it = m.find(path_len);
    if (it == end(m) or it->second > edges) {
        m[path_len] = edges;
    }

    for (auto& e : tree[u]) {
        int v, w;
        tie(v, w) = e;
        if (vis[v] or v == p)
            continue;
        dfs_path(tree, vis, v, u, path_len + w, edges + 1, m);
    }
}

int solve(const vector<vector<pii>>& tree, int k) {
    int n = tree.size();
    vi vis(n, 0);

    constexpr int INF = 1e9 + 7;
    int ans = INF;
    auto f = [&](int u) {
        // one more dfs
        // path length -> shortest leg with that length
        // add then merge maps
        map<ll, int> left, right;
        left[0] = 0;
        for (auto& e : tree[u]) {
            int v, w;
            tie(v, w) = e;
            if (vis[v]) {
                continue;
            }

            right.clear();
            // cout << "Starting path at node " << v << " weight " << w << '\n';
            dfs_path(tree, vis, v, u, w, 1, right);

            for (auto& p : right) {
                // cout << "Considering w " << p.first << " e " << p.second << '\n';
                ll path_len = p.first;
                int edges = p.second;
                auto it = left.find(k - path_len);
                if (it != end(left)) {
                    ans = min(ans, edges + it->second);
                }
            }

            for (auto& p : right) {
                if (left.find(p.first) == end(left) or left[p.first] > p.second) {
                    left[p.first] = p.second;
                }
            }
        }
    };

    vi size(n, 0);
    centroid(f, tree, vis, size, 0, -1);

    return ans == INF ? -1 : ans;
}

int best_path(int n, int k, int h[][2], int l[]) {
    // create the tree
    vector<vector<pii>> tree(n);
    for (int i = 0; i < n - 1; ++i) {
        int u = h[i][0];
        int v = h[i][1];
        int w = l[i];

        tree[u].emplace_back(v, w);
        tree[v].emplace_back(u, w);
    }

    return solve(tree, k);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 252 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 380 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 348 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 252 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 380 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 348 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 380 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 3 ms 376 KB Output is correct
22 Correct 4 ms 504 KB Output is correct
23 Correct 4 ms 504 KB Output is correct
24 Correct 3 ms 504 KB Output is correct
25 Correct 3 ms 504 KB Output is correct
26 Correct 3 ms 528 KB Output is correct
27 Correct 3 ms 376 KB Output is correct
28 Correct 4 ms 504 KB Output is correct
29 Correct 3 ms 504 KB Output is correct
30 Correct 4 ms 504 KB Output is correct
31 Correct 3 ms 504 KB Output is correct
32 Correct 4 ms 504 KB Output is correct
33 Correct 4 ms 504 KB Output is correct
34 Correct 4 ms 504 KB Output is correct
35 Correct 4 ms 508 KB Output is correct
36 Correct 4 ms 504 KB Output is correct
37 Correct 4 ms 504 KB Output is correct
38 Correct 4 ms 508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 252 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 380 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 348 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 265 ms 8908 KB Output is correct
20 Correct 244 ms 8824 KB Output is correct
21 Correct 236 ms 8952 KB Output is correct
22 Correct 213 ms 9080 KB Output is correct
23 Correct 381 ms 9208 KB Output is correct
24 Correct 169 ms 9080 KB Output is correct
25 Correct 198 ms 15224 KB Output is correct
26 Correct 101 ms 17708 KB Output is correct
27 Correct 400 ms 17372 KB Output is correct
28 Correct 1723 ms 53160 KB Output is correct
29 Correct 1659 ms 51832 KB Output is correct
30 Correct 373 ms 17380 KB Output is correct
31 Correct 366 ms 17272 KB Output is correct
32 Correct 449 ms 17400 KB Output is correct
33 Correct 812 ms 16272 KB Output is correct
34 Correct 1425 ms 31488 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 376 KB Output is correct
4 Correct 2 ms 252 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 380 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 348 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 2 ms 380 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
21 Correct 3 ms 376 KB Output is correct
22 Correct 4 ms 504 KB Output is correct
23 Correct 4 ms 504 KB Output is correct
24 Correct 3 ms 504 KB Output is correct
25 Correct 3 ms 504 KB Output is correct
26 Correct 3 ms 528 KB Output is correct
27 Correct 3 ms 376 KB Output is correct
28 Correct 4 ms 504 KB Output is correct
29 Correct 3 ms 504 KB Output is correct
30 Correct 4 ms 504 KB Output is correct
31 Correct 3 ms 504 KB Output is correct
32 Correct 4 ms 504 KB Output is correct
33 Correct 4 ms 504 KB Output is correct
34 Correct 4 ms 504 KB Output is correct
35 Correct 4 ms 508 KB Output is correct
36 Correct 4 ms 504 KB Output is correct
37 Correct 4 ms 504 KB Output is correct
38 Correct 4 ms 508 KB Output is correct
39 Correct 265 ms 8908 KB Output is correct
40 Correct 244 ms 8824 KB Output is correct
41 Correct 236 ms 8952 KB Output is correct
42 Correct 213 ms 9080 KB Output is correct
43 Correct 381 ms 9208 KB Output is correct
44 Correct 169 ms 9080 KB Output is correct
45 Correct 198 ms 15224 KB Output is correct
46 Correct 101 ms 17708 KB Output is correct
47 Correct 400 ms 17372 KB Output is correct
48 Correct 1723 ms 53160 KB Output is correct
49 Correct 1659 ms 51832 KB Output is correct
50 Correct 373 ms 17380 KB Output is correct
51 Correct 366 ms 17272 KB Output is correct
52 Correct 449 ms 17400 KB Output is correct
53 Correct 812 ms 16272 KB Output is correct
54 Correct 1425 ms 31488 KB Output is correct
55 Correct 31 ms 1784 KB Output is correct
56 Correct 17 ms 1272 KB Output is correct
57 Correct 141 ms 9112 KB Output is correct
58 Correct 53 ms 9068 KB Output is correct
59 Correct 490 ms 25468 KB Output is correct
60 Correct 1484 ms 51404 KB Output is correct
61 Correct 469 ms 18936 KB Output is correct
62 Correct 371 ms 19704 KB Output is correct
63 Correct 425 ms 19704 KB Output is correct
64 Correct 1340 ms 28496 KB Output is correct
65 Correct 1197 ms 33400 KB Output is correct
66 Correct 1465 ms 51496 KB Output is correct
67 Correct 172 ms 20200 KB Output is correct
68 Correct 688 ms 30896 KB Output is correct
69 Correct 787 ms 31352 KB Output is correct
70 Correct 629 ms 29560 KB Output is correct