답안 #1115378

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1115378 2024-11-20T12:05:39 Z gustavo_d 경주 (Race) (IOI11_race) C++17
100 / 100
269 ms 31568 KB
// #include "race.h"
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
 
const int MAXN = 2e5;
const int MAXV = 1e6+1;
vector<pair<int, int>> adj[MAXN];
bool vis[MAXN];
int sz[MAXN];
bool in[MAXN];
 
int n, k;
 
void dfs(int v, int pai) {
    sz[v] = 1;
    for (auto& [viz, w] : adj[v]) {
        if (viz == pai or in[viz]) continue;
        dfs(viz, v);
        sz[v] += sz[viz];
    }
}

int find_centroid(int v, int pai, int qty) {
    // cout << "buscando centroid " << v << endl;
    for (auto& [viz, w] : adj[v]) {
        if (viz == pai or in[viz]) continue;
        if (sz[viz] > qty / 2) return find_centroid(viz, v, qty);
    }
    // cout << "é" << v <<endl;
    return v;
}

int ans = 1e8;
vector<int> dists(MAXV, 1e8);
// op = 1 => preenche, -1 => apaga, 0 => pega resposta
void dfs_dist(int v, int pai, int dist, int depth, int op) {
    if (dist > k) return;
    if (op == 0)
        ans = min(ans, dists[k-dist] + depth);
    else if (op == 1)
        dists[dist] = min(dists[dist], depth);
    else dists[dist] = 1e8;
    for (auto& [viz, w] : adj[v]) {
        if (viz == pai or in[viz]) continue;
        dfs_dist(viz, v, dist + w, depth+1, op);
    }
}
 
void decomp(int v) {
    dfs(v, -1);
    int centroid = find_centroid(v, -1, sz[v]);
    in[centroid] = true;
    // if (centroid == 8) cout << centroid << " é centroid decompondo" << v << endl;
    for (auto& [viz, w] : adj[centroid]) {
        if (in[viz]) continue;
        dfs_dist(viz, centroid, w, 1, 0);  // resposta considera prefixo subárvores
        dfs_dist(viz, centroid, w, 1, 1);
    }
    ans = min(ans, dists[k]);
    for (auto& [viz, w] : adj[centroid]) {
        if (in[viz]) continue;
        dfs_dist(viz, centroid, w, 1, -1);
    }
    for (auto& [viz, w] : adj[centroid]) {
        if (in[viz]) continue;
        decomp(viz);
    }
}
 
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 u = H[i][0], v = H[i][1];
        int w = L[i];
        adj[u].push_back({v, w});
        adj[v].push_back({u, w});
    }
 
    decomp(0);
 
    return (ans == (int)1e8 ? -1 : ans);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 13648 KB Output is correct
