답안 #890385

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
890385 2023-12-21T05:15:00 Z anarch_y 경주 (Race) (IOI11_race) C++17
100 / 100
413 ms 52048 KB
#include <bits/stdc++.h>
#include "race.h"
using namespace std;
using ll = long long;
#define all(x) begin(x), end(x)
#define sz(x) (int)x.size()
#define pb push_back
 
const int maxn = 200001;
 
int N, K;
vector<int> adj[maxn];
vector<pair<int, int>> adj2[maxn];
int sub[maxn];
bool done[maxn];
 
int subtree_size(int s, int p){
    sub[s] = 1;
    for(auto u: adj[s]){
        if(u == p or done[u]) continue;
        sub[s] += subtree_size(u, s);
    }
    return sub[s];
}
 
int get_centroid(int s, int p, int num){
    for(auto u: adj[s]){
        if(u == p or done[u]) continue;
        if(2*sub[u] > num) return get_centroid(u, s, num);
    }
    return s;
}

ll ans = 1e18;
ll best[1000001];

void calc(int s, int p, bool fill, int d, int wt){
    if(wt > K) return;
    if(fill) best[wt] = min(best[wt], 1LL*d);
    else ans = min(ans, best[K-wt] + 1LL*d);
    for(auto &[u, t]: adj2[s]){
        if(u == p or done[u]) continue;
        calc(u, s, fill, d+1, wt+t);
    } 
}

void del(int s, int p, int d, int wt){
    if(wt > K) return;
    best[wt] = 1e18;
    for(auto &[u, t]: adj2[s]){
        if(u == p or done[u]) continue;
        del(u, s, d+1, wt+t);
    } 
}
 
void centroid_decomp(int s){
    int num = subtree_size(s, 0);
    int cent = get_centroid(s, 0, num);

    best[0] = 0;
    for(auto &[u, w]: adj2[cent]){
        if(done[u]) continue;
        calc(u, cent, false, 1, w);
        calc(u, cent, true, 1, w);
    }
    for(auto &[u, w]: adj2[cent]){
        if(done[u]) continue;
        del(u, cent, 1, w);
    }

    done[cent] = true;
    for(auto u: adj[cent]){
        if(!done[u]) centroid_decomp(u);
    }
}
 
