답안 #998601

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
998601 2024-06-14T10:23:51 Z amine_aroua 경주 (Race) (IOI11_race) C++17
100 / 100
216 ms 42556 KB
#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
using namespace std;
#define pb push_back
#define nl '\n'
#define fore(i, y) for(int i = 0; i < y; i++)
#define forr(i, x, y) for(int i = x;i<=y;i++)
#define forn(i, y, x) for(int i = y; i >= x; i--)
const int MAX_N = 200010;
const int MAX_K = 1000010;
int k;
int res = MAX_N;
vector<pair<int ,int>> adj[MAX_N];
vector<int> minPath(MAX_K , MAX_N);
bool forbidden[MAX_N];
int sz[MAX_N];
int depth[MAX_N] , dist[MAX_N];
void dfsSZ(int x , int p)
{
    sz[x] = 1;
    for(auto [u,l]   : adj[x])
    {
        if(u == p || forbidden[u])
            continue;
        dfsSZ(u , x);
        sz[x]+=sz[u];
    }
}
int dfs_down(int x , int p , int n)
{
    for(auto [u , l] : adj[x])
    {
        if(u == p || forbidden[u])
            continue;
        if(sz[u] > n/2)
            return dfs_down(u , x , n);
    }
    return x;
}
vector<int> nodes;
void dfs_compute(int x , int p , int L)
{
    nodes.pb(x);
    depth[x] = depth[p] + 1;
    dist[x] = dist[p] + L;
    for(auto [u , l] : adj[x])
    {
        if(u == p || forbidden[u])
            continue;
        dfs_compute(u , x , l);
    }
}
void rec(int root)
{
    dfsSZ(root , -1);
    int n = sz[root];
    int centroid = dfs_down(root , -1 , n);
    depth[centroid] = dist[centroid] = 0;
    minPath[0] = 0;
    vector<int> reset;
    reset.pb(0);
    for(auto [child , l] : adj[centroid])
    {
        if(forbidden[child])
            continue;
        nodes.clear();
        dfs_compute(child , centroid , l);
        for(auto node : nodes)
        {
            if(dist[node] <= k)
                res = min(res , depth[node] + minPath[k - dist[node]]);
        }
        for(auto node : nodes)
        {
            if(dist[node] <= k)
            {
                reset.pb(dist[node]);
                minPath[dist[node]] = min(minPath[dist[node]] , depth[node]);
            }
        }
    }
    for(auto r : reset)
        minPath[r] = MAX_N;
    forbidden[centroid] = 1;
    for(auto [child , l] : adj[centroid])
    {
        if(forbidden[child])
            continue;
        rec(child);
    }
}
int best_path(int N, int K, int H[][2], int L[])
{
    k = K;
    fore(i , N - 1)
    {
        adj[H[i][0]].pb({H[i][1] , L[i]});
        adj[H[i][1]].pb({H[i][0] , L[i]});
    }
    rec(0);
    return (res == MAX_N ? -1 : res);
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15192 KB Output is correct
2 Correct 3 ms 15196 KB Output is correct
3 Correct 3 ms 15192 KB Output is correct
4 Correct 2 ms 15196 KB Output is correct
5 Correct 2 ms 15196 KB Output is correct
6 Correct 3 ms 15196 KB Output is correct
7 Correct 3 ms 15196 KB Output is correct
8 Correct 3 ms 15192 KB Output is correct
9 Correct 3 ms 15192 KB Output is correct
10 Correct 3 ms 15196 KB Output is correct
11 Correct 3 ms 15192 KB Output is correct
12 Correct 3 ms 15196 KB Output is correct
13 Correct 3 ms 15196 KB Output is correct
14 Correct 3 ms 15196 KB Output is correct
15 Correct 3 ms 15196 KB Output is correct
16 Correct 3 ms 15196 KB Output is correct
17 Correct 2 ms 15196 KB Output is correct
18 Correct 3 ms 15196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15192 KB Output is correct
2 Correct 3 ms 15196 KB Output is correct
3 Correct 3 ms 15192 KB Output is correct
4 Correct 2 ms 15196 KB Output is correct
5 Correct 2 ms 15196 KB Output is correct
6 Correct 3 ms 15196 KB Output is correct
7 Correct 3 ms 15196 KB Output is correct
8 Correct 3 ms 15192 KB Output is correct
9 Correct 3 ms 15192 KB Output is correct
10 Correct 3 ms 15196 KB Output is correct
11 Correct 3 ms 15192 KB Output is correct
12 Correct 3 ms 15196 KB Output is correct
13 Correct 3 ms 15196 KB Output is correct
14 Correct 3 ms 15196 KB Output is correct
15 Correct 3 ms 15196 KB Output is correct
16 Correct 3 ms 15196 KB Output is correct
17 Correct 2 ms 15196 KB Output is correct
18 Correct 3 ms 15196 KB Output is correct
19 Correct 3 ms 15192 KB Output is correct
20 Correct 3 ms 15196 KB Output is correct
21 Correct 3 ms 15196 KB Output is correct
22 Correct 3 ms 15196 KB Output is correct
23 Correct 3 ms 15196 KB Output is correct
24 Correct 3 ms 15196 KB Output is correct
25 Correct 3 ms 15192 KB Output is correct
26 Correct 3 ms 15196 KB Output is correct
27 Correct 3 ms 15236 KB Output is correct
28 Correct 3 ms 15192 KB Output is correct
29 Correct 3 ms 15196 KB Output is correct
30 Correct 4 ms 15196 KB Output is correct
31 Correct 3 ms 15196 KB Output is correct
32 Correct 3 ms 15196 KB Output is correct
33 Correct 3 ms 15196 KB Output is correct
34 Correct 3 ms 15196 KB Output is correct
35 Correct 4 ms 15196 KB Output is correct
36 Correct 3 ms 15196 KB Output is correct
37 Correct 3 ms 15196 KB Output is correct
38 Correct 3 ms 15196 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15192 KB Output is correct
2 Correct 3 ms 15196 KB Output is correct
3 Correct 3 ms 15192 KB Output is correct
4 Correct 2 ms 15196 KB Output is correct
5 Correct 2 ms 15196 KB Output is correct
6 Correct 3 ms 15196 KB Output is correct
7 Correct 3 ms 15196 KB Output is correct
8 Correct 3 ms 15192 KB Output is correct
9 Correct 3 ms 15192 KB Output is correct
10 Correct 3 ms 15196 KB Output is correct
11 Correct 3 ms 15192 KB Output is correct
12 Correct 3 ms 15196 KB Output is correct
13 Correct 3 ms 15196 KB Output is correct
14 Correct 3 ms 15196 KB Output is correct
15 Correct 3 ms 15196 KB Output is correct
16 Correct 3 ms 15196 KB Output is correct
17 Correct 2 ms 15196 KB Output is correct
18 Correct 3 ms 15196 KB Output is correct
19 Correct 81 ms 21584 KB Output is correct
20 Correct 81 ms 22512 KB Output is correct
21 Correct 75 ms 22348 KB Output is correct
22 Correct 66 ms 23252 KB Output is correct
23 Correct 65 ms 23132 KB Output is correct
24 Correct 39 ms 22608 KB Output is correct
25 Correct 89 ms 26196 KB Output is correct
26 Correct 63 ms 30028 KB Output is correct
27 Correct 102 ms 26448 KB Output is correct
28 Correct 209 ms 41420 KB Output is correct
29 Correct 194 ms 40620 KB Output is correct
30 Correct 107 ms 27220 KB Output is correct
31 Correct 99 ms 27216 KB Output is correct
32 Correct 117 ms 26964 KB Output is correct
33 Correct 147 ms 26784 KB Output is correct
34 Correct 143 ms 27024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 15192 KB Output is correct
2 Correct 3 ms 15196 KB Output is correct
3 Correct 3 ms 15192 KB Output is correct
4 Correct 2 ms 15196 KB Output is correct
5 Correct 2 ms 15196 KB Output is correct
6 Correct 3 ms 15196 KB Output is correct
7 Correct 3 ms 15196 KB Output is correct
8 Correct 3 ms 15192 KB Output is correct
9 Correct 3 ms 15192 KB Output is correct
10 Correct 3 ms 15196 KB Output is correct
11 Correct 3 ms 15192 KB Output is correct
12 Correct 3 ms 15196 KB Output is correct
13 Correct 3 ms 15196 KB Output is correct
14 Correct 3 ms 15196 KB Output is correct
15 Correct 3 ms 15196 KB Output is correct
16 Correct 3 ms 15196 KB Output is correct
17 Correct 2 ms 15196 KB Output is correct
18 Correct 3 ms 15196 KB Output is correct
19 Correct 3 ms 15192 KB Output is correct
20 Correct 3 ms 15196 KB Output is correct
21 Correct 3 ms 15196 KB Output is correct
22 Correct 3 ms 15196 KB Output is correct
23 Correct 3 ms 15196 KB Output is correct
24 Correct 3 ms 15196 KB Output is correct
25 Correct 3 ms 15192 KB Output is correct
26 Correct 3 ms 15196 KB Output is correct
27 Correct 3 ms 15236 KB Output is correct
28 Correct 3 ms 15192 KB Output is correct
29 Correct 3 ms 15196 KB Output is correct
30 Correct 4 ms 15196 KB Output is correct
31 Correct 3 ms 15196 KB Output is correct
32 Correct 3 ms 15196 KB Output is correct
33 Correct 3 ms 15196 KB Output is correct
34 Correct 3 ms 15196 KB Output is correct
35 Correct 4 ms 15196 KB Output is correct
36 Correct 3 ms 15196 KB Output is correct
37 Correct 3 ms 15196 KB Output is correct
38 Correct 3 ms 15196 KB Output is correct
39 Correct 81 ms 21584 KB Output is correct
40 Correct 81 ms 22512 KB Output is correct
41 Correct 75 ms 22348 KB Output is correct
42 Correct 66 ms 23252 KB Output is correct
43 Correct 65 ms 23132 KB Output is correct
44 Correct 39 ms 22608 KB Output is correct
45 Correct 89 ms 26196 KB Output is correct
46 Correct 63 ms 30028 KB Output is correct
47 Correct 102 ms 26448 KB Output is correct
48 Correct 209 ms 41420 KB Output is correct
49 Correct 194 ms 40620 KB Output is correct
50 Correct 107 ms 27220 KB Output is correct
51 Correct 99 ms 27216 KB Output is correct
52 Correct 117 ms 26964 KB Output is correct
53 Correct 147 ms 26784 KB Output is correct
54 Correct 143 ms 27024 KB Output is correct
55 Correct 8 ms 15708 KB Output is correct
56 Correct 8 ms 15708 KB Output is correct
57 Correct 49 ms 22360 KB Output is correct
58 Correct 24 ms 23240 KB Output is correct
59 Correct 73 ms 30284 KB Output is correct
60 Correct 216 ms 42556 KB Output is correct
61 Correct 104 ms 26704 KB Output is correct
62 Correct 114 ms 28116 KB Output is correct
63 Correct 120 ms 28136 KB Output is correct
64 Correct 188 ms 27776 KB Output is correct
65 Correct 138 ms 28628 KB Output is correct
66 Correct 213 ms 36300 KB Output is correct
67 Correct 57 ms 30412 KB Output is correct
68 Correct 127 ms 29004 KB Output is correct
69 Correct 119 ms 26892 KB Output is correct
70 Correct 107 ms 28992 KB Output is correct