답안 #961104

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
961104 2024-04-11T13:53:43 Z okkoo 경주 (Race) (IOI11_race) C++17
100 / 100
291 ms 35652 KB
#include <iostream>
#include <vector>
#include <queue>
#include <string.h>
using namespace std;
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

const int mxn = 2e5;
vector<vector<pair<int, int> > > adj(mxn+1, vector<pair<int, int> >());
int K;
int ans = 1e9;
bool processed[mxn+1];
int sz[mxn+1];
vector<int> lengths(mxn*5+1, 1e9);

int getSize(int node, int p){
    sz[node] = 1;
    for(pair<int, int> tmp: adj[node]){
        int to = tmp.first;
        if(!processed[to] && to!=p) sz[node] += getSize(to, node);
    }
    return sz[node];
}

int getCentroid(int node, int p, int mxSize){
    for(pair<int, int> tmp: adj[node]){
        int to = tmp.first; 
        if(!processed[to] && to!=p && sz[to] > mxSize) return getCentroid(to, node, mxSize);
    }
    return node;
}

queue<pair<int, int> > q;
queue<int> L;

void dfs(int node, int p, int edgeLen, int edgeCnt){
    if(K < edgeLen) return;
    ans = min(ans, lengths[K-edgeLen] + edgeCnt);
    q.push({edgeLen, edgeCnt});
    L.push(edgeLen);
    for(pair<int, int> tmp: adj[node]){
        int to = tmp.first;
        if(!processed[to] && to!=p) dfs(to, node, edgeLen + tmp.second, edgeCnt+1);
    }
    if(!p){
        while(!q.empty()){
            lengths[q.front().first] = min(lengths[q.front().first], q.front().second);
            q.pop();
        }
    }
}

void centroidDecomposition(int node){
    int centroid = getCentroid(node, 0, getSize(node, 0)/2);
    processed[centroid] = 1;
    lengths[0] = 0;
    for(pair<int, int> tmp: adj[centroid]){
        int to = tmp.first;
        if(!processed[to]) dfs(to, 0, tmp.second, 1);
    }
    while(!L.empty()){
        lengths[L.front()] = 1e9;
        L.pop();
    }
    for(pair<int, int> tmp: adj[centroid]){
        int to = tmp.first;
        if(!processed[to]) centroidDecomposition(to);
    }
}

int best_path(int n, int k, int h[][2], int l[]){
    fastIO;
    K = k;
    for(int i=0; i<n-1; i++){
        h[i][0]++; h[i][1]++;
        adj[h[i][0]].push_back({h[i][1], l[i]});
        adj[h[i][1]].push_back({h[i][0], l[i]});
    }

    centroidDecomposition(1);

    return (ans == 1e9 ? -1 : ans);
}