2 Correct 5 ms 13660 KB Output is correct
3 Correct 5 ms 13648 KB Output is correct
4 Correct 5 ms 13796 KB Output is correct
5 Correct 5 ms 13648 KB Output is correct
6 Correct 5 ms 13744 KB Output is correct
7 Correct 6 ms 13956 KB Output is correct
8 Correct 5 ms 13648 KB Output is correct
9 Correct 5 ms 13648 KB Output is correct
10 Correct 5 ms 13648 KB Output is correct
11 Correct 6 ms 13648 KB Output is correct
12 Correct 4 ms 13648 KB Output is correct
13 Correct 5 ms 13648 KB Output is correct
14 Correct 5 ms 13648 KB Output is correct
15 Correct 5 ms 13648 KB Output is correct
16 Correct 5 ms 13648 KB Output is correct
17 Correct 5 ms 13648 KB Output is correct
18 Correct 6 ms 13648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 13648 KB Output is correct
2 Correct 5 ms 13660 KB Output is correct
3 Correct 5 ms 13648 KB Output is correct
4 Correct 5 ms 13796 KB Output is correct
5 Correct 5 ms 13648 KB Output is correct
6 Correct 5 ms 13744 KB Output is correct
7 Correct 6 ms 13956 KB Output is correct
8 Correct 5 ms 13648 KB Output is correct
9 Correct 5 ms 13648 KB Output is correct
10 Correct 5 ms 13648 KB Output is correct
11 Correct 6 ms 13648 KB Output is correct
12 Correct 4 ms 13648 KB Output is correct
13 Correct 5 ms 13648 KB Output is correct
14 Correct 5 ms 13648 KB Output is correct
15 Correct 5 ms 13648 KB Output is correct
16 Correct 5 ms 13648 KB Output is correct
17 Correct 5 ms 13648 KB Output is correct
18 Correct 6 ms 13648 KB Output is correct
19 Correct 5 ms 13648 KB Output is correct
20 Correct 8 ms 13648 KB Output is correct
21 Correct 6 ms 13904 KB Output is correct
22 Correct 6 ms 13904 KB Output is correct
23 Correct 5 ms 13916 KB Output is correct
24 Correct 5 ms 13904 KB Output is correct
25 Correct 5 ms 13904 KB Output is correct
26 Correct 5 ms 13904 KB Output is correct
27 Correct 5 ms 13904 KB Output is correct
28 Correct 5 ms 13904 KB Output is correct
29 Correct 5 ms 13732 KB Output is correct
30 Correct 5 ms 13904 KB Output is correct
31 Correct 5 ms 13904 KB Output is correct
32 Correct 5 ms 13904 KB Output is correct
33 Correct 5 ms 14072 KB Output is correct
34 Correct 5 ms 13904 KB Output is correct
35 Correct 5 ms 13904 KB Output is correct
36 Correct 5 ms 13796 KB Output is correct
37 Correct 5 ms 13904 KB Output is correct
38 Correct 5 ms 13904 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 13648 KB Output is correct
2 Correct 5 ms 13660 KB Output is correct
3 Correct 5 ms 13648 KB Output is correct
4 Correct 5 ms 13796 KB Output is correct
5 Correct 5 ms 13648 KB Output is correct
6 Correct 5 ms 13744 KB Output is correct
7 Correct 6 ms 13956 KB Output is correct
8 Correct 5 ms 13648 KB Output is correct
9 Correct 5 ms 13648 KB Output is correct
10 Correct 5 ms 13648 KB Output is correct
11 Correct 6 ms 13648 KB Output is correct
12 Correct 4 ms 13648 KB Output is correct
13 Correct 5 ms 13648 KB Output is correct
14 Correct 5 ms 13648 KB Output is correct
15 Correct 5 ms 13648 KB Output is correct
16 Correct 5 ms 13648 KB Output is correct
17 Correct 5 ms 13648 KB Output is correct
18 Correct 6 ms 13648 KB Output is correct
19 Correct 87 ms 19668 KB Output is correct
20 Correct 82 ms 19668 KB Output is correct
21 Correct 91 ms 19536 KB Output is correct
22 Correct 80 ms 19664 KB Output is correct
23 Correct 46 ms 19784 KB Output is correct
24 Correct 34 ms 19784 KB Output is correct
25 Correct 76 ms 20816 KB Output is correct
26 Correct 66 ms 23624 KB Output is correct
27 Correct 94 ms 26440 KB Output is correct
28 Correct 142 ms 31568 KB Output is correct
29 Correct 144 ms 31048 KB Output is correct
30 Correct 96 ms 26440 KB Output is correct
31 Correct 99 ms 26448 KB Output is correct
32 Correct 107 ms 26564 KB Output is correct
33 Correct 144 ms 25308 KB Output is correct
34 Correct 121 ms 26196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 13648 KB Output is correct
2 Correct 5 ms 13660 KB Output is correct
3 Correct 5 ms 13648 KB Output is correct
4 Correct 5 ms 13796 KB Output is correct
5 Correct 5 ms 13648 KB Output is correct
6 Correct 5 ms 13744 KB Output is correct
7 Correct 6 ms 13956 KB Output is correct
8 Correct 5 ms 13648 KB Output is correct
9 Correct 5 ms 13648 KB Output is correct
10 Correct 5 ms 13648 KB Output is correct
11 Correct 6 ms 13648 KB Output is correct
12 Correct 4 ms 13648 KB Output is correct
13 Correct 5 ms 13648 KB Output is correct
14 Correct 5 ms 13648 KB Output is correct
15 Correct 5 ms 13648 KB Output is correct
16 Correct 5 ms 13648 KB Output is correct
17 Correct 5 ms 13648 KB Output is correct
18 Correct 6 ms 13648 KB Output is correct
19 Correct 5 ms 13648 KB Output is correct
20 Correct 8 ms 13648 KB Output is correct
21 Correct 6 ms 13904 KB Output is correct
22 Correct 6 ms 13904 KB Output is correct
23 Correct 5 ms 13916 KB Output is correct
24 Correct 5 ms 13904 KB Output is correct
25 Correct 5 ms 13904 KB Output is correct
26 Correct 5 ms 13904 KB Output is correct
27 Correct 5 ms 13904 KB Output is correct
28 Correct 5 ms 13904 KB Output is correct
29 Correct 5 ms 13732 KB Output is correct
30 Correct 5 ms 13904 KB Output is correct
31 Correct 5 ms 13904 KB Output is correct
32 Correct 5 ms 13904 KB Output is correct
33 Correct 5 ms 14072 KB Output is correct
34 Correct 5 ms 13904 KB Output is correct
35 Correct 5 ms 13904 KB Output is correct
36 Correct 5 ms 13796 KB Output is correct
37 Correct 5 ms 13904 KB Output is correct
38 Correct 5 ms 13904 KB Output is correct
39 Correct 87 ms 19668 KB Output is correct
40 Correct 82 ms 19668 KB Output is correct
41 Correct 91 ms 19536 KB Output is correct
42 Correct 80 ms 19664 KB Output is correct
43 Correct 46 ms 19784 KB Output is correct
44 Correct 34 ms 19784 KB Output is correct
45 Correct 76 ms 20816 KB Output is correct
46 Correct 66 ms 23624 KB Output is correct
47 Correct 94 ms 26440 KB Output is correct
48 Correct 142 ms 31568 KB Output is correct
49 Correct 144 ms 31048 KB Output is correct
50 Correct 96 ms 26440 KB Output is correct
51 Correct 99 ms 26448 KB Output is correct
52 Correct 107 ms 26564 KB Output is correct
53 Correct 144 ms 25308 KB Output is correct
54 Correct 121 ms 26196 KB Output is correct
55 Correct 11 ms 14416 KB Output is correct
56 Correct 11 ms 14408 KB Output is correct
57 Correct 60 ms 21328 KB Output is correct
58 Correct 27 ms 20932 KB Output is correct
59 Correct 68 ms 23632 KB Output is correct
60 Correct 269 ms 31152 KB Output is correct
61 Correct 107 ms 26700 KB Output is correct
62 Correct 125 ms 26436 KB Output is correct
63 Correct 143 ms 26588 KB Output is correct
64 Correct 267 ms 26700 KB Output is correct
65 Correct 102 ms 27336 KB Output is correct
66 Correct 212 ms 30272 KB Output is correct
67 Correct 63 ms 27576 KB Output is correct
68 Correct 161 ms 27424 KB Output is correct
69 Correct 148 ms 27464 KB Output is correct
70 Correct 139 ms 26960 KB Output is correct