답안 #1097846

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1097846 2024-10-08T10:38:35 Z SiddharthJoshi6263 경주 (Race) (IOI11_race) C++17
100 / 100
362 ms 108876 KB
#include <bits/stdc++.h>

using namespace std;

// #ifdef ONLINE_JUDGE
// #define debug(...)
// #else
// #include "debug.h"
// #endif

#define f first
#define s second
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (long long)(x).size()
#define pii pair<long long, long long>

long long ANS = LLONG_MAX;

void dfs_sze(long long u, long long par, vector<vector<pii>> &adj, vector<long long> &sze, vector<long long> &len, vector<long long> &depth) {
    sze[u] = 1;
    for (auto v : adj[u]) {
        if (v.f != par) {
            len[v.f] = len[u] + v.s;
            depth[v.f] = depth[u] + 1;
            dfs_sze(v.f, u, adj, sze, len, depth);
            sze[u] += sze[v.f];
        }
    }
}

void small_to_large_merging(long long K, long long u, long long par, vector<long long> &sze, vector<long long> &len, vector<long long> &depth, vector<vector<pii>> &adj, vector<multiset<pii>> &sets) {
    multiset<pii> smalls;
    for (auto x : adj[u]) {
        if (x.f != par) {
            small_to_large_merging(K, x.f, u, sze, len, depth, adj, sets);
            if (sz(sets[x.f]) > sz(smalls)) {
                swap(sets[x.f], smalls);
            }
            for (auto y : sets[x.f]) {
                long long act_len = y.f - len[u];
                long long act_depth = y.s - depth[u];
                pii find_pair = {K - act_len + len[u], -1ll};
                auto it = smalls.lower_bound(find_pair);
                if (it != smalls.end()) {
                    if (it->f == K + len[u] - act_len) {
                        ANS = min(ANS, it->s - depth[u] + act_depth);
                    }
                }
            }
            for (auto y : sets[x.f]) {
                smalls.insert(y);
            }
        }
    }
    if (sz(smalls) > sz(sets[u])) {
        swap(smalls, sets[u]);
        for (auto x : smalls) {
            sets[u].insert(x);
        }
        sets[u].insert({len[u], depth[u]});
        pii find_pair = {K + len[u], -1ll};
        auto it = sets[u].lower_bound(find_pair);
        if (it != sets[u].end()) {
            if (it->f == K + len[u]) {
                ANS = min(ANS, it->s - depth[u]);
            }
        }
    }
    else {
        for (auto x : smalls) {
            sets[u].insert(x);
        }
        sets[u].insert({len[u], depth[u]});
        pii find_pair = {K + len[u], -1ll};
        auto it = smalls.lower_bound(find_pair);
        if (it != smalls.end()) {
            if (it->f == K + len[u]) {
                ANS = min(ANS, it->s - depth[u]);
            }
        }
    }
}

int best_path (int N, int k, int H[][2], int L[]) {
    ANS = LLONG_MAX;
    long long n = N, K = k;
    vector<vector<pii>> adj(n);
    for (long long i = 0; i < n - 1; i++) {
        adj[H[i][0]].pb({H[i][1], L[i]});
        adj[H[i][1]].pb({H[i][0], L[i]});
    }
    // debug(adj);
    vector<long long> sze(n, 0);
    vector<long long> len(n, 0);
    vector<long long> depth(n, 0);
    dfs_sze(0, -1, adj, sze, len, depth);
    // debug(sze, len, depth);
    vector<multiset<pii>> sets(n);
    small_to_large_merging(K, 0, -1, sze, len, depth, adj, sets);
    if (ANS == LLONG_MAX) {
        return -1;
    }
    return ANS;
}

// void solve() {
//     int n, K;
//     cin >> n >> K;
//     int H[n - 1][2];
//     int L[n - 1];
//     for (long long i = 0; i < n - 1; i++) {
//         cin >> H[i][0] >> H[i][1];
//     }
//     for (long long i = 0; i < n - 1; i++) {
//         cin >> L[i];
//     }
//     long long ans;
//     cin >> ans;
//     cout << best_path(n, K, H, L) << ' ' << ans << endl;
// }