int best_path(int _N, int _K, int H[][2], int L[]){
    N = _N, K = _K;
    for(int i=0; i<N-1; i++){
        int a = H[i][0], b = H[i][1], w = L[i];
        a++; b++;
        adj[a].pb(b); adj[b].pb(a);
        adj2[a].pb({b, w}); adj2[b].pb({a, w});
    }
    fill(best, best+K+1, 1e18);
    centroid_decomp(1);
    if(ans == 1e18) ans = -1;
    return ans;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15964 KB Output is correct
2 Correct 3 ms 15964 KB Output is correct
3 Correct 3 ms 16116 KB Output is correct
4 Correct 4 ms 15964 KB Output is correct
5 Correct 3 ms 15964 KB Output is correct
6 Correct 3 ms 16132 KB Output is correct
7 Correct 3 ms 15964 KB Output is correct
8 Correct 3 ms 15964 KB Output is correct
9 Correct 3 ms 15964 KB Output is correct
10 Correct 4 ms 15964 KB Output is correct
11 Correct 4 ms 15960 KB Output is correct
12 Correct 3 ms 15964 KB Output is correct
13 Correct 4 ms 16140 KB Output is correct
14 Correct 3 ms 16108 KB Output is correct
15 Correct 4 ms 15964 KB Output is correct
16 Correct 3 ms 15964 KB Output is correct
17 Correct 3 ms 15964 KB Output is correct
18 Correct 3 ms 16136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15964 KB Output is correct
2 Correct 3 ms 15964 KB Output is correct
3 Correct 3 ms 16116 KB Output is correct
4 Correct 4 ms 15964 KB Output is correct
5 Correct 3 ms 15964 KB Output is correct
6 Correct 3 ms 16132 KB Output is correct
7 Correct 3 ms 15964 KB Output is correct
8 Correct 3 ms 15964 KB Output is correct
9 Correct 3 ms 15964 KB Output is correct
10 Correct 4 ms 15964 KB Output is correct
11 Correct 4 ms 15960 KB Output is correct
12 Correct 3 ms 15964 KB Output is correct
13 Correct 4 ms 16140 KB Output is correct
14 Correct 3 ms 16108 KB Output is correct
15 Correct 4 ms 15964 KB Output is correct
16 Correct 3 ms 15964 KB Output is correct
17 Correct 3 ms 15964 KB Output is correct
18 Correct 3 ms 16136 KB Output is correct
19 Correct 3 ms 15960 KB Output is correct
20 Correct 4 ms 15960 KB Output is correct
21 Correct 4 ms 15964 KB Output is correct
22 Correct 5 ms 22364 KB Output is correct
23 Correct 5 ms 22240 KB Output is correct
24 Correct 5 ms 22364 KB Output is correct
25 Correct 5 ms 22364 KB Output is correct
26 Correct 5 ms 20060 KB Output is correct
27 Correct 5 ms 22364 KB Output is correct
28 Correct 4 ms 18012 KB Output is correct
29 Correct 4 ms 20316 KB Output is correct
30 Correct 4 ms 20316 KB Output is correct
31 Correct 5 ms 22364 KB Output is correct
32 Correct 5 ms 22364 KB Output is correct
33 Correct 5 ms 22364 KB Output is correct
34 Correct 5 ms 22364 KB Output is correct
35 Correct 5 ms 22364 KB Output is correct
36 Correct 5 ms 22364 KB Output is correct
37 Correct 3 ms 22108 KB Output is correct
38 Correct 5 ms 20056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15964 KB Output is correct
2 Correct 3 ms 15964 KB Output is correct
3 Correct 3 ms 16116 KB Output is correct
4 Correct 4 ms 15964 KB Output is correct
5 Correct 3 ms 15964 KB Output is correct
6 Correct 3 ms 16132 KB Output is correct
7 Correct 3 ms 15964 KB Output is correct
8 Correct 3 ms 15964 KB Output is correct
9 Correct 3 ms 15964 KB Output is correct
10 Correct 4 ms 15964 KB Output is correct
11 Correct 4 ms 15960 KB Output is correct
12 Correct 3 ms 15964 KB Output is correct
13 Correct 4 ms 16140 KB Output is correct
14 Correct 3 ms 16108 KB Output is correct
15 Correct 4 ms 15964 KB Output is correct
16 Correct 3 ms 15964 KB Output is correct
17 Correct 3 ms 15964 KB Output is correct
18 Correct 3 ms 16136 KB Output is correct
19 Correct 105 ms 26512 KB Output is correct
20 Correct 107 ms 26448 KB Output is correct
21 Correct 108 ms 26520 KB Output is correct
22 Correct 101 ms 26688 KB Output is correct
23 Correct 59 ms 26800 KB Output is correct
24 Correct 52 ms 26928 KB Output is correct
25 Correct 106 ms 29344 KB Output is correct
26 Correct 85 ms 32012 KB Output is correct
27 Correct 164 ms 35408 KB Output is correct
28 Correct 197 ms 46400 KB Output is correct
29 Correct 198 ms 45396 KB Output is correct
30 Correct 160 ms 35416 KB Output is correct
31 Correct 175 ms 35408 KB Output is correct
32 Correct 192 ms 35980 KB Output is correct
33 Correct 169 ms 33808 KB Output is correct
34 Correct 145 ms 34640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15964 KB Output is correct
2 Correct 3 ms 15964 KB Output is correct
3 Correct 3 ms 16116 KB Output is correct
4 Correct 4 ms 15964 KB Output is correct
5 Correct 3 ms 15964 KB Output is correct
6 Correct 3 ms 16132 KB Output is correct
7 Correct 3 ms 15964 KB Output is correct
8 Correct 3 ms 15964 KB Output is correct
9 Correct 3 ms 15964 KB Output is correct
10 Correct 4 ms 15964 KB Output is correct
11 Correct 4 ms 15960 KB Output is correct
12 Correct 3 ms 15964 KB Output is correct
13 Correct 4 ms 16140 KB Output is correct
14 Correct 3 ms 16108 KB Output is correct
15 Correct 4 ms 15964 KB Output is correct
16 Correct 3 ms 15964 KB Output is correct
17 Correct 3 ms 15964 KB Output is correct
18 Correct 3 ms 16136 KB Output is correct
19 Correct 3 ms 15960 KB Output is correct
20 Correct 4 ms 15960 KB Output is correct
21 Correct 4 ms 15964 KB Output is correct
22 Correct 5 ms 22364 KB Output is correct
23 Correct 5 ms 22240 KB Output is correct
24 Correct 5 ms 22364 KB Output is correct
25 Correct 5 ms 22364 KB Output is correct
26 Correct 5 ms 20060 KB Output is correct
27 Correct 5 ms 22364 KB Output is correct
28 Correct 4 ms 18012 KB Output is correct
29 Correct 4 ms 20316 KB Output is correct
30 Correct 4 ms 20316 KB Output is correct
31 Correct 5 ms 22364 KB Output is correct
32 Correct 5 ms 22364 KB Output is correct
33 Correct 5 ms 22364 KB Output is correct
34 Correct 5 ms 22364 KB Output is correct
35 Correct 5 ms 22364 KB Output is correct
36 Correct 5 ms 22364 KB Output is correct
37 Correct 3 ms 22108 KB Output is correct
38 Correct 5 ms 20056 KB Output is correct
39 Correct 105 ms 26512 KB Output is correct
40 Correct 107 ms 26448 KB Output is correct
41 Correct 108 ms 26520 KB Output is correct
42 Correct 101 ms 26688 KB Output is correct
43 Correct 59 ms 26800 KB Output is correct
44 Correct 52 ms 26928 KB Output is correct
45 Correct 106 ms 29344 KB Output is correct
46 Correct 85 ms 32012 KB Output is correct
47 Correct 164 ms 35408 KB Output is correct
48 Correct 197 ms 46400 KB Output is correct
49 Correct 198 ms 45396 KB Output is correct
50 Correct 160 ms 35416 KB Output is correct
51 Correct 175 ms 35408 KB Output is correct
52 Correct 192 ms 35980 KB Output is correct
53 Correct 169 ms 33808 KB Output is correct
54 Correct 145 ms 34640 KB Output is correct
55 Correct 10 ms 16984 KB Output is correct
56 Correct 11 ms 16732 KB Output is correct
57 Correct 66 ms 26632 KB Output is correct
58 Correct 33 ms 26684 KB Output is correct
59 Correct 83 ms 34128 KB Output is correct
60 Correct 392 ms 52048 KB Output is correct
61 Correct 194 ms 35716 KB Output is correct
62 Correct 194 ms 41812 KB Output is correct
63 Correct 262 ms 41864 KB Output is correct
64 Correct 413 ms 39504 KB Output is correct
65 Correct 203 ms 35980 KB Output is correct
66 Correct 304 ms 49548 KB Output is correct
67 Correct 108 ms 42548 KB Output is correct
68 Correct 248 ms 42920 KB Output is correct
69 Correct 226 ms 42836 KB Output is correct
70 Correct 243 ms 41808 KB Output is correct