답안 #943458

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
943458 2024-03-11T13:42:59 Z R_i_n_Nabil 경주 (Race) (IOI11_race) C++17
100 / 100
537 ms 57984 KB
#include "race.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;

#define endl '\n'
#define ll long long
#define mod INT_MAX

const int N = 2e5 + 40;
int k, ans = mod, siz[N], in[N], out[N], depth[N], flat[N], timer;
ll euler[N];

vector < pair < int , int > > g[N];
bitset < N > dead;

void dfs(int bap, int dada) {
    siz[bap] = 1;
    for(auto [cld, val] : g[bap]) {
        if(cld == dada or dead[cld]) continue;
        dfs(cld, bap);
        siz[bap] += siz[cld];
    }
}

int find(int bap, int dada, int sz) {
    for(auto [cld, val] : g[bap]) {
        if(cld == dada or dead[cld]) continue;
        if(siz[cld] > sz) return find(cld, bap, sz);
    }
    return bap;
}

void euler_tour(int bap, int dada, ll curr_val = 0, int dep = 0) {
    in[bap] = ++timer;
    flat[timer] = bap;
    euler[bap] = curr_val;
    depth[bap] = dep;
    for(auto[cld, val] : g[bap]) {
        if(cld == dada or dead[cld]) continue;
        euler_tour(cld, bap, val + curr_val, dep + 1);
    }
    out[bap] = timer;
}

void calculate_ans(int king) {
    timer = 0;
    euler_tour(king, king);

    gp_hash_table < ll , int > gp;
    gp[0] = 0;

    for(auto[cld, val] : g[king]) {
        // Query
        if(dead[cld]) continue;
        for (int i = in[cld]; i <= out[cld]; i++)
        {
           // if(euler[i] > k) continue;
            int v = flat[i];
            ll need = k - euler[v];
            auto it = gp.find(need);
            if(it != gp.end()) {
                ans = min(ans, depth[v] + gp[need]);
            }
        }
        // Update
        for(int i = in[cld]; i <= out[cld]; i++) {
            //if(euler[i] > k) continue; 
            int v = flat[i];
            auto it = gp.find(euler[v]);
            if(it == gp.end()) {
                gp[euler[v]] = depth[v];
            }
            else {
                gp[euler[v]] = min(depth[v], gp[euler[v]]);
            }
        }
    }
}

void solve_sub_prob(int curr) {
    dfs(curr, curr);
    int king = find(curr, curr, siz[curr] / 2);

    calculate_ans(king);
    dead[king] = 1;

    for(auto [cld, val] : g[king]) {
        if(dead[cld]) continue;
        solve_sub_prob(cld);
    }

}

int best_path(int N, int K, int H[][2], int L[])
{
    for (int i = 0; i < N - 1; ++i)
    {
        int u = H[i][0], v = H[i][1], w = L[i];
        g[u].emplace_back(v, w), g[v].emplace_back(u, w);
    }
    k = K;
    solve_sub_prob(0);

    if(ans == mod) ans = -1;
    return ans;
}

// void solve()
// {
//     cin >> n >> k;
//     pair<int, int> pr[n + 9];
//     for (int i = 1; i < n; i++)
//     {
//         cin >> pr[i].first >> pr[i].second;
//     }
//     for (int i = 1; i < n; i++)
//     {
//         int x; cin >> x;
//         g[pr[i].first].push_back({pr[i].second,x});
//         g[pr[i].second].push_back({pr[i].first, x});
//     }
//     solve_sub_prob(0);
//     if (ans < mod)
//         cout << ans << endl;
//     else
//         cout << -1 << endl;
// }

// int main()
// {
//    solve();