// signed main() {
//     ios_base::sync_with_stdio(false);
//     cin.tie(NULL);
//     cout.tie(NULL);
//     long long zz = 1;
//     // cin >> zz;
//     for (long long i = 1; i <= zz; i++) {
//         // cout << "Case #" << i << ": ";
//         solve();
//     }
//     return 0;
// }
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 492 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 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 0 ms 348 KB Output is correct
15 Correct 1 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 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 492 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 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 0 ms 348 KB Output is correct
15 Correct 1 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 0 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 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 2 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 2 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 2 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 1 ms 600 KB Output is correct
38 Correct 2 ms 604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 492 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 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 0 ms 348 KB Output is correct
15 Correct 1 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 0 ms 348 KB Output is correct
19 Correct 168 ms 52556 KB Output is correct
20 Correct 163 ms 51616 KB Output is correct
21 Correct 177 ms 52384 KB Output is correct
22 Correct 181 ms 47700 KB Output is correct
23 Correct 205 ms 65324 KB Output is correct
24 Correct 151 ms 51540 KB Output is correct
25 Correct 96 ms 38736 KB Output is correct
26 Correct 70 ms 49800 KB Output is correct
27 Correct 216 ms 66132 KB Output is correct
28 Correct 208 ms 99492 KB Output is correct
29 Correct 225 ms 96592 KB Output is correct
30 Correct 223 ms 66128 KB Output is correct
31 Correct 213 ms 66132 KB Output is correct
32 Correct 256 ms 66224 KB Output is correct
33 Correct 252 ms 80724 KB Output is correct
34 Correct 261 ms 79696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 492 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 1 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 0 ms 348 KB Output is correct
15 Correct 1 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 0 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 604 KB Output is correct
22 Correct 1 ms 604 KB Output is correct
23 Correct 1 ms 604 KB Output is correct
24 Correct 2 ms 604 KB Output is correct
25 Correct 1 ms 604 KB Output is correct
26 Correct 1 ms 600 KB Output is correct
27 Correct 1 ms 604 KB Output is correct
28 Correct 1 ms 604 KB Output is correct
29 Correct 2 ms 604 KB Output is correct
30 Correct 1 ms 604 KB Output is correct
31 Correct 1 ms 604 KB Output is correct
32 Correct 1 ms 604 KB Output is correct
33 Correct 2 ms 604 KB Output is correct
34 Correct 1 ms 604 KB Output is correct
35 Correct 1 ms 604 KB Output is correct
36 Correct 1 ms 604 KB Output is correct
37 Correct 1 ms 600 KB Output is correct
38 Correct 2 ms 604 KB Output is correct
39 Correct 168 ms 52556 KB Output is correct
40 Correct 163 ms 51616 KB Output is correct
41 Correct 177 ms 52384 KB Output is correct
42 Correct 181 ms 47700 KB Output is correct
43 Correct 205 ms 65324 KB Output is correct
44 Correct 151 ms 51540 KB Output is correct
45 Correct 96 ms 38736 KB Output is correct
46 Correct 70 ms 49800 KB Output is correct
47 Correct 216 ms 66132 KB Output is correct
48 Correct 208 ms 99492 KB Output is correct
49 Correct 225 ms 96592 KB Output is correct
50 Correct 223 ms 66128 KB Output is correct
51 Correct 213 ms 66132 KB Output is correct
52 Correct 256 ms 66224 KB Output is correct
53 Correct 252 ms 80724 KB Output is correct
54 Correct 261 ms 79696 KB Output is correct
55 Correct 11 ms 4444 KB Output is correct
56 Correct 12 ms 5000 KB Output is correct
57 Correct 179 ms 68716 KB Output is correct
58 Correct 76 ms 29268 KB Output is correct
59 Correct 69 ms 49848 KB Output is correct
60 Correct 207 ms 97684 KB Output is correct
61 Correct 231 ms 66348 KB Output is correct
62 Correct 215 ms 66128 KB Output is correct
63 Correct 250 ms 66132 KB Output is correct
64 Correct 357 ms 108876 KB Output is correct
65 Correct 362 ms 100392 KB Output is correct
66 Correct 211 ms 89936 KB Output is correct
67 Correct 241 ms 59324 KB Output is correct
68 Correct 258 ms 67808 KB Output is correct
69 Correct 282 ms 73552 KB Output is correct
70 Correct 243 ms 65136 KB Output is correct