Submission #789107

# Submission time Handle Problem Language Result Execution time Memory
789107 2023-07-21T04:53:22 Z diobrando97 Race (IOI11_race) C++17
100 / 100
494 ms 84428 KB
#include <bits/stdc++.h>
#pragma GCC optimize(3)
#define ll long long
#define pii pair<int, int>
#define pll pair<long long, long long>
#define F first
#define S second
#define endl '\n'
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 2e5 + 5;

double startTime = clock();
double getCurrentTime() {
    return (double)(clock() - startTime) / CLOCKS_PER_SEC;
}

int n, k;
int ans = inf;
int tot = 0, cur = 0;
array<int, 3> dis[N];
multiset<int> st[N*5];

struct CD{
    int n;
    vector<vector<pii>> g;
    vector<int> par, sz;
    vector<bool> vis;
    
    CD(){}
    CD(int n){
        init(n);
    }

    void init(int n){
        this->n = n;
        g.assign(n+1, {});
        par.assign(n+1, 0);
        sz.assign(n+1, 0);
        vis.assign(n+1, 0);
    }
    
    void addEdge(int x, int y, int w){
        g[x].push_back({y, w});
        g[y].push_back({x, w});
    }
    
    void dfs(int i, int f){
        sz[i] = 1;
        for(auto [j, w] : g[i]){
            if(j == f || vis[j]) continue;
            dfs(j, i);
            sz[i] += sz[j];
        }
    }

    int getCentroid(int i, int f, int n){
        for(auto [j, w] : g[i]){
            if(j == f || vis[j]) continue;
            if(sz[j] * 2 > n) return getCentroid(j, i, n);
        }
        return i;
    }
    
    void dfs2(int i, int f, int w, int len, int mark){
        if(w > k) return;
        dis[++tot] = {w, len, mark};
        for(auto [j, wj] : g[i]){
            if(j == f || vis[j]) continue;
            dfs2(j, i, w+wj, len+1, mark);
        }
        
    }

    void work(int i, int f = 0){
        dfs(i, f);
        int cen = getCentroid(i, f, sz[i]);
        vis[cen] = 1;
        par[cen] = f;

        tot = cur = 0;
        for(auto [j, w] : g[cen]){
            if(j == f || vis[j]) continue;
            dfs2(j, cen, w, 1, ++cur);
        }

        st[0] = {0};
        for(int j=1; j<=tot; j++){
            st[dis[j][0]].insert(dis[j][1]);
        }
        int x = 1, y = 1;
        for(int j=1; j<=cur; j++){
            while(x <= tot && dis[x][2] == j){
                st[dis[x][0]].erase(st[dis[x][0]].find(dis[x][1]));
                x++;
            }
            while(y <= tot && dis[y][2] == j){
                int tar = k - dis[y][0];
                if(tar >= 0 && tar <= k && !st[tar].empty()){
                    ans = min(ans, dis[y][1] + *st[tar].begin());
                }
                y++;
            }
        }
        
        for(auto [j, w] : g[cen]){
            if(vis[j]) continue;
            work(j, cen);
        }
    }
};

int best_path(int N, int K, int h[][2], int l[]) {
    n = N; k = K;
    CD cent(n);
    for (int i = 0; i < n - 1; i++) {
        int x = h[i][0] + 1, y = h[i][1] + 1, w = l[i];
        cent.addEdge(x, y, w);
    }
    memset(dis, 0, sizeof dis);
    for(int i=1; i<=k; i++) st[i].clear();

    ans = inf;
    cent.work(1, 0);
    return (ans == inf ? -1 : ans);
    // cout << (ans == inf ? -1 : ans) << endl;
}

// void solve(){
//     cin >> n >> k;
//     CD cent(n);
//     for(int i=1; i<n; i++){
//         int x, y, w; cin >> x >> y >> w;
//         x++; y++;
//         cent.addEdge(x, y, w);
//     }
    
//     memset(dis, 0, sizeof dis);
//     for(int i=1; i<=n; i++) st[i].clear();

//     cent.work(1, 0);
//     cout << (ans == inf ? -1 : ans) << endl;
// }


// int main(){
//     ios::sync_with_stdio(0); cin.tie(0);
//     freopen("input.txt", "r", stdin);
//     freopen("output.txt", "w", stdout);

//     int t = 1;
//     //cin >> t;
//     while(t--)
//     solve();

