Submission #501028

# Submission time Handle Problem Language Result Execution time Memory
501028 2022-01-02T08:35:15 Z Jomnoi Race (IOI11_race) C++17
100 / 100
935 ms 42520 KB
#include <bits/stdc++.h>
#include "race.h"
#define DEBUG 0
using namespace std;

const int N = 2e5 + 10;
const int INF = 1e9 + 7;

vector <pair <int, int>> adj[N];
int sz[N];
bool visited[N];
map <long long, int> mp;
int ans = INF;

int find_size(int u, int p) {
    sz[u] = 1;
    for(auto [v, w] : adj[u]) {
        if(v == p || visited[v]) {
            continue;
        }

        sz[u] += find_size(v, u);
    }
    return sz[u];
}

int find_centroid(int u, int p, int n) {
    for(auto [v, w] : adj[u]) {
        if(v == p || visited[v]) {
            continue;
        }

        if(sz[v] > n / 2) {
            return find_centroid(v, u, n);
        }
    }
    return u;
}

void dfs(int u, int p, long long total, bool state, int depth, int K) {
    if(state) {
        if(mp.count(total)) {
            mp[total] = min(mp[total], depth);
        }
        else {
            mp[total] = depth;
        }
    }
    else {
        if(mp.count(K - total)) {
            ans = min(ans, depth + mp[K - total]);
        }
    }

    for(auto [v, w] : adj[u]) {
        if(v == p || visited[v]) {
            continue;
        }

        dfs(v, u, total + w, state, depth + 1, K);
    }
}

void centroid(int u, int K) {
    int c = find_centroid(u, -1, find_size(u, -1));
    visited[c] = 1;

    mp.clear();
    mp.emplace(0, 0);
    for(auto [v, w] : adj[c]) {
        if(visited[v]) {
            continue;
        }

        dfs(v, -1, w, 0, 1, K);
        dfs(v, -1, w, 1, 1, K);
    }

    for(auto [v, w] : adj[c]) {
        if(visited[v]) {
            continue;
        }

        centroid(v, K);
    }
}

