Submission #753714

# Submission time Handle Problem Language Result Execution time Memory
753714 2023-06-05T18:20:06 Z nicksms Race (IOI11_race) C++17
100 / 100
1928 ms 77880 KB
/**
 *      Author:  Nicholas Winschel
 *      Created: 05.10.2023 22:07:36
**/

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using db=long double;
template<class T> using V=vector<T>;
using vi = V<int>;
using vl = V<ll>;
using pi = pair<int,int>;
#define f first
#define s second
#define sz(x) (int)((x).size())

int best_path(int N, int K, int H[][2], int L[]) {
	V<bool> r(N);
    vi s(N);
    V<V<pi>> a(N);
    if (K==0) return -1;
    for (int i = 0; i < N-1; i++) {
        a[H[i][0]].emplace_back(H[i][1],i);
        a[H[i][1]].emplace_back(H[i][0],i);
    }
    auto dfs = [&](int n, int p, auto &&dfs) -> int {
        s[n]=1;
        for (auto e : a[n]) {
            if (e.f != p && !r[e.f]) s[n] += dfs(e.f,n,dfs);
        }
        return s[n];
    };
    auto gc = [&](int n, int ms, int p, auto &&gc) -> int {
        for (auto e : a[n]) {
            if (e.f != p && !r[e.f]) {
                if (s[e.f] * 2 > ms) return gc(e.f,ms,n,gc);
            }
        }
        return n;
    };
    int best = N+1;
    auto dnq = [&](int n, auto &&dnq) -> void {
        int C = gc(n, dfs(n, -1, dfs), -1, gc);
        // cout << "cent: " << C << endl;

        V<map<ll, int>> cl(sz(a[C]));

        map<ll, pi> bes;

        auto ins= [&](ll d, int dep) {
            // cout << "ins: " << d << " " << dep << endl;
            if (!bes.count(d)) {
                bes[d]={dep,N+1};
                return;
            }
            if (bes[d].f >= dep) {
                bes[d].s=bes[d].f;
                bes[d].f=dep;
                return;
            }
            if (bes[d].s >= dep) {
                bes[d].s=dep;
                return;
            }
        };
        
        auto gn =[&](ll d, int dep) {
            // cout << "gn: " << d << " " << dep << endl;
            if (!bes.count(d)) return N+1;
            // cout << "ret: " << (dep == bes[d].f ? bes[d].s : bes[d].f) << endl;
            return (dep == bes[d].f ? bes[d].s : bes[d].f);
        };

        auto dfs2 =[&](int v, int p, int c, ll d, int dep, auto &&dfs2) -> void {
            if (dep > best || d > K) return;
            if (!cl[c].count(d)) cl[c][d]=dep;
            cl[c][d] = min(cl[c][d], dep);
            for (auto e : a[v]) {
                if (e.f != p && !r[e.f]) {
                    dfs2(e.f, v, c, d+L[e.s], dep+1, dfs2);
                }
            }
        };

        for (int i = 0; i < sz(a[C]); i++) if (!r[a[C][i].f]) {
            // cout << "dir: " << a[C][i].f << endl;
            dfs2(a[C][i].f, C, i, (ll)L[a[C][i].s], 1, dfs2);
            if (cl[i].count(K)) best = min(cl[i][K], best);
            for (auto pr : cl[i]) {
                ins(pr.f, pr.s);
            }
        }
        for (int i = 0; i < sz(a[C]); i++) {
            for (auto pr : cl[i]) {
                best = min(best, pr.s+gn(K-pr.f,cl[i].count(K-pr.f) ? cl[i][K-pr.f] : -1));
            }
        }

        r[C]=1;
        for (auto e : a[C]) {
            if (!r[e.f]) dnq(e.f, dnq);
        }
    };
    dnq(0,dnq);

    return best > N-1 ? -1 : best;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 308 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 308 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 308 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 308 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 3 ms 468 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 324 KB Output is correct
25 Correct 3 ms 596 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 532 KB Output is correct
29 Correct 3 ms 468 KB Output is correct
30 Correct 4 ms 596 KB Output is correct
31 Correct 3 ms 596 KB Output is correct
32 Correct 3 ms 576 KB Output is correct
33 Correct 3 ms 596 KB Output is correct
34 Correct 2 ms 340 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 2 ms 448 KB Output is correct
37 Correct 3 ms 596 KB Output is correct
38 Correct 3 ms 572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 308 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 308 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 221 ms 9384 KB Output is correct
20 Correct 233 ms 9296 KB Output is correct
21 Correct 260 ms 9424 KB Output is correct
22 Correct 218 ms 9628 KB Output is correct
23 Correct 86 ms 9596 KB Output is correct
24 Correct 88 ms 9420 KB Output is correct
25 Correct 143 ms 10968 KB Output is correct
26 Correct 103 ms 14880 KB Output is correct
27 Correct 229 ms 18888 KB Output is correct
28 Correct 270 ms 25480 KB Output is correct
29 Correct 268 ms 24796 KB Output is correct
30 Correct 297 ms 18880 KB Output is correct
31 Correct 284 ms 18828 KB Output is correct
32 Correct 294 ms 18772 KB Output is correct
33 Correct 586 ms 17748 KB Output is correct
34 Correct 243 ms 18164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 308 KB Output is correct
6 Correct 1 ms 308 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 308 KB Output is correct
18 Correct 1 ms 308 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 3 ms 468 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 324 KB Output is correct
25 Correct 3 ms 596 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 2 ms 532 KB Output is correct
29 Correct 3 ms 468 KB Output is correct
30 Correct 4 ms 596 KB Output is correct
31 Correct 3 ms 596 KB Output is correct
32 Correct 3 ms 576 KB Output is correct
33 Correct 3 ms 596 KB Output is correct
34 Correct 2 ms 340 KB Output is correct
35 Correct 2 ms 340 KB Output is correct
36 Correct 2 ms 448 KB Output is correct
37 Correct 3 ms 596 KB Output is correct
38 Correct 3 ms 572 KB Output is correct
39 Correct 221 ms 9384 KB Output is correct
40 Correct 233 ms 9296 KB Output is correct
41 Correct 260 ms 9424 KB Output is correct
42 Correct 218 ms 9628 KB Output is correct
43 Correct 86 ms 9596 KB Output is correct
44 Correct 88 ms 9420 KB Output is correct
45 Correct 143 ms 10968 KB Output is correct
46 Correct 103 ms 14880 KB Output is correct
47 Correct 229 ms 18888 KB Output is correct
48 Correct 270 ms 25480 KB Output is correct
49 Correct 268 ms 24796 KB Output is correct
50 Correct 297 ms 18880 KB Output is correct
51 Correct 284 ms 18828 KB Output is correct
52 Correct 294 ms 18772 KB Output is correct
53 Correct 586 ms 17748 KB Output is correct
54 Correct 243 ms 18164 KB Output is correct
55 Correct 37 ms 2036 KB Output is correct
56 Correct 22 ms 1188 KB Output is correct
57 Correct 188 ms 9552 KB Output is correct
58 Correct 75 ms 20072 KB Output is correct
59 Correct 727 ms 36976 KB Output is correct
60 Correct 1928 ms 77880 KB Output is correct
61 Correct 394 ms 20940 KB Output is correct
62 Correct 447 ms 25560 KB Output is correct
63 Correct 523 ms 25524 KB Output is correct
64 Correct 1458 ms 52336 KB Output is correct
65 Correct 242 ms 19404 KB Output is correct
66 Correct 911 ms 27536 KB Output is correct
67 Correct 179 ms 41128 KB Output is correct
68 Correct 258 ms 20208 KB Output is correct
69 Correct 272 ms 20100 KB Output is correct
70 Correct 235 ms 19556 KB Output is correct