답안 #553067

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
553067 2022-04-24T14:50:56 Z RaresFelix 경주 (Race) (IOI11_race) C++17
100 / 100
1006 ms 53300 KB
#include "race.h"
#include <bits/stdc++.h>
#define MN 200071
#define MK 20
using namespace std;
using ll = long long;
vector<pair<int, int> > L[MN];

namespace LCA {
    static inline int log2(const int &v) {
        return 31 - __builtin_clz(v);
    }
    int In[MN], Out[MN], tmr;
    ll Niv[MN], RMQ[MK][MN * 2];
    void dfi(int u, int p, int niv) {
        In[u] = Out[u] = ++tmr;
        RMQ[0][In[u]] = Niv[u] = niv;
        for(auto [it, w] : L[u]) if(it != p) {
            dfi(it, u, niv + w);
            RMQ[0][Out[u] = ++tmr] = Niv[u];
        }
    }
    void init(int n) {
        for(int k = 1; k < MK; ++k)
            for(int i = 1; i <= n; ++i)
                RMQ[k][i] = min(RMQ[k-1][i], RMQ[k-1][i + (1 << (k-1))]);
    }
    int query(int st, int dr) {
        int l = log2(dr - st + 1);
        return min(RMQ[l][st], RMQ[l][dr - (1 << l) + 1]);
    }
    int dist(int u, int v) {
        return Niv[u] + Niv[v] - 2 * query(min(In[u], In[v]), max(Out[u], Out[v]));
    }
}

bool Used[MN];
int Sz[MN];

void dfi(int u, int p) {
    Sz[u] = 1;
    for(auto &[it, w] : L[u])
        if(it != p && !Used[it]) dfi(it, u), Sz[u] += Sz[it];
}

int centr(int u, int p, const int &sz) {
    for(auto [it, w] : L[u])
        if(it != p && !Used[it] && Sz[it] * 2 > sz) return centr(it, u, sz);
    return u;
}

void dfc(int u, int p, vector<tuple<int, ll, int> > &comp, ll d, int niv = 1) {
    comp.push_back({u, d, niv});
    for(auto [it, w] : L[u])
        if(it != p && !Used[it])
            dfc(it, u, comp, d + w, niv + 1);
}

void desc(int u, int par_comp, int k, int &rez) {
    dfi(u, 0);
    int c = centr(u, 0, Sz[u]);
    map<ll, int> Posib;
    Used[c] = 1;
    Posib[0] = 0;
    for(auto [it, w] : L[c]) {
        if(Used[it]) continue;
        vector<tuple<int, ll, int> > comp;
        dfc(it, c, comp, w);
        for(auto &[u, dist, niv] : comp)
            if(Posib.count(k - dist)) rez = min(rez, niv + Posib[k - dist]);
        for(auto &[u, dist, niv] : comp)
            if(!Posib.count(dist)) Posib[dist] = niv;
            else Posib[dist] = min(Posib[dist], niv);
    }
    for(auto &[it, w] : L[c])
        if(!Used[it])
            desc(it, c, k, rez);
}

int best_path(int N, int K, int H[][2], int W[]) {
    for(int i = 0; i < N - 1; ++i) {
        L[H[i][0] + 1].push_back({H[i][1] + 1, W[i]});
        L[H[i][1] + 1].push_back({H[i][0] + 1, W[i]});
    }
    int rez = numeric_limits<int>::max() / 4;
    desc(1, 0, K, rez);
    if(rez == numeric_limits<int>::max() / 4) rez = -1;
    return rez;
}

# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5008 KB Output is correct
2 Correct 4 ms 4948 KB Output is correct
3 Correct 4 ms 4948 KB Output is correct
4 Correct 3 ms 5008 KB Output is correct
5 Correct 3 ms 4960 KB Output is correct
6 Correct 3 ms 5008 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 5008 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 4 ms 5008 KB Output is correct
13 Correct 3 ms 5004 KB Output is correct
14 Correct 3 ms 5008 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5008 KB Output is correct
2 Correct 4 ms 4948 KB Output is correct
3 Correct 4 ms 4948 KB Output is correct
4 Correct 3 ms 5008 KB Output is correct
5 Correct 3 ms 4960 KB Output is correct
6 Correct 3 ms 5008 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 5008 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 4 ms 5008 KB Output is correct
13 Correct 3 ms 5004 KB Output is correct
14 Correct 3 ms 5008 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 4 ms 5012 KB Output is correct
22 Correct 5 ms 5152 KB Output is correct
23 Correct 4 ms 5204 KB Output is correct
24 Correct 5 ms 5204 KB Output is correct
25 Correct 5 ms 5076 KB Output is correct
26 Correct 6 ms 5076 KB Output is correct
27 Correct 6 ms 5076 KB Output is correct
28 Correct 6 ms 5152 KB Output is correct
29 Correct 5 ms 5148 KB Output is correct
30 Correct 5 ms 5144 KB Output is correct
31 Correct 4 ms 5144 KB Output is correct
32 Correct 4 ms 5204 KB Output is correct
33 Correct 6 ms 5204 KB Output is correct
34 Correct 4 ms 5204 KB Output is correct
35 Correct 4 ms 5204 KB Output is correct
36 Correct 4 ms 5148 KB Output is correct
37 Correct 6 ms 5204 KB Output is correct
38 Correct 4 ms 5136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5008 KB Output is correct
2 Correct 4 ms 4948 KB Output is correct
3 Correct 4 ms 4948 KB Output is correct
4 Correct 3 ms 5008 KB Output is correct
5 Correct 3 ms 4960 KB Output is correct
6 Correct 3 ms 5008 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 5008 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 4 ms 5008 KB Output is correct
13 Correct 3 ms 5004 KB Output is correct
14 Correct 3 ms 5008 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 296 ms 12892 KB Output is correct
20 Correct 233 ms 12820 KB Output is correct
21 Correct 248 ms 13088 KB Output is correct
22 Correct 211 ms 12236 KB Output is correct
23 Correct 292 ms 12404 KB Output is correct
24 Correct 166 ms 12988 KB Output is correct
25 Correct 205 ms 19504 KB Output is correct
26 Correct 108 ms 19916 KB Output is correct
27 Correct 350 ms 17796 KB Output is correct
28 Correct 987 ms 53300 KB Output is correct
29 Correct 982 ms 52432 KB Output is correct
30 Correct 346 ms 17612 KB Output is correct
31 Correct 343 ms 17520 KB Output is correct
32 Correct 439 ms 17752 KB Output is correct
33 Correct 506 ms 20776 KB Output is correct
34 Correct 778 ms 38184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 5008 KB Output is correct
2 Correct 4 ms 4948 KB Output is correct
3 Correct 4 ms 4948 KB Output is correct
4 Correct 3 ms 5008 KB Output is correct
5 Correct 3 ms 4960 KB Output is correct
6 Correct 3 ms 5008 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 3 ms 5008 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 4 ms 5008 KB Output is correct
13 Correct 3 ms 5004 KB Output is correct
14 Correct 3 ms 5008 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 3 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 4 ms 5012 KB Output is correct
22 Correct 5 ms 5152 KB Output is correct
23 Correct 4 ms 5204 KB Output is correct
24 Correct 5 ms 5204 KB Output is correct
25 Correct 5 ms 5076 KB Output is correct
26 Correct 6 ms 5076 KB Output is correct
27 Correct 6 ms 5076 KB Output is correct
28 Correct 6 ms 5152 KB Output is correct
29 Correct 5 ms 5148 KB Output is correct
30 Correct 5 ms 5144 KB Output is correct
31 Correct 4 ms 5144 KB Output is correct
32 Correct 4 ms 5204 KB Output is correct
33 Correct 6 ms 5204 KB Output is correct
34 Correct 4 ms 5204 KB Output is correct
35 Correct 4 ms 5204 KB Output is correct
36 Correct 4 ms 5148 KB Output is correct
37 Correct 6 ms 5204 KB Output is correct
38 Correct 4 ms 5136 KB Output is correct
39 Correct 296 ms 12892 KB Output is correct
40 Correct 233 ms 12820 KB Output is correct
41 Correct 248 ms 13088 KB Output is correct
42 Correct 211 ms 12236 KB Output is correct
43 Correct 292 ms 12404 KB Output is correct
44 Correct 166 ms 12988 KB Output is correct
45 Correct 205 ms 19504 KB Output is correct
46 Correct 108 ms 19916 KB Output is correct
47 Correct 350 ms 17796 KB Output is correct
48 Correct 987 ms 53300 KB Output is correct
49 Correct 982 ms 52432 KB Output is correct
50 Correct 346 ms 17612 KB Output is correct
51 Correct 343 ms 17520 KB Output is correct
52 Correct 439 ms 17752 KB Output is correct
53 Correct 506 ms 20776 KB Output is correct
54 Correct 778 ms 38184 KB Output is correct
55 Correct 27 ms 6448 KB Output is correct
56 Correct 21 ms 5912 KB Output is correct
57 Correct 145 ms 13456 KB Output is correct
58 Correct 48 ms 11584 KB Output is correct
59 Correct 362 ms 26792 KB Output is correct
60 Correct 988 ms 51628 KB Output is correct
61 Correct 360 ms 19288 KB Output is correct
62 Correct 350 ms 17684 KB Output is correct
63 Correct 399 ms 17548 KB Output is correct
64 Correct 931 ms 31964 KB Output is correct
65 Correct 786 ms 39020 KB Output is correct
66 Correct 1006 ms 51956 KB Output is correct
67 Correct 158 ms 17552 KB Output is correct
68 Correct 490 ms 35544 KB Output is correct
69 Correct 496 ms 36008 KB Output is correct
70 Correct 518 ms 34144 KB Output is correct