Submission #1091739

# Submission time Handle Problem Language Result Execution time Memory
1091739 2024-09-22T01:37:48 Z daviedu Race (IOI11_race) C++17
100 / 100
426 ms 90816 KB
#include <bits/stdc++.h>
using namespace std;

#define fastio ios_base::sync_with_stdio(0); cin.tie(0)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(a) (int) (a).size()
#define ll long long
#define ld long double

struct P{
    ll x, y;
};

void dbg_out() { cerr << endl; }
template <typename H, typename... T>
void dbg_out(H h, T... t) { cerr << ' ' << h; dbg_out(t...); }
#define dbg(...) { cerr << #__VA_ARGS__ << ':'; dbg_out(__VA_ARGS__); }

const ll MX = 2e5+5;
vector<pair<ll, ll>> g [MX];
vector<ll> tour;
ll depth[MX], s[MX], st[MX], ed[MX];
ll dist[MX];

void calc(ll x, ll y, ll dp, ll ds){
    s[x] = 1;
    depth[x] = dp;
    st[x] = sz(tour);
    tour.push_back(x);
    dist[x] = ds;
    for (auto [z, w]: g[x]){
        if (z == y) continue;
        calc(z, x, dp+1, ds+w);
        s[x] += s[z];
    }
    ed[x] = sz(tour);
}

ll ans=MX+1;
ll delta = 0;
ll deltb = 0;
map<ll, ll> path;
set<ll> exists;
set<pair<ll, ll>> paths;
void dfs(ll x, ll y, ll k, bool keep){
    ll big=-1, bsize=-1, bdelta=0;
    for (auto [z, w]: g[x]){
        if (z == y) continue;
        if (s[z] > bsize) big = z, bsize = s[z], bdelta = w;
    }

    for (auto [z, w]: g[x]){
        if (z == y || z == big) continue;
        dfs(z, x, k, 0);
    }

    if (big != -1) dfs(big, x, k, 1), delta += bdelta, deltb++;

    // apply your delta to paths
    for (auto [z, w]: g[x]){
        if (z == y || z == big) continue;
        for (ll i=st[z]; i<ed[z]; i++){
            ll ds = dist[tour[i]] - dist[x];
            ll d = depth[tour[i]] - depth[x];

            if (!exists.count(k-ds-delta)) continue;
            ans = min(ans, d + path[k-ds-delta] + deltb);
        }
        for (ll i=st[z]; i<ed[z]; i++){
            ll ds = dist[tour[i]] - dist[x];
            ll d = depth[tour[i]] - depth[x];

            if (exists.count(ds-delta)) path[ds-delta] = min(path[ds-delta], d - deltb);
            else{
                exists.insert(ds-delta);
                path[ds-delta] = d - deltb;
            }
        }
    }
    if (exists.count(k-delta)){
        ans = min(path[k-delta] + deltb, ans);
    }
    exists.insert(-delta);
    path[-delta] = -deltb;

    if (keep) return;

    exists.clear();
    path.clear();
    delta = 0;
    deltb = 0;
}