// int main(){
//     int n, k;
//     cin >> n >> k;
//     int h[n-1][2], l[n-1];
//     for(int i=0; i<n-1; i++){
//         cin >> h[i][0] >> h[i][1];
//     }
//     for(int i=0; i<n-1; i++){
//         cin >> l[i];
//     }
//     cout << best_path(n, k, h, l) << '\n';
// }
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 13148 KB Output is correct
2 Correct 3 ms 13148 KB Output is correct
3 Correct 4 ms 13080 KB Output is correct
4 Correct 7 ms 13148 KB Output is correct
5 Correct 5 ms 13148 KB Output is correct
6 Correct 5 ms 13144 KB Output is correct
7 Correct 7 ms 13148 KB Output is correct
8 Correct 4 ms 13148 KB Output is correct
9 Correct 5 ms 13144 KB Output is correct
10 Correct 4 ms 13148 KB Output is correct
11 Correct 6 ms 13148 KB Output is correct
12 Correct 4 ms 13148 KB Output is correct
13 Correct 4 ms 13148 KB Output is correct
14 Correct 7 ms 13148 KB Output is correct
15 Correct 3 ms 13144 KB Output is correct
16 Correct 4 ms 13144 KB Output is correct
17 Correct 5 ms 13148 KB Output is correct
18 Correct 4 ms 13148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 13148 KB Output is correct
2 Correct 3 ms 13148 KB Output is correct
3 Correct 4 ms 13080 KB Output is correct
4 Correct 7 ms 13148 KB Output is correct
5 Correct 5 ms 13148 KB Output is correct
6 Correct 5 ms 13144 KB Output is correct
7 Correct 7 ms 13148 KB Output is correct
8 Correct 4 ms 13148 KB Output is correct
9 Correct 5 ms 13144 KB Output is correct
10 Correct 4 ms 13148 KB Output is correct
11 Correct 6 ms 13148 KB Output is correct
12 Correct 4 ms 13148 KB Output is correct
13 Correct 4 ms 13148 KB Output is correct
14 Correct 7 ms 13148 KB Output is correct
15 Correct 3 ms 13144 KB Output is correct
16 Correct 4 ms 13144 KB Output is correct
17 Correct 5 ms 13148 KB Output is correct
18 Correct 4 ms 13148 KB Output is correct
19 Correct 4 ms 13148 KB Output is correct
20 Correct 4 ms 13148 KB Output is correct
21 Correct 4 ms 13148 KB Output is correct
22 Correct 4 ms 13148 KB Output is correct
23 Correct 4 ms 13148 KB Output is correct
24 Correct 4 ms 13148 KB Output is correct
25 Correct 4 ms 13148 KB Output is correct
26 Correct 4 ms 13148 KB Output is correct
27 Correct 4 ms 13148 KB Output is correct
28 Correct 4 ms 13148 KB Output is correct
29 Correct 4 ms 13144 KB Output is correct
30 Correct 5 ms 13148 KB Output is correct
31 Correct 4 ms 13212 KB Output is correct
32 Correct 4 ms 13148 KB Output is correct
33 Correct 4 ms 13148 KB Output is correct
34 Correct 5 ms 13148 KB Output is correct
35 Correct 4 ms 13148 KB Output is correct
36 Correct 4 ms 13016 KB Output is correct
37 Correct 5 ms 13144 KB Output is correct
38 Correct 6 ms 13144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 13148 KB Output is correct
2 Correct 3 ms 13148 KB Output is correct
3 Correct 4 ms 13080 KB Output is correct
4 Correct 7 ms 13148 KB Output is correct
5 Correct 5 ms 13148 KB Output is correct
6 Correct 5 ms 13144 KB Output is correct
7 Correct 7 ms 13148 KB Output is correct
8 Correct 4 ms 13148 KB Output is correct
9 Correct 5 ms 13144 KB Output is correct
10 Correct 4 ms 13148 KB Output is correct
11 Correct 6 ms 13148 KB Output is correct
12 Correct 4 ms 13148 KB Output is correct
13 Correct 4 ms 13148 KB Output is correct
14 Correct 7 ms 13148 KB Output is correct
15 Correct 3 ms 13144 KB Output is correct
16 Correct 4 ms 13144 KB Output is correct
17 Correct 5 ms 13148 KB Output is correct
18 Correct 4 ms 13148 KB Output is correct
19 Correct 81 ms 19056 KB Output is correct
20 Correct 77 ms 19212 KB Output is correct
21 Correct 81 ms 19232 KB Output is correct
22 Correct 76 ms 19676 KB Output is correct
23 Correct 50 ms 19284 KB Output is correct
24 Correct 38 ms 19336 KB Output is correct
25 Correct 85 ms 21856 KB Output is correct
26 Correct 68 ms 25696 KB Output is correct
27 Correct 121 ms 23500 KB Output is correct
28 Correct 162 ms 34568 KB Output is correct
29 Correct 172 ms 33624 KB Output is correct
30 Correct 106 ms 23268 KB Output is correct
31 Correct 100 ms 23512 KB Output is correct
32 Correct 126 ms 23380 KB Output is correct
33 Correct 128 ms 22344 KB Output is correct
34 Correct 120 ms 22284 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 13148 KB Output is correct
2 Correct 3 ms 13148 KB Output is correct
3 Correct 4 ms 13080 KB Output is correct
4 Correct 7 ms 13148 KB Output is correct
5 Correct 5 ms 13148 KB Output is correct
6 Correct 5 ms 13144 KB Output is correct
7 Correct 7 ms 13148 KB Output is correct
8 Correct 4 ms 13148 KB Output is correct
9 Correct 5 ms 13144 KB Output is correct
10 Correct 4 ms 13148 KB Output is correct
11 Correct 6 ms 13148 KB Output is correct
12 Correct 4 ms 13148 KB Output is correct
13 Correct 4 ms 13148 KB Output is correct
14 Correct 7 ms 13148 KB Output is correct
15 Correct 3 ms 13144 KB Output is correct
16 Correct 4 ms 13144 KB Output is correct
17 Correct 5 ms 13148 KB Output is correct
18 Correct 4 ms 13148 KB Output is correct
19 Correct 4 ms 13148 KB Output is correct
20 Correct 4 ms 13148 KB Output is correct
21 Correct 4 ms 13148 KB Output is correct
22 Correct 4 ms 13148 KB Output is correct
23 Correct 4 ms 13148 KB Output is correct
24 Correct 4 ms 13148 KB Output is correct
25 Correct 4 ms 13148 KB Output is correct
26 Correct 4 ms 13148 KB Output is correct
27 Correct 4 ms 13148 KB Output is correct
28 Correct 4 ms 13148 KB Output is correct
29 Correct 4 ms 13144 KB Output is correct
30 Correct 5 ms 13148 KB Output is correct
31 Correct 4 ms 13212 KB Output is correct
32 Correct 4 ms 13148 KB Output is correct
33 Correct 4 ms 13148 KB Output is correct
34 Correct 5 ms 13148 KB Output is correct
35 Correct 4 ms 13148 KB Output is correct
36 Correct 4 ms 13016 KB Output is correct
37 Correct 5 ms 13144 KB Output is correct
38 Correct 6 ms 13144 KB Output is correct
39 Correct 81 ms 19056 KB Output is correct
40 Correct 77 ms 19212 KB Output is correct
41 Correct 81 ms 19232 KB Output is correct
42 Correct 76 ms 19676 KB Output is correct
43 Correct 50 ms 19284 KB Output is correct
44 Correct 38 ms 19336 KB Output is correct
45 Correct 85 ms 21856 KB Output is correct
46 Correct 68 ms 25696 KB Output is correct
47 Correct 121 ms 23500 KB Output is correct
48 Correct 162 ms 34568 KB Output is correct
49 Correct 172 ms 33624 KB Output is correct
50 Correct 106 ms 23268 KB Output is correct
51 Correct 100 ms 23512 KB Output is correct
52 Correct 126 ms 23380 KB Output is correct
53 Correct 128 ms 22344 KB Output is correct
54 Correct 120 ms 22284 KB Output is correct
55 Correct 12 ms 13656 KB Output is correct
56 Correct 10 ms 13916 KB Output is correct
57 Correct 57 ms 20008 KB Output is correct
58 Correct 31 ms 19584 KB Output is correct
59 Correct 72 ms 25716 KB Output is correct
60 Correct 291 ms 35652 KB Output is correct
61 Correct 116 ms 23632 KB Output is correct
62 Correct 126 ms 24096 KB Output is correct
63 Correct 157 ms 24140 KB Output is correct
64 Correct 276 ms 24636 KB Output is correct
65 Correct 117 ms 23304 KB Output is correct
66 Correct 280 ms 31632 KB Output is correct
67 Correct 71 ms 24316 KB Output is correct
68 Correct 152 ms 24068 KB Output is correct
69 Correct 147 ms 23892 KB Output is correct
70 Correct 188 ms 23688 KB Output is correct