Submission #524796

# Submission time Handle Problem Language Result Execution time Memory
524796 2022-02-10T04:30:41 Z XII Race (IOI11_race) C++17
100 / 100
374 ms 40904 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

#define fi first
#define se second
#define mp make_pair
#define eb emplace_back
#define ALL(x) (x).begin(), (x).end()

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORU(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)

#define IOS cin.tie(0)->sync_with_stdio(false);
#define PROB "IOI11_race"
//void Fi(){
//    if(fopen(PROB".inp", "r")){
//        freopen(PROB".inp", "r", stdin);
//        freopen(PROB".out", "w", stdout);
//    }
//}

const int INF = 1e9;
using pi = pair<int, int>;
vector<bool> vis;
vector<int> sz, mned, mark;
vector<vector<pi>> adj;
int lengthPath, ti, ans;

int dfs(int u, int p){
    sz[u] = 1;
    for(auto [w, v]: adj[u]) if(v != p && !vis[v]){
        sz[u] += dfs(v, u);
    }
    return sz[u];
}

int centroid(int u, int p, int n){
    for(auto [w, v]: adj[u]) if(v != p && !vis[v]){
        if(sz[v] > n / 2) return centroid(v, u, n);
    }
    return u;
}

stack<pi> tmp;
void dfs2(int u, int p, int dis, int edg){
    if(dis > lengthPath) return;

    if(mark[lengthPath - dis] == ti){
        ans = min(ans, edg + mned[lengthPath - dis]);
    }
    tmp.emplace(dis, edg);

    for(auto [w, v]: adj[u]) if(v != p && !vis[v]){
        dfs2(v, u, dis + w, edg + 1);
    }
}

void build(int u, int p){
    int c = centroid(u, p, dfs(u, p));

    mned[0] = 0;
    mark[0] = ++ti;

    for(auto [w, v]: adj[c]) if(!vis[v]){
        dfs2(v, c, w, 1);
        for(; !tmp.empty(); tmp.pop()){
            auto [dis, edg] = tmp.top();
            if(mark[dis] == ti){
                mned[dis] = min(mned[dis], edg);
            } else{
                mned[dis] = edg;
                mark[dis] = ti;
            }
        }
    }

    vis[c] = true;
    for(auto [w, v]: adj[c]) if(!vis[v]){
        build(v, c);
    }
}

int best_path(int n, int k, int h[][2], int l[]){
    sz.resize(n);
    adj.clear(); adj.resize(n);
    vis.assign(n, false);
    mned.resize(k + 1);
    mark.assign(k + 1, 0);
    ti = 0;
    lengthPath = k;
    ans = INF;
    FOR(i, 0, n - 1){
        adj[h[i][0]].eb(l[i], h[i][1]);
        adj[h[i][1]].eb(l[i], h[i][0]);
    }
    build(0, -1);
    return (ans == INF ? -1 : ans);
}

//const int N = 2e5 + 1;
//int n, k;
//int h[N][2], l[N];
//
//void enter(){
//    cin >> n >> k;
//    FOR(i, 0, n - 1) cin >> h[i][0] >> h[i][1];
//    FOR(i, 0, n - 1) cin >> l[i];
//}
//
//int main(){
//    IOS;
//    Fi();
//    enter();
//    cout << best_path(n, k, h, l);
//    return 0;
//}

# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 280 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 296 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 296 KB Output is correct
12 Correct 1 ms 280 KB Output is correct
13 Correct 0 ms 296 KB Output is correct
14 Correct 1 ms 300 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 296 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 280 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 296 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 296 KB Output is correct
12 Correct 1 ms 280 KB Output is correct
13 Correct 0 ms 296 KB Output is correct
14 Correct 1 ms 300 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 296 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 292 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 4 ms 7500 KB Output is correct
23 Correct 4 ms 6220 KB Output is correct
24 Correct 4 ms 7116 KB Output is correct
25 Correct 4 ms 6988 KB Output is correct
26 Correct 3 ms 3020 KB Output is correct
27 Correct 4 ms 6588 KB Output is correct
28 Correct 2 ms 1868 KB Output is correct
29 Correct 2 ms 2892 KB Output is correct
30 Correct 2 ms 3248 KB Output is correct
31 Correct 3 ms 5452 KB Output is correct
32 Correct 4 ms 5964 KB Output is correct
33 Correct 4 ms 6476 KB Output is correct
34 Correct 4 ms 4940 KB Output is correct
35 Correct 4 ms 6700 KB Output is correct
36 Correct 4 ms 7628 KB Output is correct
37 Correct 4 ms 6604 KB Output is correct
38 Correct 3 ms 4300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 280 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 296 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 296 KB Output is correct
12 Correct 1 ms 280 KB Output is correct
13 Correct 0 ms 296 KB Output is correct
14 Correct 1 ms 300 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 296 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 132 ms 9272 KB Output is correct
20 Correct 115 ms 9268 KB Output is correct
21 Correct 108 ms 9444 KB Output is correct
22 Correct 95 ms 9516 KB Output is correct
23 Correct 73 ms 9492 KB Output is correct
24 Correct 57 ms 9552 KB Output is correct
25 Correct 98 ms 12720 KB Output is correct
26 Correct 79 ms 16904 KB Output is correct
27 Correct 147 ms 18596 KB Output is correct
28 Correct 252 ms 32988 KB Output is correct
29 Correct 248 ms 31960 KB Output is correct
30 Correct 144 ms 18592 KB Output is correct
31 Correct 151 ms 18480 KB Output is correct
32 Correct 219 ms 18644 KB Output is correct
33 Correct 192 ms 17472 KB Output is correct
34 Correct 200 ms 18364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 0 ms 280 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 1 ms 304 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 296 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 296 KB Output is correct
12 Correct 1 ms 280 KB Output is correct
13 Correct 0 ms 296 KB Output is correct
14 Correct 1 ms 300 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 0 ms 204 KB Output is correct
17 Correct 1 ms 296 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 292 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 4 ms 7500 KB Output is correct
23 Correct 4 ms 6220 KB Output is correct
24 Correct 4 ms 7116 KB Output is correct
25 Correct 4 ms 6988 KB Output is correct
26 Correct 3 ms 3020 KB Output is correct
27 Correct 4 ms 6588 KB Output is correct
28 Correct 2 ms 1868 KB Output is correct
29 Correct 2 ms 2892 KB Output is correct
30 Correct 2 ms 3248 KB Output is correct
31 Correct 3 ms 5452 KB Output is correct
32 Correct 4 ms 5964 KB Output is correct
33 Correct 4 ms 6476 KB Output is correct
34 Correct 4 ms 4940 KB Output is correct
35 Correct 4 ms 6700 KB Output is correct
36 Correct 4 ms 7628 KB Output is correct
37 Correct 4 ms 6604 KB Output is correct
38 Correct 3 ms 4300 KB Output is correct
39 Correct 132 ms 9272 KB Output is correct
40 Correct 115 ms 9268 KB Output is correct
41 Correct 108 ms 9444 KB Output is correct
42 Correct 95 ms 9516 KB Output is correct
43 Correct 73 ms 9492 KB Output is correct
44 Correct 57 ms 9552 KB Output is correct
45 Correct 98 ms 12720 KB Output is correct
46 Correct 79 ms 16904 KB Output is correct
47 Correct 147 ms 18596 KB Output is correct
48 Correct 252 ms 32988 KB Output is correct
49 Correct 248 ms 31960 KB Output is correct
50 Correct 144 ms 18592 KB Output is correct
51 Correct 151 ms 18480 KB Output is correct
52 Correct 219 ms 18644 KB Output is correct
53 Correct 192 ms 17472 KB Output is correct
54 Correct 200 ms 18364 KB Output is correct
55 Correct 7 ms 1228 KB Output is correct
56 Correct 8 ms 1208 KB Output is correct
57 Correct 76 ms 9764 KB Output is correct
58 Correct 32 ms 9124 KB Output is correct
59 Correct 82 ms 18340 KB Output is correct
60 Correct 374 ms 40904 KB Output is correct
61 Correct 164 ms 18692 KB Output is correct
62 Correct 199 ms 26396 KB Output is correct
63 Correct 228 ms 26528 KB Output is correct
64 Correct 344 ms 23620 KB Output is correct
65 Correct 217 ms 19608 KB Output is correct
66 Correct 321 ms 37360 KB Output is correct
67 Correct 104 ms 27132 KB Output is correct
68 Correct 201 ms 27040 KB Output is correct
69 Correct 224 ms 27296 KB Output is correct
70 Correct 231 ms 26340 KB Output is correct