int best_path(int n, int k, int edges[][2], int weights[]){
    for (int i=0; i<n-1; i++){
        g[edges[i][0]].push_back({edges[i][1], weights[i]});
        g[edges[i][1]].push_back({edges[i][0], weights[i]});
    }
    calc(0, 0, 0, 0);
    dfs(0, 0, k, 1);
    if (ans == MX+1) return -1;
    else return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16476 KB Output is correct
2 Correct 2 ms 16476 KB Output is correct
3 Correct 2 ms 16488 KB Output is correct
4 Correct 2 ms 16476 KB Output is correct
5 Correct 2 ms 16476 KB Output is correct
6 Correct 2 ms 16476 KB Output is correct
7 Correct 3 ms 16476 KB Output is correct
8 Correct 2 ms 16476 KB Output is correct
9 Correct 2 ms 16476 KB Output is correct
10 Correct 2 ms 16476 KB Output is correct
11 Correct 2 ms 16476 KB Output is correct
12 Correct 3 ms 16476 KB Output is correct
13 Correct 2 ms 16596 KB Output is correct
14 Correct 2 ms 16476 KB Output is correct
15 Correct 3 ms 16476 KB Output is correct
16 Correct 2 ms 16476 KB Output is correct
17 Correct 2 ms 16592 KB Output is correct
18 Correct 2 ms 16476 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16476 KB Output is correct
2 Correct 2 ms 16476 KB Output is correct
3 Correct 2 ms 16488 KB Output is correct
4 Correct 2 ms 16476 KB Output is correct
5 Correct 2 ms 16476 KB Output is correct
6 Correct 2 ms 16476 KB Output is correct
7 Correct 3 ms 16476 KB Output is correct
8 Correct 2 ms 16476 KB Output is correct
9 Correct 2 ms 16476 KB Output is correct
10 Correct 2 ms 16476 KB Output is correct
11 Correct 2 ms 16476 KB Output is correct
12 Correct 3 ms 16476 KB Output is correct
13 Correct 2 ms 16596 KB Output is correct
14 Correct 2 ms 16476 KB Output is correct
15 Correct 3 ms 16476 KB Output is correct
16 Correct 2 ms 16476 KB Output is correct
17 Correct 2 ms 16592 KB Output is correct
18 Correct 2 ms 16476 KB Output is correct
19 Correct 2 ms 16472 KB Output is correct
20 Correct 3 ms 16476 KB Output is correct
21 Correct 3 ms 16476 KB Output is correct
22 Correct 4 ms 16732 KB Output is correct
23 Correct 3 ms 16732 KB Output is correct
24 Correct 3 ms 16732 KB Output is correct
25 Correct 3 ms 16732 KB Output is correct
26 Correct 3 ms 16732 KB Output is correct
27 Correct 3 ms 16476 KB Output is correct
28 Correct 3 ms 16984 KB Output is correct
29 Correct 3 ms 16732 KB Output is correct
30 Correct 3 ms 16732 KB Output is correct
31 Correct 3 ms 16732 KB Output is correct
32 Correct 5 ms 16732 KB Output is correct
33 Correct 4 ms 16732 KB Output is correct
34 Correct 3 ms 16732 KB Output is correct
35 Correct 3 ms 16728 KB Output is correct
36 Correct 3 ms 16700 KB Output is correct
37 Correct 3 ms 16732 KB Output is correct
38 Correct 3 ms 16728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16476 KB Output is correct
2 Correct 2 ms 16476 KB Output is correct
3 Correct 2 ms 16488 KB Output is correct
4 Correct 2 ms 16476 KB Output is correct
5 Correct 2 ms 16476 KB Output is correct
6 Correct 2 ms 16476 KB Output is correct
7 Correct 3 ms 16476 KB Output is correct
8 Correct 2 ms 16476 KB Output is correct
9 Correct 2 ms 16476 KB Output is correct
10 Correct 2 ms 16476 KB Output is correct
11 Correct 2 ms 16476 KB Output is correct
12 Correct 3 ms 16476 KB Output is correct
13 Correct 2 ms 16596 KB Output is correct
14 Correct 2 ms 16476 KB Output is correct
15 Correct 3 ms 16476 KB Output is correct
16 Correct 2 ms 16476 KB Output is correct
17 Correct 2 ms 16592 KB Output is correct
18 Correct 2 ms 16476 KB Output is correct
19 Correct 80 ms 26064 KB Output is correct
20 Correct 79 ms 26064 KB Output is correct
21 Correct 85 ms 26076 KB Output is correct
22 Correct 79 ms 26076 KB Output is correct
23 Correct 137 ms 26312 KB Output is correct
24 Correct 89 ms 25548 KB Output is correct
25 Correct 49 ms 35780 KB Output is correct
26 Correct 43 ms 43992 KB Output is correct
27 Correct 119 ms 33488 KB Output is correct
28 Correct 195 ms 90816 KB Output is correct
29 Correct 187 ms 87856 KB Output is correct
30 Correct 132 ms 33416 KB Output is correct
31 Correct 144 ms 33484 KB Output is correct
32 Correct 158 ms 33488 KB Output is correct
33 Correct 158 ms 32200 KB Output is correct
34 Correct 312 ms 53836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 16476 KB Output is correct
2 Correct 2 ms 16476 KB Output is correct
3 Correct 2 ms 16488 KB Output is correct
4 Correct 2 ms 16476 KB Output is correct
5 Correct 2 ms 16476 KB Output is correct
6 Correct 2 ms 16476 KB Output is correct
7 Correct 3 ms 16476 KB Output is correct
8 Correct 2 ms 16476 KB Output is correct
9 Correct 2 ms 16476 KB Output is correct
10 Correct 2 ms 16476 KB Output is correct
11 Correct 2 ms 16476 KB Output is correct
12 Correct 3 ms 16476 KB Output is correct
13 Correct 2 ms 16596 KB Output is correct
14 Correct 2 ms 16476 KB Output is correct
15 Correct 3 ms 16476 KB Output is correct
16 Correct 2 ms 16476 KB Output is correct
17 Correct 2 ms 16592 KB Output is correct
18 Correct 2 ms 16476 KB Output is correct
19 Correct 2 ms 16472 KB Output is correct
20 Correct 3 ms 16476 KB Output is correct
21 Correct 3 ms 16476 KB Output is correct
22 Correct 4 ms 16732 KB Output is correct
23 Correct 3 ms 16732 KB Output is correct
24 Correct 3 ms 16732 KB Output is correct
25 Correct 3 ms 16732 KB Output is correct
26 Correct 3 ms 16732 KB Output is correct
27 Correct 3 ms 16476 KB Output is correct
28 Correct 3 ms 16984 KB Output is correct
29 Correct 3 ms 16732 KB Output is correct
30 Correct 3 ms 16732 KB Output is correct
31 Correct 3 ms 16732 KB Output is correct
32 Correct 5 ms 16732 KB Output is correct
33 Correct 4 ms 16732 KB Output is correct
34 Correct 3 ms 16732 KB Output is correct
35 Correct 3 ms 16728 KB Output is correct
36 Correct 3 ms 16700 KB Output is correct
37 Correct 3 ms 16732 KB Output is correct
38 Correct 3 ms 16728 KB Output is correct
39 Correct 80 ms 26064 KB Output is correct
40 Correct 79 ms 26064 KB Output is correct
41 Correct 85 ms 26076 KB Output is correct
42 Correct 79 ms 26076 KB Output is correct
43 Correct 137 ms 26312 KB Output is correct
44 Correct 89 ms 25548 KB Output is correct
45 Correct 49 ms 35780 KB Output is correct
46 Correct 43 ms 43992 KB Output is correct
47 Correct 119 ms 33488 KB Output is correct
48 Correct 195 ms 90816 KB Output is correct
49 Correct 187 ms 87856 KB Output is correct
50 Correct 132 ms 33416 KB Output is correct
51 Correct 144 ms 33484 KB Output is correct
52 Correct 158 ms 33488 KB Output is correct
53 Correct 158 ms 32200 KB Output is correct
54 Correct 312 ms 53836 KB Output is correct
55 Correct 15 ms 18008 KB Output is correct
56 Correct 7 ms 17240 KB Output is correct
57 Correct 59 ms 26600 KB Output is correct
58 Correct 35 ms 26304 KB Output is correct
59 Correct 75 ms 55244 KB Output is correct
60 Correct 163 ms 89936 KB Output is correct
61 Correct 173 ms 37548 KB Output is correct
62 Correct 116 ms 34500 KB Output is correct
63 Correct 134 ms 34760 KB Output is correct
64 Correct 426 ms 45176 KB Output is correct
65 Correct 385 ms 56000 KB Output is correct
66 Correct 201 ms 83116 KB Output is correct
67 Correct 89 ms 35924 KB Output is correct
68 Correct 254 ms 52804 KB Output is correct
69 Correct 282 ms 52852 KB Output is correct
70 Correct 258 ms 51264 KB Output is correct