답안 #1000809

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1000809 2024-06-18T09:31:17 Z saayan007 경주 (Race) (IOI11_race) C++17
100 / 100
299 ms 32536 KB
/*

This problem can be found at https://oj.uz/problem/view/IOI11_race.

SOLUTION

Consider the problem of finding a path of length K containing minimum edges THAT GOES A FIXED NODE x.
This can be done in O(Size of Tree) time. Once this is done, we can remove the node x and edges connected to it. Then we can solve the problem for the remaining connected components.

The best choice of x is the centroid of the tree.

*/ 

#include "race.h"
#include "bits/stdc++.h"
using namespace std;
 
const int mxN = 2e5L + 10;
const int mxK = 1e6L + 10;
int n, k;
vector<pair<int, int>> adj[mxN];
int ans = mxN;
int sz[mxN];
bool proc[mxN] = {};
int dp[mxK];
 
int get_sz(int x, int p) {
    sz[x] = 1;
    for(auto [y, w] : adj[x]) {
        if(proc[y] || y == p) continue;
        sz[x] += get_sz(y, x);
    }
    return sz[x];
}
 
int get_cen(int x, int p, int tot) {
    for(auto [y, w] : adj[x]) {
        if(proc[y] || y == p || sz[y] * 2 <= tot) continue;
        return get_cen(y, x, tot);
    }
    return x;
}
 
void dfs(int x, int p, int dep, int dis, int flag) {
    if(dis > k) return;
    if(flag == 0) {
        if(dp[k - dis] != mxN) ans = min(ans, dep + dp[k - dis]);
    }
    else if(flag == 1) {
        dp[dis] = min(dp[dis], dep);
    }
    else if(flag == 2) {
        dp[dis] = mxN;
    }
 
    for(auto [y, w] : adj[x]) {
        if(proc[y] || y == p) continue;
        dfs(y, x, dep + 1, dis + w, flag);
    }
}
 
void solve(int x = 1, int p = -1) {
    int c = get_cen(x, p, get_sz(x, p));
    dp[0] = 0;
    for(auto [d, w] : adj[c]) {
        if(proc[d]) continue;
        dfs(d, c, 1, w, 0);
        dfs(d, c, 1, w, 1);
    }
    for(auto [d, w] : adj[c]) {
        if(proc[d]) continue;
        dfs(d, c, 1, w, 2);
    }
    dp[0] = mxN;
    proc[c] = 1;
    for(auto [d, w] : adj[c]) {
        if(proc[d]) continue;
        solve(d, c);
    }
}
 
