답안 #434368

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
434368 2021-06-21T05:13:29 Z cuom1999 경주 (Race) (IOI11_race) C++17
100 / 100
2060 ms 95808 KB
#include <bits/stdc++.h>
using namespace std;

// Usage: addEdge(u, v) - O(1)
//        init()        - O(n log)
//        use cenRoot, cenChild, cenDad to get the relations in Centroid Tree
struct CentroidDecomposition {
    int n, cenRoot;
    vector<int> cenDad, subCD;
    vector<vector<int>> cenChild, adj;
    vector<set<int>> s;

    // n vertices
    CentroidDecomposition(int n): n(n) {
        cenDad.resize(n + 1);
        cenChild.resize(n + 1);
        s.resize(n + 1);
        subCD.resize(n + 1);
        adj.resize(n + 1);
    }

    void addEdge(int u, int v) {
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    int dfsCD(int a, int par) {
        subCD[a] = 1;
        for (int i: s[a]) {
            if (i != par) {
                dfsCD(i, a);
                subCD[a] += subCD[i];
            }
        }
        return subCD[a];
    }

    int centroid(int a, int p, int n) {
        for (auto i: s[a]) {
            if (i != p && subCD[i] > n / 2) {
                return centroid(i, a, n);
            }
        }
        return a;
    }

    void centroidDec(int a, int p) {
        int n = dfsCD(a, p);
        int cen = centroid(a, p, n);
        cenDad[cen] = p;

        if(p) cenChild[p].push_back(cen);
        else cenRoot = cen;

        for (int i:s[cen]){
            s[i].erase(cen);
            centroidDec(i,cen);
        }
    }

    void init() {
        for (int i = 1; i <= n; i++) {
            for (auto j: adj[i]) s[i].insert(j);
        }
        dfsCD(1, 0);
        centroidDec(1, 0);
    }
};

set<array<int, 2>> adj[200005];
int s[200005], d[200005], minDist[1000005], res = 1e9;

void dfs(int u, int p) {
    for (auto i: adj[u]) {
        if (i[0] == p) continue;
        d[i[0]] = d[u] + 1;
        s[i[0]] = s[u] + i[1];
        dfs(i[0], u);
    }
}

vector<int> solve(int u, CentroidDecomposition& tree, int k) {
    vector<vector<int>> childV;
    vector<array<int, 2>> tmp;
    for (auto [i, c]: adj[u]) {
        tmp.push_back({i, c});
        adj[i].erase(adj[i].lower_bound({u, 0}));
    }
    for (auto i: tree.cenChild[u]) {
        childV.push_back(solve(i, tree, k));
    }
    for (auto [i, c]: tmp) {
        adj[i].insert({u, c});
    }

    vector<int> v = {u};


    d[u] = s[u] = 0;
    dfs(u, 0);
    
    minDist[0] = 0;

    for (auto v1: childV) {
        for (auto j: v1) {
            if (s[j] > k) continue;
            res = min(res, minDist[k - s[j]] + d[j]);
        }
        for (auto j: v1) {
            if (s[j] <= k)
                minDist[s[j]] = min(minDist[s[j]], d[j]);
            v.push_back(j);
        }
    }
    for (auto i: v) {
        if (s[i] <= k) {
            minDist[s[i]] = 1e9;
        }
    }
    return v;
}

int best_path(int N, int K, int H[][2], int L[]) {
    int n = N, k = K;
    CentroidDecomposition tree(n);

    for (int i = 0; i < n - 1; i++) {
        int u = H[i][0], v = H[i][1], c = L[i];
        // cin >> u >> v >> c;
        u++; v++;

        tree.addEdge(u, v);
        adj[u].insert({v, c});
        adj[v].insert({u, c});
    }
    tree.init();

    for (int i = 0; i <= k; i++) {
        minDist[i] = 1e9;
    }

    solve(tree.cenRoot, tree, k);

    if (res > n) {
        res = -1;
    }
    // cout << res << endl;
    return res;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 7 ms 9676 KB Output is correct
4 Correct 7 ms 9676 KB Output is correct
5 Correct 7 ms 9744 KB Output is correct
6 Correct 7 ms 9676 KB Output is correct
7 Correct 7 ms 9676 KB Output is correct
8 Correct 8 ms 9676 KB Output is correct
9 Correct 7 ms 9804 KB Output is correct
10 Correct 7 ms 9676 KB Output is correct
11 Correct 7 ms 9676 KB Output is correct
12 Correct 6 ms 9756 KB Output is correct
13 Correct 6 ms 9676 KB Output is correct
14 Correct 7 ms 9676 KB Output is correct
15 Correct 7 ms 9676 KB Output is correct
16 Correct 8 ms 9676 KB Output is correct
17 Correct 7 ms 9676 KB Output is correct
18 Correct 7 ms 9676 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 7 ms 9676 KB Output is correct
4 Correct 7 ms 9676 KB Output is correct
5 Correct 7 ms 9744 KB Output is correct
6 Correct 7 ms 9676 KB Output is correct
7 Correct 7 ms 9676 KB Output is correct
8 Correct 8 ms 9676 KB Output is correct
9 Correct 7 ms 9804 KB Output is correct
10 Correct 7 ms 9676 KB Output is correct
11 Correct 7 ms 9676 KB Output is correct
12 Correct 6 ms 9756 KB Output is correct
13 Correct 6 ms 9676 KB Output is correct
14 Correct 7 ms 9676 KB Output is correct
15 Correct 7 ms 9676 KB Output is correct
16 Correct 8 ms 9676 KB Output is correct
17 Correct 7 ms 9676 KB Output is correct
18 Correct 7 ms 9676 KB Output is correct
19 Correct 6 ms 9676 KB Output is correct
20 Correct 7 ms 9696 KB Output is correct
21 Correct 8 ms 10060 KB Output is correct
22 Correct 13 ms 13644 KB Output is correct
23 Correct 11 ms 13048 KB Output is correct
24 Correct 11 ms 13388 KB Output is correct
25 Correct 10 ms 13260 KB Output is correct
26 Correct 9 ms 11340 KB Output is correct
27 Correct 11 ms 13132 KB Output is correct
28 Correct 9 ms 10828 KB Output is correct
29 Correct 10 ms 11364 KB Output is correct
30 Correct 9 ms 11468 KB Output is correct
31 Correct 10 ms 12612 KB Output is correct
32 Correct 12 ms 12988 KB Output is correct
33 Correct 11 ms 13148 KB Output is correct
34 Correct 10 ms 12392 KB Output is correct
35 Correct 13 ms 13224 KB Output is correct
36 Correct 11 ms 13644 KB Output is correct
37 Correct 11 ms 13048 KB Output is correct
38 Correct 10 ms 11944 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 7 ms 9676 KB Output is correct
4 Correct 7 ms 9676 KB Output is correct
5 Correct 7 ms 9744 KB Output is correct
6 Correct 7 ms 9676 KB Output is correct
7 Correct 7 ms 9676 KB Output is correct
8 Correct 8 ms 9676 KB Output is correct
9 Correct 7 ms 9804 KB Output is correct
10 Correct 7 ms 9676 KB Output is correct
11 Correct 7 ms 9676 KB Output is correct
12 Correct 6 ms 9756 KB Output is correct
13 Correct 6 ms 9676 KB Output is correct
14 Correct 7 ms 9676 KB Output is correct
15 Correct 7 ms 9676 KB Output is correct
16 Correct 8 ms 9676 KB Output is correct
17 Correct 7 ms 9676 KB Output is correct
18 Correct 7 ms 9676 KB Output is correct
19 Correct 517 ms 45048 KB Output is correct
20 Correct 506 ms 46548 KB Output is correct
21 Correct 521 ms 46668 KB Output is correct
22 Correct 519 ms 46788 KB Output is correct
23 Correct 678 ms 46656 KB Output is correct
24 Correct 369 ms 46532 KB Output is correct
25 Correct 836 ms 50324 KB Output is correct
26 Correct 355 ms 50420 KB Output is correct
27 Correct 667 ms 82064 KB Output is correct
28 Correct 1948 ms 91452 KB Output is correct
29 Correct 1997 ms 91428 KB Output is correct
30 Correct 644 ms 82172 KB Output is correct
31 Correct 649 ms 82080 KB Output is correct
32 Correct 1092 ms 83788 KB Output is correct
33 Correct 1362 ms 83156 KB Output is correct
34 Correct 1278 ms 83628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 7 ms 9676 KB Output is correct
4 Correct 7 ms 9676 KB Output is correct
5 Correct 7 ms 9744 KB Output is correct
6 Correct 7 ms 9676 KB Output is correct
7 Correct 7 ms 9676 KB Output is correct
8 Correct 8 ms 9676 KB Output is correct
9 Correct 7 ms 9804 KB Output is correct
10 Correct 7 ms 9676 KB Output is correct
11 Correct 7 ms 9676 KB Output is correct
12 Correct 6 ms 9756 KB Output is correct
13 Correct 6 ms 9676 KB Output is correct
14 Correct 7 ms 9676 KB Output is correct
15 Correct 7 ms 9676 KB Output is correct
16 Correct 8 ms 9676 KB Output is correct
17 Correct 7 ms 9676 KB Output is correct
18 Correct 7 ms 9676 KB Output is correct
19 Correct 6 ms 9676 KB Output is correct
20 Correct 7 ms 9696 KB Output is correct
21 Correct 8 ms 10060 KB Output is correct
22 Correct 13 ms 13644 KB Output is correct
23 Correct 11 ms 13048 KB Output is correct
24 Correct 11 ms 13388 KB Output is correct
25 Correct 10 ms 13260 KB Output is correct
26 Correct 9 ms 11340 KB Output is correct
27 Correct 11 ms 13132 KB Output is correct
28 Correct 9 ms 10828 KB Output is correct
29 Correct 10 ms 11364 KB Output is correct
30 Correct 9 ms 11468 KB Output is correct
31 Correct 10 ms 12612 KB Output is correct
32 Correct 12 ms 12988 KB Output is correct
33 Correct 11 ms 13148 KB Output is correct
34 Correct 10 ms 12392 KB Output is correct
35 Correct 13 ms 13224 KB Output is correct
36 Correct 11 ms 13644 KB Output is correct
37 Correct 11 ms 13048 KB Output is correct
38 Correct 10 ms 11944 KB Output is correct
39 Correct 517 ms 45048 KB Output is correct
40 Correct 506 ms 46548 KB Output is correct
41 Correct 521 ms 46668 KB Output is correct
42 Correct 519 ms 46788 KB Output is correct
43 Correct 678 ms 46656 KB Output is correct
44 Correct 369 ms 46532 KB Output is correct
45 Correct 836 ms 50324 KB Output is correct
46 Correct 355 ms 50420 KB Output is correct
47 Correct 667 ms 82064 KB Output is correct
48 Correct 1948 ms 91452 KB Output is correct
49 Correct 1997 ms 91428 KB Output is correct
50 Correct 644 ms 82172 KB Output is correct
51 Correct 649 ms 82080 KB Output is correct
52 Correct 1092 ms 83788 KB Output is correct
53 Correct 1362 ms 83156 KB Output is correct
54 Correct 1278 ms 83628 KB Output is correct
55 Correct 31 ms 13508 KB Output is correct
56 Correct 31 ms 13480 KB Output is correct
57 Correct 310 ms 45608 KB Output is correct
58 Correct 174 ms 49716 KB Output is correct
59 Correct 318 ms 51200 KB Output is correct
60 Correct 2060 ms 95264 KB Output is correct
61 Correct 651 ms 82236 KB Output is correct
62 Correct 640 ms 85968 KB Output is correct
63 Correct 1083 ms 87868 KB Output is correct
64 Correct 1391 ms 86204 KB Output is correct
65 Correct 1241 ms 84504 KB Output is correct
66 Correct 2028 ms 95268 KB Output is correct
67 Correct 898 ms 95808 KB Output is correct
68 Correct 1220 ms 88024 KB Output is correct
69 Correct 1126 ms 88524 KB Output is correct
70 Correct 1063 ms 84356 KB Output is correct