답안 #844731

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
844731 2023-09-05T18:44:35 Z Mr_Husanboy 경주 (Race) (IOI11_race) C++17
100 / 100
1653 ms 42596 KB
#include "race.h"
#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define ff first
#define ss second

int d;

struct centroid{
    vector<vector<pair<int,int>>> g;
    vector<int> par, sz;
    vector<ll> dis;
    vector<bool> vis;
    multiset<pair<ll, int>> st;
    int n;

    centroid (int _n){
        init(_n);
    }

    void init(int _n){
        n = _n;
        g.assign(n, vector<pair<int,int>>());
        par.assign(n, 0);
        sz.assign(n, 0);
        vis.assign(n, false);
        dis.assign(n, 0ll);
    }

    void edge(int u, int v, int w){
        g[u].push_back({v, w}); 
        g[v].push_back({u, w});
    }

    int calc_size(int i, int p = -1){
        if(vis[i]) return 0;
        sz[i] = 1;
        for(auto [u, w] : g[i]){
            if(u == p) continue;
            sz[i] += calc_size(u, i);
        }
        //cout << i << ": " << sz[i] << ' ' << endl;
        return sz[i];
    }

    int find_centroid(int i, int p, int len){
        //cout << "i " << i << ' ';
        for(auto [u, w] : g[i]){
            if(vis[u] || u == p) continue;
            if(sz[u] > len/2) return find_centroid(u, i, len);
        }
        return i;
    }

    void decompose(int i = 0, int p = -1){
        int c = find_centroid(i, -1, calc_size(i));
        vis[c] = true;
        par[c] = p;
        //cout << c << endl;


        dfs(c, -1, 0, 0, 1);
        for(auto [u, w] : g[c]){
            if(vis[u]) continue;
            dfs(u, c, w, 1, -1);
            calc(u, c, w, 1);
            dfs(u, c, w, 1, 1);
        }
        dfs(c, -1, 0, 0, -1);
        
        for(auto [u, w] : g[c]){
            if(vis[u]) continue;
            decompose(u, c);
        }
    }

    int res = 1e9;
    void dfs(int i, int p, ll cur, int len, int flag){
        if(flag > 0){
          //cout << flag << endl;
          //cout << i << ' ' << cur << ' ' << len << endl;
            st.insert({cur, len});
        }else{
          //cout << flag << endl;
          //cout << i << ' ' << cur << ' ' << len << endl;
            st.erase(st.find({cur, len}));
        }
        for(auto [u, w] : g[i]){
            if(vis[u] || u == p) continue;
            dfs(u, i, cur + w, len + 1, flag);
        }
    }

    void calc(int i, int p, ll cur, int len){
        if(cur > d) return;
        auto it = st.lower_bound({d - cur, -1});
        if(it != st.end() && it->ff == d - cur){
           res = min(res, it->ss + len);
        }
        for(auto [u, w] : g[i]){
          if(vis[u] || u == p) continue;
          calc(u, i, cur + w, len + 1);
        }
    }
};


int best_path(int n, int k, int way[][2], int len[])
{
    
    d = k;
    centroid g(n);
    for(int i = 0; i < n - 1; i ++){
        g.edge(way[i][0], way[i][1], len[i]);
    }

    g.decompose();
    return g.res == 1e9 ? -1 : g.res;


}

# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2496 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2496 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2392 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2496 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2496 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2392 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 3 ms 2396 KB Output is correct
22 Correct 3 ms 2396 KB Output is correct
23 Correct 3 ms 2652 KB Output is correct
24 Correct 3 ms 2396 KB Output is correct
25 Correct 3 ms 2396 KB Output is correct
26 Correct 3 ms 2648 KB Output is correct
27 Correct 2 ms 2396 KB Output is correct
28 Correct 3 ms 2396 KB Output is correct
29 Correct 3 ms 2396 KB Output is correct
30 Correct 3 ms 2396 KB Output is correct
31 Correct 3 ms 2396 KB Output is correct
32 Correct 3 ms 2500 KB Output is correct
33 Correct 3 ms 2396 KB Output is correct
34 Correct 4 ms 2396 KB Output is correct
35 Correct 3 ms 2396 KB Output is correct
36 Correct 3 ms 2396 KB Output is correct
37 Correct 3 ms 2396 KB Output is correct
38 Correct 3 ms 2396 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2496 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2496 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2392 KB Output is correct
19 Correct 676 ms 19896 KB Output is correct
20 Correct 640 ms 19896 KB Output is correct
21 Correct 614 ms 20052 KB Output is correct
22 Correct 528 ms 20012 KB Output is correct
23 Correct 710 ms 20244 KB Output is correct
24 Correct 342 ms 20124 KB Output is correct
25 Correct 607 ms 21332 KB Output is correct
26 Correct 407 ms 23120 KB Output is correct
27 Correct 657 ms 35640 KB Output is correct
28 Correct 1226 ms 42596 KB Output is correct
29 Correct 1286 ms 41824 KB Output is correct
30 Correct 659 ms 35428 KB Output is correct
31 Correct 613 ms 35264 KB Output is correct
32 Correct 704 ms 35488 KB Output is correct
33 Correct 1102 ms 34232 KB Output is correct
34 Correct 1289 ms 34968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2652 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2496 KB Output is correct
7 Correct 1 ms 2396 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 1 ms 2396 KB Output is correct
11 Correct 1 ms 2652 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2496 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2392 KB Output is correct
17 Correct 1 ms 2396 KB Output is correct
18 Correct 1 ms 2392 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 3 ms 2396 KB Output is correct
22 Correct 3 ms 2396 KB Output is correct
23 Correct 3 ms 2652 KB Output is correct
24 Correct 3 ms 2396 KB Output is correct
25 Correct 3 ms 2396 KB Output is correct
26 Correct 3 ms 2648 KB Output is correct
27 Correct 2 ms 2396 KB Output is correct
28 Correct 3 ms 2396 KB Output is correct
29 Correct 3 ms 2396 KB Output is correct
30 Correct 3 ms 2396 KB Output is correct
31 Correct 3 ms 2396 KB Output is correct
32 Correct 3 ms 2500 KB Output is correct
33 Correct 3 ms 2396 KB Output is correct
34 Correct 4 ms 2396 KB Output is correct
35 Correct 3 ms 2396 KB Output is correct
36 Correct 3 ms 2396 KB Output is correct
37 Correct 3 ms 2396 KB Output is correct
38 Correct 3 ms 2396 KB Output is correct
39 Correct 676 ms 19896 KB Output is correct
40 Correct 640 ms 19896 KB Output is correct
41 Correct 614 ms 20052 KB Output is correct
42 Correct 528 ms 20012 KB Output is correct
43 Correct 710 ms 20244 KB Output is correct
44 Correct 342 ms 20124 KB Output is correct
45 Correct 607 ms 21332 KB Output is correct
46 Correct 407 ms 23120 KB Output is correct
47 Correct 657 ms 35640 KB Output is correct
48 Correct 1226 ms 42596 KB Output is correct
49 Correct 1286 ms 41824 KB Output is correct
50 Correct 659 ms 35428 KB Output is correct
51 Correct 613 ms 35264 KB Output is correct
52 Correct 704 ms 35488 KB Output is correct
53 Correct 1102 ms 34232 KB Output is correct
54 Correct 1289 ms 34968 KB Output is correct
55 Correct 36 ms 4048 KB Output is correct
56 Correct 39 ms 3932 KB Output is correct
57 Correct 586 ms 20136 KB Output is correct
58 Correct 117 ms 19692 KB Output is correct
59 Correct 511 ms 23360 KB Output is correct
60 Correct 1262 ms 41856 KB Output is correct
61 Correct 640 ms 35588 KB Output is correct
62 Correct 611 ms 35664 KB Output is correct
63 Correct 745 ms 35476 KB Output is correct
64 Correct 1653 ms 35872 KB Output is correct
65 Correct 1310 ms 36440 KB Output is correct
66 Correct 1306 ms 40664 KB Output is correct
67 Correct 336 ms 36292 KB Output is correct
68 Correct 739 ms 36012 KB Output is correct
69 Correct 714 ms 36240 KB Output is correct
70 Correct 643 ms 34656 KB Output is correct