int best_path(int N, int K, int H[][2], int L[])
{
    for(int i = 0; i < mxK; ++i) dp[i] = mxN;
    n = N; k = K;
    for(int i = 0; i < n - 1; ++i) {
        adj[H[i][0] + 1].emplace_back(H[i][1] + 1, L[i]);
        adj[H[i][1] + 1].emplace_back(H[i][0] + 1, L[i]);
    }
 
    solve();
    return (ans > n - 1 ? -1 : ans);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9052 KB Output is correct
2 Correct 4 ms 9052 KB Output is correct
3 Correct 4 ms 8920 KB Output is correct
4 Correct 4 ms 9052 KB Output is correct
5 Correct 4 ms 9052 KB Output is correct
6 Correct 4 ms 9052 KB Output is correct
7 Correct 4 ms 9052 KB Output is correct
8 Correct 4 ms 9052 KB Output is correct
9 Correct 3 ms 9052 KB Output is correct
10 Correct 4 ms 9052 KB Output is correct
11 Correct 4 ms 8844 KB Output is correct
12 Correct 4 ms 9052 KB Output is correct
13 Correct 4 ms 9052 KB Output is correct
14 Correct 4 ms 9052 KB Output is correct
15 Correct 3 ms 9052 KB Output is correct
16 Correct 4 ms 9052 KB Output is correct
17 Correct 4 ms 9052 KB Output is correct
18 Correct 4 ms 9052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9052 KB Output is correct
2 Correct 4 ms 9052 KB Output is correct
3 Correct 4 ms 8920 KB Output is correct
4 Correct 4 ms 9052 KB Output is correct
5 Correct 4 ms 9052 KB Output is correct
6 Correct 4 ms 9052 KB Output is correct
7 Correct 4 ms 9052 KB Output is correct
8 Correct 4 ms 9052 KB Output is correct
9 Correct 3 ms 9052 KB Output is correct
10 Correct 4 ms 9052 KB Output is correct
11 Correct 4 ms 8844 KB Output is correct
12 Correct 4 ms 9052 KB Output is correct
13 Correct 4 ms 9052 KB Output is correct
14 Correct 4 ms 9052 KB Output is correct
15 Correct 3 ms 9052 KB Output is correct
16 Correct 4 ms 9052 KB Output is correct
17 Correct 4 ms 9052 KB Output is correct
18 Correct 4 ms 9052 KB Output is correct
19 Correct 3 ms 9052 KB Output is correct
20 Correct 3 ms 9052 KB Output is correct
21 Correct 4 ms 9052 KB Output is correct
22 Correct 4 ms 9052 KB Output is correct
23 Correct 4 ms 9052 KB Output is correct
24 Correct 4 ms 9052 KB Output is correct
25 Correct 4 ms 9052 KB Output is correct
26 Correct 4 ms 9012 KB Output is correct
27 Correct 4 ms 9052 KB Output is correct
28 Correct 4 ms 9052 KB Output is correct
29 Correct 4 ms 8992 KB Output is correct
30 Correct 4 ms 8980 KB Output is correct
31 Correct 4 ms 9048 KB Output is correct
32 Correct 4 ms 9052 KB Output is correct
33 Correct 8 ms 9052 KB Output is correct
34 Correct 5 ms 9052 KB Output is correct
35 Correct 4 ms 9052 KB Output is correct
36 Correct 4 ms 9052 KB Output is correct
37 Correct 4 ms 9052 KB Output is correct
38 Correct 4 ms 9052 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9052 KB Output is correct
2 Correct 4 ms 9052 KB Output is correct
3 Correct 4 ms 8920 KB Output is correct
4 Correct 4 ms 9052 KB Output is correct
5 Correct 4 ms 9052 KB Output is correct
6 Correct 4 ms 9052 KB Output is correct
7 Correct 4 ms 9052 KB Output is correct
8 Correct 4 ms 9052 KB Output is correct
9 Correct 3 ms 9052 KB Output is correct
10 Correct 4 ms 9052 KB Output is correct
11 Correct 4 ms 8844 KB Output is correct
12 Correct 4 ms 9052 KB Output is correct
13 Correct 4 ms 9052 KB Output is correct
14 Correct 4 ms 9052 KB Output is correct
15 Correct 3 ms 9052 KB Output is correct
16 Correct 4 ms 9052 KB Output is correct
17 Correct 4 ms 9052 KB Output is correct
18 Correct 4 ms 9052 KB Output is correct
19 Correct 89 ms 14420 KB Output is correct
20 Correct 85 ms 15440 KB Output is correct
21 Correct 102 ms 15304 KB Output is correct
22 Correct 85 ms 15700 KB Output is correct
23 Correct 47 ms 15960 KB Output is correct
24 Correct 36 ms 15700 KB Output is correct
25 Correct 92 ms 18348 KB Output is correct
26 Correct 69 ms 20756 KB Output is correct
27 Correct 101 ms 21328 KB Output is correct
28 Correct 153 ms 32536 KB Output is correct
29 Correct 149 ms 31828 KB Output is correct
30 Correct 98 ms 21840 KB Output is correct
31 Correct 116 ms 21844 KB Output is correct
32 Correct 111 ms 21840 KB Output is correct
33 Correct 138 ms 21060 KB Output is correct
34 Correct 112 ms 20800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9052 KB Output is correct
2 Correct 4 ms 9052 KB Output is correct
3 Correct 4 ms 8920 KB Output is correct
4 Correct 4 ms 9052 KB Output is correct
5 Correct 4 ms 9052 KB Output is correct
6 Correct 4 ms 9052 KB Output is correct
7 Correct 4 ms 9052 KB Output is correct
8 Correct 4 ms 9052 KB Output is correct
9 Correct 3 ms 9052 KB Output is correct
10 Correct 4 ms 9052 KB Output is correct
11 Correct 4 ms 8844 KB Output is correct
12 Correct 4 ms 9052 KB Output is correct
13 Correct 4 ms 9052 KB Output is correct
14 Correct 4 ms 9052 KB Output is correct
15 Correct 3 ms 9052 KB Output is correct
16 Correct 4 ms 9052 KB Output is correct
17 Correct 4 ms 9052 KB Output is correct
18 Correct 4 ms 9052 KB Output is correct
19 Correct 3 ms 9052 KB Output is correct
20 Correct 3 ms 9052 KB Output is correct
21 Correct 4 ms 9052 KB Output is correct
22 Correct 4 ms 9052 KB Output is correct
23 Correct 4 ms 9052 KB Output is correct
24 Correct 4 ms 9052 KB Output is correct
25 Correct 4 ms 9052 KB Output is correct
26 Correct 4 ms 9012 KB Output is correct
27 Correct 4 ms 9052 KB Output is correct
28 Correct 4 ms 9052 KB Output is correct
29 Correct 4 ms 8992 KB Output is correct
30 Correct 4 ms 8980 KB Output is correct
31 Correct 4 ms 9048 KB Output is correct
32 Correct 4 ms 9052 KB Output is correct
33 Correct 8 ms 9052 KB Output is correct
34 Correct 5 ms 9052 KB Output is correct
35 Correct 4 ms 9052 KB Output is correct
36 Correct 4 ms 9052 KB Output is correct
37 Correct 4 ms 9052 KB Output is correct
38 Correct 4 ms 9052 KB Output is correct
39 Correct 89 ms 14420 KB Output is correct
40 Correct 85 ms 15440 KB Output is correct
41 Correct 102 ms 15304 KB Output is correct
42 Correct 85 ms 15700 KB Output is correct
43 Correct 47 ms 15960 KB Output is correct
44 Correct 36 ms 15700 KB Output is correct
45 Correct 92 ms 18348 KB Output is correct
46 Correct 69 ms 20756 KB Output is correct
47 Correct 101 ms 21328 KB Output is correct
48 Correct 153 ms 32536 KB Output is correct
49 Correct 149 ms 31828 KB Output is correct
50 Correct 98 ms 21840 KB Output is correct
51 Correct 116 ms 21844 KB Output is correct
52 Correct 111 ms 21840 KB Output is correct
53 Correct 138 ms 21060 KB Output is correct
54 Correct 112 ms 20800 KB Output is correct
55 Correct 10 ms 9560 KB Output is correct
56 Correct 10 ms 9596 KB Output is correct
57 Correct 52 ms 14804 KB Output is correct
58 Correct 28 ms 15568 KB Output is correct
59 Correct 71 ms 20820 KB Output is correct
60 Correct 299 ms 32128 KB Output is correct
61 Correct 123 ms 21328 KB Output is correct
62 Correct 117 ms 21768 KB Output is correct
63 Correct 159 ms 21844 KB Output is correct
64 Correct 280 ms 20560 KB Output is correct
65 Correct 112 ms 22652 KB Output is correct
66 Correct 246 ms 27988 KB Output is correct
67 Correct 64 ms 23240 KB Output is correct
68 Correct 151 ms 22276 KB Output is correct
69 Correct 142 ms 20252 KB Output is correct
70 Correct 135 ms 21836 KB Output is correct