//     return 0;
// }
# Verdict Execution time Memory Grader output
1 Correct 22 ms 49572 KB Output is correct
2 Correct 21 ms 49584 KB Output is correct
3 Correct 28 ms 49564 KB Output is correct
4 Correct 22 ms 49532 KB Output is correct
5 Correct 20 ms 49572 KB Output is correct
6 Correct 25 ms 49568 KB Output is correct
7 Correct 21 ms 49620 KB Output is correct
8 Correct 21 ms 49540 KB Output is correct
9 Correct 22 ms 49620 KB Output is correct
10 Correct 25 ms 49524 KB Output is correct
11 Correct 20 ms 49536 KB Output is correct
12 Correct 21 ms 49544 KB Output is correct
13 Correct 22 ms 49556 KB Output is correct
14 Correct 22 ms 49588 KB Output is correct
15 Correct 21 ms 49528 KB Output is correct
16 Correct 20 ms 49540 KB Output is correct
17 Correct 24 ms 49592 KB Output is correct
18 Correct 26 ms 49588 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 49572 KB Output is correct
2 Correct 21 ms 49584 KB Output is correct
3 Correct 28 ms 49564 KB Output is correct
4 Correct 22 ms 49532 KB Output is correct
5 Correct 20 ms 49572 KB Output is correct
6 Correct 25 ms 49568 KB Output is correct
7 Correct 21 ms 49620 KB Output is correct
8 Correct 21 ms 49540 KB Output is correct
9 Correct 22 ms 49620 KB Output is correct
10 Correct 25 ms 49524 KB Output is correct
11 Correct 20 ms 49536 KB Output is correct
12 Correct 21 ms 49544 KB Output is correct
13 Correct 22 ms 49556 KB Output is correct
14 Correct 22 ms 49588 KB Output is correct
15 Correct 21 ms 49528 KB Output is correct
16 Correct 20 ms 49540 KB Output is correct
17 Correct 24 ms 49592 KB Output is correct
18 Correct 26 ms 49588 KB Output is correct
19 Correct 21 ms 49560 KB Output is correct
20 Correct 20 ms 49592 KB Output is correct
21 Correct 24 ms 49660 KB Output is correct
22 Correct 27 ms 49620 KB Output is correct
23 Correct 32 ms 49620 KB Output is correct
24 Correct 30 ms 49728 KB Output is correct
25 Correct 29 ms 49720 KB Output is correct
26 Correct 27 ms 49620 KB Output is correct
27 Correct 29 ms 49652 KB Output is correct
28 Correct 26 ms 49720 KB Output is correct
29 Correct 24 ms 49760 KB Output is correct
30 Correct 29 ms 49764 KB Output is correct
31 Correct 25 ms 49648 KB Output is correct
32 Correct 25 ms 49688 KB Output is correct
33 Correct 26 ms 49748 KB Output is correct
34 Correct 29 ms 49712 KB Output is correct
35 Correct 32 ms 49620 KB Output is correct
36 Correct 26 ms 49620 KB Output is correct
37 Correct 26 ms 49680 KB Output is correct
38 Correct 25 ms 49756 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 49572 KB Output is correct
2 Correct 21 ms 49584 KB Output is correct
3 Correct 28 ms 49564 KB Output is correct
4 Correct 22 ms 49532 KB Output is correct
5 Correct 20 ms 49572 KB Output is correct
6 Correct 25 ms 49568 KB Output is correct
7 Correct 21 ms 49620 KB Output is correct
8 Correct 21 ms 49540 KB Output is correct
9 Correct 22 ms 49620 KB Output is correct
10 Correct 25 ms 49524 KB Output is correct
11 Correct 20 ms 49536 KB Output is correct
12 Correct 21 ms 49544 KB Output is correct
13 Correct 22 ms 49556 KB Output is correct
14 Correct 22 ms 49588 KB Output is correct
15 Correct 21 ms 49528 KB Output is correct
16 Correct 20 ms 49540 KB Output is correct
17 Correct 24 ms 49592 KB Output is correct
18 Correct 26 ms 49588 KB Output is correct
19 Correct 172 ms 59528 KB Output is correct
20 Correct 152 ms 59240 KB Output is correct
21 Correct 177 ms 60572 KB Output is correct
22 Correct 183 ms 62892 KB Output is correct
23 Correct 78 ms 59248 KB Output is correct
24 Correct 88 ms 59116 KB Output is correct
25 Correct 155 ms 60524 KB Output is correct
26 Correct 189 ms 67108 KB Output is correct
27 Correct 156 ms 68972 KB Output is correct
28 Correct 250 ms 75572 KB Output is correct
29 Correct 209 ms 75036 KB Output is correct
30 Correct 160 ms 68988 KB Output is correct
31 Correct 162 ms 68876 KB Output is correct
32 Correct 189 ms 68872 KB Output is correct
33 Correct 230 ms 67512 KB Output is correct
34 Correct 152 ms 68316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 49572 KB Output is correct
2 Correct 21 ms 49584 KB Output is correct
3 Correct 28 ms 49564 KB Output is correct
4 Correct 22 ms 49532 KB Output is correct
5 Correct 20 ms 49572 KB Output is correct
6 Correct 25 ms 49568 KB Output is correct
7 Correct 21 ms 49620 KB Output is correct
8 Correct 21 ms 49540 KB Output is correct
9 Correct 22 ms 49620 KB Output is correct
10 Correct 25 ms 49524 KB Output is correct
11 Correct 20 ms 49536 KB Output is correct
12 Correct 21 ms 49544 KB Output is correct
13 Correct 22 ms 49556 KB Output is correct
14 Correct 22 ms 49588 KB Output is correct
15 Correct 21 ms 49528 KB Output is correct
16 Correct 20 ms 49540 KB Output is correct
17 Correct 24 ms 49592 KB Output is correct
18 Correct 26 ms 49588 KB Output is correct
19 Correct 21 ms 49560 KB Output is correct
20 Correct 20 ms 49592 KB Output is correct
21 Correct 24 ms 49660 KB Output is correct
22 Correct 27 ms 49620 KB Output is correct
23 Correct 32 ms 49620 KB Output is correct
24 Correct 30 ms 49728 KB Output is correct
25 Correct 29 ms 49720 KB Output is correct
26 Correct 27 ms 49620 KB Output is correct
27 Correct 29 ms 49652 KB Output is correct
28 Correct 26 ms 49720 KB Output is correct
29 Correct 24 ms 49760 KB Output is correct
30 Correct 29 ms 49764 KB Output is correct
31 Correct 25 ms 49648 KB Output is correct
32 Correct 25 ms 49688 KB Output is correct
33 Correct 26 ms 49748 KB Output is correct
34 Correct 29 ms 49712 KB Output is correct
35 Correct 32 ms 49620 KB Output is correct
36 Correct 26 ms 49620 KB Output is correct
37 Correct 26 ms 49680 KB Output is correct
38 Correct 25 ms 49756 KB Output is correct
39 Correct 172 ms 59528 KB Output is correct
40 Correct 152 ms 59240 KB Output is correct
41 Correct 177 ms 60572 KB Output is correct
42 Correct 183 ms 62892 KB Output is correct
43 Correct 78 ms 59248 KB Output is correct
44 Correct 88 ms 59116 KB Output is correct
45 Correct 155 ms 60524 KB Output is correct
46 Correct 189 ms 67108 KB Output is correct
47 Correct 156 ms 68972 KB Output is correct
48 Correct 250 ms 75572 KB Output is correct
49 Correct 209 ms 75036 KB Output is correct
50 Correct 160 ms 68988 KB Output is correct
51 Correct 162 ms 68876 KB Output is correct
52 Correct 189 ms 68872 KB Output is correct
53 Correct 230 ms 67512 KB Output is correct
54 Correct 152 ms 68316 KB Output is correct
55 Correct 32 ms 50700 KB Output is correct
56 Correct 35 ms 51004 KB Output is correct
57 Correct 225 ms 63892 KB Output is correct
58 Correct 69 ms 63532 KB Output is correct
59 Correct 136 ms 67112 KB Output is correct
60 Correct 475 ms 84428 KB Output is correct
61 Correct 176 ms 70816 KB Output is correct
62 Correct 232 ms 78044 KB Output is correct
63 Correct 298 ms 78068 KB Output is correct
64 Correct 494 ms 78448 KB Output is correct
65 Correct 165 ms 69616 KB Output is correct
66 Correct 390 ms 73948 KB Output is correct
67 Correct 171 ms 78768 KB Output is correct
68 Correct 307 ms 73628 KB Output is correct
69 Correct 289 ms 73720 KB Output is correct
70 Correct 293 ms 72644 KB Output is correct