답안 #1082599

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1082599 2024-08-31T17:06:52 Z f0rizen 경주 (Race) (IOI11_race) C++17
100 / 100
635 ms 39244 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int inf = 1e9 + 7;
const ll infll = 1e18;

struct Edge {
    int to, w;
};

int k;
vector<vector<Edge>> g;
vector<bool> used;
vector<int> sz;
map<int, int> mp;
int ans = inf;

void dfs_sz(int v, int p = -1) {
    sz[v] = 1;
    for (auto [u, w] : g[v]) {
        if (u != p && !used[u]) {
            dfs_sz(u, v);
            sz[v] += sz[u];
        }
    }
}

void dfs_ans(int v, int p = -1, int sum = 0, int d = 0) {
    if (mp.find(k - sum) != mp.end()) {
        ans = min(ans, mp[k - sum] + d);
    }
    for (auto [u, w] : g[v]) {
        if (u != p && !used[u]) {
            dfs_ans(u, v, sum + w, d + 1);
        }
    }
}

void dfs_mp(int v, int p = -1, int sum = 0, int d = 0) {
    if (mp.find(sum) == mp.end()) {
        mp[sum] = d;
    } else {
        mp[sum] = min(mp[sum], d);
    }
    for (auto [u, w] : g[v]) {
        if (u != p && !used[u]) {
            dfs_mp(u, v, sum + w, d + 1);
        }
    }
}

int centroid(int v, int p, int n) {
    for (auto [u, w] : g[v]) {
        if (u != p && !used[u]) {
            if (sz[u] * 2 > n) {
                return centroid(u, v, n);
            }
        }
    }
    return v;
}

void build(int v) {
    dfs_sz(v);
    mp[0] = 0;
    for (auto [u, w] : g[v]) {
        if (!used[u]) {
            dfs_ans(u, v, w, 1);
            dfs_mp(u, v, w, 1);
        }
    }
    mp.clear();
    used[v] = 1;
    for (auto [u, w] : g[v]) {
        if (!used[u]) {
            build(centroid(u, v, sz[u]));
        }
    }
}

int best_path(int N, int K, int H[][2], int L[]) {
    k = K;
    g.resize(N);
    for (int i = 0; i < N - 1; ++i) {
        int u = H[i][0], v = H[i][1], w = L[i];
        g[u].push_back({v, w});
        g[v].push_back({u, w});
    }
    used.resize(N);
    sz.resize(N);
    dfs_sz(0);
    build(centroid(0, -1, sz[0]));
    if (ans == inf) {
        return -1;
    } else {
        return ans;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 2 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 2 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 2 ms 348 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 2 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 168 ms 9308 KB Output is correct
20 Correct 158 ms 9304 KB Output is correct
21 Correct 148 ms 9308 KB Output is correct
22 Correct 138 ms 9300 KB Output is correct
23 Correct 210 ms 9580 KB Output is correct
24 Correct 115 ms 9552 KB Output is correct
25 Correct 132 ms 13648 KB Output is correct
26 Correct 69 ms 15100 KB Output is correct
27 Correct 201 ms 18644 KB Output is correct
28 Correct 578 ms 39244 KB Output is correct
29 Correct 572 ms 38480 KB Output is correct
30 Correct 196 ms 18768 KB Output is correct
31 Correct 195 ms 18772 KB Output is correct
32 Correct 258 ms 18772 KB Output is correct
33 Correct 323 ms 17908 KB Output is correct
34 Correct 573 ms 27644 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 348 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 2 ms 348 KB Output is correct
23 Correct 2 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 2 ms 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 2 ms 348 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 2 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 168 ms 9308 KB Output is correct
40 Correct 158 ms 9304 KB Output is correct
41 Correct 148 ms 9308 KB Output is correct
42 Correct 138 ms 9300 KB Output is correct
43 Correct 210 ms 9580 KB Output is correct
44 Correct 115 ms 9552 KB Output is correct
45 Correct 132 ms 13648 KB Output is correct
46 Correct 69 ms 15100 KB Output is correct
47 Correct 201 ms 18644 KB Output is correct
48 Correct 578 ms 39244 KB Output is correct
49 Correct 572 ms 38480 KB Output is correct
50 Correct 196 ms 18768 KB Output is correct
51 Correct 195 ms 18772 KB Output is correct
52 Correct 258 ms 18772 KB Output is correct
53 Correct 323 ms 17908 KB Output is correct
54 Correct 573 ms 27644 KB Output is correct
55 Correct 16 ms 1388 KB Output is correct
56 Correct 9 ms 1228 KB Output is correct
57 Correct 94 ms 9556 KB Output is correct
58 Correct 31 ms 9168 KB Output is correct
59 Correct 211 ms 18768 KB Output is correct
60 Correct 577 ms 37976 KB Output is correct
61 Correct 243 ms 20272 KB Output is correct
62 Correct 194 ms 18960 KB Output is correct
63 Correct 242 ms 18984 KB Output is correct
64 Correct 635 ms 23376 KB Output is correct
65 Correct 584 ms 28820 KB Output is correct
66 Correct 564 ms 36436 KB Output is correct
67 Correct 78 ms 19544 KB Output is correct
68 Correct 344 ms 27216 KB Output is correct
69 Correct 334 ms 27472 KB Output is correct
70 Correct 305 ms 26148 KB Output is correct