답안 #1059059

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1059059 2024-08-14T16:34:37 Z CodeLakVN 경주 (Race) (IOI11_race) C++17
100 / 100
195 ms 41428 KB
#ifndef LAK
#include <race.h>
#endif // LAK

#include <bits/stdc++.h>

#define no "NO"
#define yes "YES"
#define F first
#define S second
#define vec vector
#define task "main"
#define ll long long
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())

using namespace std;

template<typename U, typename V> bool maximize(U &a, V b) {
    if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool minimize(U &a, V b) {
    if (a > b) { a = b; return 1; } return 0;
}

const int N = (int)2e5 + 9;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;

int numNode, numDist;
vector<ii> adj[N];
int child[N], minEdge[(int)1e6 + 5];
bool del[N];
int ans = INF;

int countChild(int u, int p) {
    child[u] = 1;
    for (auto [v, w] : adj[u]) {
        if (v == p || del[v]) continue;
        countChild(v, u);
        child[u] += child[v];
    }
    return child[u];
}

int centroid(int u, int p, int m) {
    for (auto [v, w] : adj[u]) {
        if (v == p || del[v]) continue;
        if (child[v] > m / 2) return centroid(v, u, m);
    }
    return u;
}

void DFS_calc(int u, int p, int depth, int curLen) {
    if (curLen > numDist) return;
    minimize(ans, depth + minEdge[numDist - curLen]);
    for (auto [v, w] : adj[u]) {
        if (del[v] || v == p) continue;
        DFS_calc(v, u, depth + 1, curLen + w);
    }
}

void DFS_minimize(int u, int p, int depth, int curLen, vector<int> &tmp) {
    if (curLen > numDist) return;
    tmp.push_back(curLen);
    minimize(minEdge[curLen], depth);
    for (auto [v, w] : adj[u]) {
        if (del[v] || v == p) continue;
        DFS_minimize(v, u, depth + 1, curLen + w, tmp);
    }
}

void cd(int u) {
    int m = countChild(u, 0);
    u = centroid(u, 0, m);

    vector<int> tmp;
    for (auto [v, w] : adj[u]) {
        if (del[v]) continue;
        DFS_calc(v, u, 1, w);
        DFS_minimize(v, u, 1, w, tmp);
    }
    del[u] = 1;

    for (int len : tmp) minEdge[len] = INF;

    for (auto [v, w] : adj[u]) {
        if (del[v]) continue;
        cd(v);
    }
}

int best_path(int n, int k, int h[][2], int l[]) {
    numNode = n;
    numDist = k;
    FOR(i, 0, numNode - 2) {
        int u = h[i][0], v = h[i][1];
        int w = l[i];
        u++;
        v++;
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
    }

    FOR(i, 1, (int)1e6) minEdge[i] = INF;

    cd(1);

    return (ans != INF ? ans : -1);
}

#ifdef LAK
int edge[N][2];
int length[N];

void main_code() {
    int numNode, requireDist;
    cin >> numNode >> requireDist;
    FOR(i, 0, numNode - 2) {
        int u, v, l;
        cin >> u >> v >> l;
        edge[i][0] = u, edge[i][1] = v;
        length[i] = l;
    }
    cout << best_path(numNode, requireDist, edge, length);
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    const bool MULTITEST = 0;
    int num_test = 1; if (MULTITEST) cin >> num_test;
    while (num_test--) { main_code(); cout << "\n"; }
}
#endif // LAK

/* Lak lu theo dieu nhac */
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13656 KB Output is correct
2 Correct 2 ms 13660 KB Output is correct
3 Correct 2 ms 13660 KB Output is correct
4 Correct 2 ms 13660 KB Output is correct
5 Correct 2 ms 13660 KB Output is correct
6 Correct 2 ms 13660 KB Output is correct
7 Correct 2 ms 13652 KB Output is correct
8 Correct 2 ms 13660 KB Output is correct
9 Correct 2 ms 13668 KB Output is correct
10 Correct 2 ms 13680 KB Output is correct
11 Correct 2 ms 13660 KB Output is correct
12 Correct 2 ms 13656 KB Output is correct
13 Correct 2 ms 13660 KB Output is correct
14 Correct 3 ms 13656 KB Output is correct
15 Correct 2 ms 13656 KB Output is correct
16 Correct 2 ms 13656 KB Output is correct
17 Correct 2 ms 13660 KB Output is correct
18 Correct 2 ms 13660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13656 KB Output is correct
2 Correct 2 ms 13660 KB Output is correct
3 Correct 2 ms 13660 KB Output is correct
4 Correct 2 ms 13660 KB Output is correct
5 Correct 2 ms 13660 KB Output is correct
6 Correct 2 ms 13660 KB Output is correct
7 Correct 2 ms 13652 KB Output is correct
8 Correct 2 ms 13660 KB Output is correct
9 Correct 2 ms 13668 KB Output is correct
10 Correct 2 ms 13680 KB Output is correct
11 Correct 2 ms 13660 KB Output is correct
12 Correct 2 ms 13656 KB Output is correct
13 Correct 2 ms 13660 KB Output is correct
14 Correct 3 ms 13656 KB Output is correct
15 Correct 2 ms 13656 KB Output is correct
16 Correct 2 ms 13656 KB Output is correct
17 Correct 2 ms 13660 KB Output is correct
18 Correct 2 ms 13660 KB Output is correct
19 Correct 2 ms 13660 KB Output is correct
20 Correct 2 ms 13660 KB Output is correct
21 Correct 2 ms 13488 KB Output is correct
22 Correct 2 ms 13664 KB Output is correct
23 Correct 2 ms 13656 KB Output is correct
24 Correct 2 ms 13656 KB Output is correct
25 Correct 2 ms 13672 KB Output is correct
26 Correct 3 ms 13660 KB Output is correct
27 Correct 2 ms 13660 KB Output is correct
28 Correct 2 ms 13660 KB Output is correct
29 Correct 2 ms 13660 KB Output is correct
30 Correct 2 ms 13660 KB Output is correct
31 Correct 2 ms 13660 KB Output is correct
32 Correct 2 ms 13660 KB Output is correct
33 Correct 2 ms 13660 KB Output is correct
34 Correct 2 ms 13660 KB Output is correct
35 Correct 2 ms 13660 KB Output is correct
36 Correct 2 ms 13732 KB Output is correct
37 Correct 2 ms 13660 KB Output is correct
38 Correct 2 ms 13148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13656 KB Output is correct
2 Correct 2 ms 13660 KB Output is correct
3 Correct 2 ms 13660 KB Output is correct
4 Correct 2 ms 13660 KB Output is correct
5 Correct 2 ms 13660 KB Output is correct
6 Correct 2 ms 13660 KB Output is correct
7 Correct 2 ms 13652 KB Output is correct
8 Correct 2 ms 13660 KB Output is correct
9 Correct 2 ms 13668 KB Output is correct
10 Correct 2 ms 13680 KB Output is correct
11 Correct 2 ms 13660 KB Output is correct
12 Correct 2 ms 13656 KB Output is correct
13 Correct 2 ms 13660 KB Output is correct
14 Correct 3 ms 13656 KB Output is correct
15 Correct 2 ms 13656 KB Output is correct
16 Correct 2 ms 13656 KB Output is correct
17 Correct 2 ms 13660 KB Output is correct
18 Correct 2 ms 13660 KB Output is correct
19 Correct 63 ms 20888 KB Output is correct
20 Correct 60 ms 21016 KB Output is correct
21 Correct 65 ms 21232 KB Output is correct
22 Correct 61 ms 21588 KB Output is correct
23 Correct 37 ms 21080 KB Output is correct
24 Correct 27 ms 21080 KB Output is correct
25 Correct 68 ms 24420 KB Output is correct
26 Correct 55 ms 28892 KB Output is correct
27 Correct 77 ms 26348 KB Output is correct
28 Correct 121 ms 40664 KB Output is correct
29 Correct 121 ms 39512 KB Output is correct
30 Correct 76 ms 26228 KB Output is correct
31 Correct 79 ms 26192 KB Output is correct
32 Correct 85 ms 25680 KB Output is correct
33 Correct 98 ms 24404 KB Output is correct
34 Correct 83 ms 25936 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 13656 KB Output is correct
2 Correct 2 ms 13660 KB Output is correct
3 Correct 2 ms 13660 KB Output is correct
4 Correct 2 ms 13660 KB Output is correct
5 Correct 2 ms 13660 KB Output is correct
6 Correct 2 ms 13660 KB Output is correct
7 Correct 2 ms 13652 KB Output is correct
8 Correct 2 ms 13660 KB Output is correct
9 Correct 2 ms 13668 KB Output is correct
10 Correct 2 ms 13680 KB Output is correct
11 Correct 2 ms 13660 KB Output is correct
12 Correct 2 ms 13656 KB Output is correct
13 Correct 2 ms 13660 KB Output is correct
14 Correct 3 ms 13656 KB Output is correct
15 Correct 2 ms 13656 KB Output is correct
16 Correct 2 ms 13656 KB Output is correct
17 Correct 2 ms 13660 KB Output is correct
18 Correct 2 ms 13660 KB Output is correct
19 Correct 2 ms 13660 KB Output is correct
20 Correct 2 ms 13660 KB Output is correct
21 Correct 2 ms 13488 KB Output is correct
22 Correct 2 ms 13664 KB Output is correct
23 Correct 2 ms 13656 KB Output is correct
24 Correct 2 ms 13656 KB Output is correct
25 Correct 2 ms 13672 KB Output is correct
26 Correct 3 ms 13660 KB Output is correct
27 Correct 2 ms 13660 KB Output is correct
28 Correct 2 ms 13660 KB Output is correct
29 Correct 2 ms 13660 KB Output is correct
30 Correct 2 ms 13660 KB Output is correct
31 Correct 2 ms 13660 KB Output is correct
32 Correct 2 ms 13660 KB Output is correct
33 Correct 2 ms 13660 KB Output is correct
34 Correct 2 ms 13660 KB Output is correct
35 Correct 2 ms 13660 KB Output is correct
36 Correct 2 ms 13732 KB Output is correct
37 Correct 2 ms 13660 KB Output is correct
38 Correct 2 ms 13148 KB Output is correct
39 Correct 63 ms 20888 KB Output is correct
40 Correct 60 ms 21016 KB Output is correct
41 Correct 65 ms 21232 KB Output is correct
42 Correct 61 ms 21588 KB Output is correct
43 Correct 37 ms 21080 KB Output is correct
44 Correct 27 ms 21080 KB Output is correct
45 Correct 68 ms 24420 KB Output is correct
46 Correct 55 ms 28892 KB Output is correct
47 Correct 77 ms 26348 KB Output is correct
48 Correct 121 ms 40664 KB Output is correct
49 Correct 121 ms 39512 KB Output is correct
50 Correct 76 ms 26228 KB Output is correct
51 Correct 79 ms 26192 KB Output is correct
52 Correct 85 ms 25680 KB Output is correct
53 Correct 98 ms 24404 KB Output is correct
54 Correct 83 ms 25936 KB Output is correct
55 Correct 7 ms 13660 KB Output is correct
56 Correct 9 ms 13660 KB Output is correct
57 Correct 45 ms 20324 KB Output is correct
58 Correct 20 ms 20172 KB Output is correct
59 Correct 59 ms 27448 KB Output is correct
60 Correct 195 ms 41428 KB Output is correct
61 Correct 85 ms 26704 KB Output is correct
62 Correct 95 ms 26588 KB Output is correct
63 Correct 104 ms 27616 KB Output is correct
64 Correct 186 ms 27332 KB Output is correct
65 Correct 81 ms 27216 KB Output is correct
66 Correct 185 ms 36436 KB Output is correct
67 Correct 49 ms 28868 KB Output is correct
68 Correct 98 ms 28364 KB Output is correct
69 Correct 100 ms 27600 KB Output is correct
70 Correct 93 ms 27852 KB Output is correct