int best_path(int N, int K, int H[][2], int L[]) {
    for(int i = 0; i < N - 1; i++) {
        H[i][0]++, H[i][1]++;
        adj[H[i][0]].emplace_back(H[i][1], L[i]);
        adj[H[i][1]].emplace_back(H[i][0], L[i]);
    }

    centroid(1, K);

    if(ans == INF) {
        ans = -1;
    }
    return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 2 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 2 ms 4940 KB Output is correct
7 Correct 2 ms 4940 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 3 ms 5068 KB Output is correct
10 Correct 3 ms 5004 KB Output is correct
11 Correct 3 ms 5000 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 5008 KB Output is correct
14 Correct 3 ms 5004 KB Output is correct
15 Correct 2 ms 5000 KB Output is correct
16 Correct 3 ms 5000 KB Output is correct
17 Correct 3 ms 5000 KB Output is correct
18 Correct 3 ms 4944 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 2 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 2 ms 4940 KB Output is correct
7 Correct 2 ms 4940 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 3 ms 5068 KB Output is correct
10 Correct 3 ms 5004 KB Output is correct
11 Correct 3 ms 5000 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 5008 KB Output is correct
14 Correct 3 ms 5004 KB Output is correct
15 Correct 2 ms 5000 KB Output is correct
16 Correct 3 ms 5000 KB Output is correct
17 Correct 3 ms 5000 KB Output is correct
18 Correct 3 ms 4944 KB Output is correct
19 Correct 3 ms 4996 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 5 ms 5068 KB Output is correct
22 Correct 5 ms 5148 KB Output is correct
23 Correct 4 ms 5068 KB Output is correct
24 Correct 4 ms 5068 KB Output is correct
25 Correct 4 ms 5068 KB Output is correct
26 Correct 5 ms 5068 KB Output is correct
27 Correct 3 ms 5020 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 4 ms 5148 KB Output is correct
30 Correct 4 ms 5140 KB Output is correct
31 Correct 4 ms 5068 KB Output is correct
32 Correct 4 ms 5068 KB Output is correct
33 Correct 5 ms 5012 KB Output is correct
34 Correct 4 ms 5140 KB Output is correct
35 Correct 5 ms 5136 KB Output is correct
36 Correct 4 ms 5012 KB Output is correct
37 Correct 4 ms 5012 KB Output is correct
38 Correct 4 ms 5012 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 2 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 2 ms 4940 KB Output is correct
7 Correct 2 ms 4940 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 3 ms 5068 KB Output is correct
10 Correct 3 ms 5004 KB Output is correct
11 Correct 3 ms 5000 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 5008 KB Output is correct
14 Correct 3 ms 5004 KB Output is correct
15 Correct 2 ms 5000 KB Output is correct
16 Correct 3 ms 5000 KB Output is correct
17 Correct 3 ms 5000 KB Output is correct
18 Correct 3 ms 4944 KB Output is correct
19 Correct 224 ms 11684 KB Output is correct
20 Correct 241 ms 11684 KB Output is correct
21 Correct 212 ms 11696 KB Output is correct
22 Correct 183 ms 11716 KB Output is correct
23 Correct 287 ms 12100 KB Output is correct
24 Correct 124 ms 11840 KB Output is correct
25 Correct 202 ms 16156 KB Output is correct
26 Correct 112 ms 17340 KB Output is correct
27 Correct 281 ms 18884 KB Output is correct
28 Correct 888 ms 42520 KB Output is correct
29 Correct 935 ms 41624 KB Output is correct
30 Correct 287 ms 19036 KB Output is correct
31 Correct 316 ms 18940 KB Output is correct
32 Correct 324 ms 19176 KB Output is correct
33 Correct 415 ms 17680 KB Output is correct
34 Correct 782 ms 30928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 4 ms 4940 KB Output is correct
3 Correct 2 ms 4940 KB Output is correct
4 Correct 3 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 2 ms 4940 KB Output is correct
7 Correct 2 ms 4940 KB Output is correct
8 Correct 2 ms 4940 KB Output is correct
9 Correct 3 ms 5068 KB Output is correct
10 Correct 3 ms 5004 KB Output is correct
11 Correct 3 ms 5000 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 5008 KB Output is correct
14 Correct 3 ms 5004 KB Output is correct
15 Correct 2 ms 5000 KB Output is correct
16 Correct 3 ms 5000 KB Output is correct
17 Correct 3 ms 5000 KB Output is correct
18 Correct 3 ms 4944 KB Output is correct
19 Correct 3 ms 4996 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 5 ms 5068 KB Output is correct
22 Correct 5 ms 5148 KB Output is correct
23 Correct 4 ms 5068 KB Output is correct
24 Correct 4 ms 5068 KB Output is correct
25 Correct 4 ms 5068 KB Output is correct
26 Correct 5 ms 5068 KB Output is correct
27 Correct 3 ms 5020 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 4 ms 5148 KB Output is correct
30 Correct 4 ms 5140 KB Output is correct
31 Correct 4 ms 5068 KB Output is correct
32 Correct 4 ms 5068 KB Output is correct
33 Correct 5 ms 5012 KB Output is correct
34 Correct 4 ms 5140 KB Output is correct
35 Correct 5 ms 5136 KB Output is correct
36 Correct 4 ms 5012 KB Output is correct
37 Correct 4 ms 5012 KB Output is correct
38 Correct 4 ms 5012 KB Output is correct
39 Correct 224 ms 11684 KB Output is correct
40 Correct 241 ms 11684 KB Output is correct
41 Correct 212 ms 11696 KB Output is correct
42 Correct 183 ms 11716 KB Output is correct
43 Correct 287 ms 12100 KB Output is correct
44 Correct 124 ms 11840 KB Output is correct
45 Correct 202 ms 16156 KB Output is correct
46 Correct 112 ms 17340 KB Output is correct
47 Correct 281 ms 18884 KB Output is correct
48 Correct 888 ms 42520 KB Output is correct
49 Correct 935 ms 41624 KB Output is correct
50 Correct 287 ms 19036 KB Output is correct
51 Correct 316 ms 18940 KB Output is correct
52 Correct 324 ms 19176 KB Output is correct
53 Correct 415 ms 17680 KB Output is correct
54 Correct 782 ms 30928 KB Output is correct
55 Correct 24 ms 5904 KB Output is correct
56 Correct 20 ms 5624 KB Output is correct
57 Correct 130 ms 11856 KB Output is correct
58 Correct 43 ms 11588 KB Output is correct
59 Correct 309 ms 22000 KB Output is correct
60 Correct 824 ms 40900 KB Output is correct
61 Correct 317 ms 20740 KB Output is correct
62 Correct 276 ms 19072 KB Output is correct
63 Correct 337 ms 19048 KB Output is correct
64 Correct 903 ms 25176 KB Output is correct
65 Correct 850 ms 32084 KB Output is correct
66 Correct 863 ms 39748 KB Output is correct
67 Correct 119 ms 19568 KB Output is correct
68 Correct 454 ms 30276 KB Output is correct
69 Correct 499 ms 30532 KB Output is correct
70 Correct 486 ms 29004 KB Output is correct