//     return 0;
// }
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12636 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 3 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12636 KB Output is correct
7 Correct 2 ms 12636 KB Output is correct
8 Correct 2 ms 12636 KB Output is correct
9 Correct 3 ms 12632 KB Output is correct
10 Correct 2 ms 12736 KB Output is correct
11 Correct 3 ms 12724 KB Output is correct
12 Correct 2 ms 12636 KB Output is correct
13 Correct 2 ms 12636 KB Output is correct
14 Correct 2 ms 12632 KB Output is correct
15 Correct 2 ms 12636 KB Output is correct
16 Correct 2 ms 12636 KB Output is correct
17 Correct 2 ms 12636 KB Output is correct
18 Correct 2 ms 12636 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12636 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 3 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12636 KB Output is correct
7 Correct 2 ms 12636 KB Output is correct
8 Correct 2 ms 12636 KB Output is correct
9 Correct 3 ms 12632 KB Output is correct
10 Correct 2 ms 12736 KB Output is correct
11 Correct 3 ms 12724 KB Output is correct
12 Correct 2 ms 12636 KB Output is correct
13 Correct 2 ms 12636 KB Output is correct
14 Correct 2 ms 12632 KB Output is correct
15 Correct 2 ms 12636 KB Output is correct
16 Correct 2 ms 12636 KB Output is correct
17 Correct 2 ms 12636 KB Output is correct
18 Correct 2 ms 12636 KB Output is correct
19 Correct 2 ms 12636 KB Output is correct
20 Correct 2 ms 12636 KB Output is correct
21 Correct 3 ms 12636 KB Output is correct
22 Correct 3 ms 12892 KB Output is correct
23 Correct 3 ms 12916 KB Output is correct
24 Correct 3 ms 12756 KB Output is correct
25 Correct 3 ms 12892 KB Output is correct
26 Correct 3 ms 12892 KB Output is correct
27 Correct 3 ms 12636 KB Output is correct
28 Correct 3 ms 12888 KB Output is correct
29 Correct 3 ms 12892 KB Output is correct
30 Correct 3 ms 12892 KB Output is correct
31 Correct 4 ms 12888 KB Output is correct
32 Correct 3 ms 12892 KB Output is correct
33 Correct 3 ms 12908 KB Output is correct
34 Correct 3 ms 12888 KB Output is correct
35 Correct 4 ms 12892 KB Output is correct
36 Correct 4 ms 12928 KB Output is correct
37 Correct 3 ms 12756 KB Output is correct
38 Correct 3 ms 12892 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12636 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 3 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12636 KB Output is correct
7 Correct 2 ms 12636 KB Output is correct
8 Correct 2 ms 12636 KB Output is correct
9 Correct 3 ms 12632 KB Output is correct
10 Correct 2 ms 12736 KB Output is correct
11 Correct 3 ms 12724 KB Output is correct
12 Correct 2 ms 12636 KB Output is correct
13 Correct 2 ms 12636 KB Output is correct
14 Correct 2 ms 12632 KB Output is correct
15 Correct 2 ms 12636 KB Output is correct
16 Correct 2 ms 12636 KB Output is correct
17 Correct 2 ms 12636 KB Output is correct
18 Correct 2 ms 12636 KB Output is correct
19 Correct 123 ms 19284 KB Output is correct
20 Correct 121 ms 19292 KB Output is correct
21 Correct 120 ms 19280 KB Output is correct
22 Correct 110 ms 19292 KB Output is correct
23 Correct 127 ms 19548 KB Output is correct
24 Correct 71 ms 19548 KB Output is correct
25 Correct 121 ms 23572 KB Output is correct
26 Correct 83 ms 26460 KB Output is correct
27 Correct 176 ms 24400 KB Output is correct
28 Correct 530 ms 56804 KB Output is correct
29 Correct 481 ms 55748 KB Output is correct
30 Correct 176 ms 25688 KB Output is correct
31 Correct 178 ms 25700 KB Output is correct
32 Correct 233 ms 25680 KB Output is correct
33 Correct 269 ms 24612 KB Output is correct
34 Correct 372 ms 42872 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 12636 KB Output is correct
2 Correct 3 ms 12636 KB Output is correct
3 Correct 2 ms 12636 KB Output is correct
4 Correct 3 ms 12636 KB Output is correct
5 Correct 2 ms 12636 KB Output is correct
6 Correct 2 ms 12636 KB Output is correct
7 Correct 2 ms 12636 KB Output is correct
8 Correct 2 ms 12636 KB Output is correct
9 Correct 3 ms 12632 KB Output is correct
10 Correct 2 ms 12736 KB Output is correct
11 Correct 3 ms 12724 KB Output is correct
12 Correct 2 ms 12636 KB Output is correct
13 Correct 2 ms 12636 KB Output is correct
14 Correct 2 ms 12632 KB Output is correct
15 Correct 2 ms 12636 KB Output is correct
16 Correct 2 ms 12636 KB Output is correct
17 Correct 2 ms 12636 KB Output is correct
18 Correct 2 ms 12636 KB Output is correct
19 Correct 2 ms 12636 KB Output is correct
20 Correct 2 ms 12636 KB Output is correct
21 Correct 3 ms 12636 KB Output is correct
22 Correct 3 ms 12892 KB Output is correct
23 Correct 3 ms 12916 KB Output is correct
24 Correct 3 ms 12756 KB Output is correct
25 Correct 3 ms 12892 KB Output is correct
26 Correct 3 ms 12892 KB Output is correct
27 Correct 3 ms 12636 KB Output is correct
28 Correct 3 ms 12888 KB Output is correct
29 Correct 3 ms 12892 KB Output is correct
30 Correct 3 ms 12892 KB Output is correct
31 Correct 4 ms 12888 KB Output is correct
32 Correct 3 ms 12892 KB Output is correct
33 Correct 3 ms 12908 KB Output is correct
34 Correct 3 ms 12888 KB Output is correct
35 Correct 4 ms 12892 KB Output is correct
36 Correct 4 ms 12928 KB Output is correct
37 Correct 3 ms 12756 KB Output is correct
38 Correct 3 ms 12892 KB Output is correct
39 Correct 123 ms 19284 KB Output is correct
40 Correct 121 ms 19292 KB Output is correct
41 Correct 120 ms 19280 KB Output is correct
42 Correct 110 ms 19292 KB Output is correct
43 Correct 127 ms 19548 KB Output is correct
44 Correct 71 ms 19548 KB Output is correct
45 Correct 121 ms 23572 KB Output is correct
46 Correct 83 ms 26460 KB Output is correct
47 Correct 176 ms 24400 KB Output is correct
48 Correct 530 ms 56804 KB Output is correct
49 Correct 481 ms 55748 KB Output is correct
50 Correct 176 ms 25688 KB Output is correct
51 Correct 178 ms 25700 KB Output is correct
52 Correct 233 ms 25680 KB Output is correct
53 Correct 269 ms 24612 KB Output is correct
54 Correct 372 ms 42872 KB Output is correct
55 Correct 12 ms 13856 KB Output is correct
56 Correct 12 ms 13236 KB Output is correct
57 Correct 86 ms 19828 KB Output is correct
58 Correct 37 ms 20676 KB Output is correct
59 Correct 133 ms 36732 KB Output is correct
60 Correct 499 ms 57984 KB Output is correct
61 Correct 196 ms 27900 KB Output is correct
62 Correct 185 ms 26516 KB Output is correct
63 Correct 248 ms 26452 KB Output is correct
64 Correct 524 ms 34120 KB Output is correct
65 Correct 350 ms 45644 KB Output is correct
66 Correct 537 ms 53172 KB Output is correct
67 Correct 94 ms 27840 KB Output is correct
68 Correct 250 ms 45032 KB Output is correct
69 Correct 245 ms 43100 KB Output is correct
70 Correct 248 ms 45176 KB Output is correct