답안 #971789

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
971789 2024-04-29T09:47:06 Z tnknguyen_ 경주 (Race) (IOI11_race) C++14
100 / 100
358 ms 41572 KB
#include <bits/stdc++.h>
using namespace std;
#define endl '\n' 
#define pii pair<int, int>
//#define int long long 

const int MX = 2e5 + 5;
const int MK = 1e6 + 5;

vector<pii> gr[MX];
vector<int> values;
int f[MK], vi[MK];
int sz[MX], del[MX];
int n, k, ans = 1e9;

int dfs(int u, int p){
    sz[u] = 1;
    for(pii e : gr[u]){
        int v = e.first;
        if(v != p && !del[v]){
            sz[u] += dfs(v, u);
        }
    }
    return sz[u];
}

int centroid(int u, int p, const int &n){
    for(pii e : gr[u]){
        int v = e.first;
        if(v != p && !del[v] && sz[v] > n/2){
            return centroid(v, u, n);
        }
    }
    return u;
}

void solve(int u, int p, int dep, int sum, int &ans, const int &op, const int &k){
    if(sum > k){
        return;
    }
    if(op == 1){
        ans = min(ans, dep + f[k - sum]);
    }
    else{
        f[sum] = min(f[sum], dep);
        values.push_back(sum);
    }

    for(pii e : gr[u]){
        int v, c;
        tie(v, c) = e;
        if(v != p && !del[v]){
            solve(v, u, dep + 1, sum + c, ans, op, k);
        }
    }
}

void decompose(int u){
    dfs(u, 0);
    u = centroid(u, 0, sz[u]);
    del[u] = 1;
    vector<int> reset;

    for(pii e : gr[u]){
        int v, c;
        tie(v, c) = e;
        if(!del[v]){
            values.clear();
            solve(v, u, 1, c, ans, 1, k);
            solve(v, u, 1, c, ans, 2, k);
            for(int x : values){
                if(vi[x] == 0){
                    reset.push_back(x);
                    vi[x] = 1;
                }
            }
        }
    }    

    for(int x : reset){
        f[x] = 1e9;
        vi[x] = 0;
    }

    for(pii e : gr[u]){
        int v, c;
        tie(v, c) = e;
        if(!del[v]){
            decompose(v);  
        }
    }
}

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], c = L[i];
        ++u, ++v;
        gr[u].push_back({v, c});
        gr[v].push_back({u, c});
    }

    fill(f+1, f+K+1, 1e9);
    decompose(1);

    return (ans == 1e9 ? -1 : ans);
}

// int32_t main() {
//     ios_base::sync_with_stdio(0);
//     cin.tie(0);
//     cout.tie(0);

//     freopen("main.inp","r",stdin);
//     freopen("main.out","w",stdout);

//     int n, k;
//     cin>>n>>k;
//     int h[n][2], l[n];

//     for(int i=0; i<n-1; ++i){
//         cin>>h[i][0]>>h[i][1]>>l[i];
//     }

//     cout<<best_path(n, k, h, l);

//     return 0;
// }
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13916 KB Output is correct
2 Correct 3 ms 13916 KB Output is correct
3 Correct 2 ms 13916 KB Output is correct
4 Correct 2 ms 13916 KB Output is correct
5 Correct 3 ms 13916 KB Output is correct
6 Correct 2 ms 13916 KB Output is correct
7 Correct 3 ms 13916 KB Output is correct
8 Correct 4 ms 13912 KB Output is correct
9 Correct 3 ms 14168 KB Output is correct
10 Correct 2 ms 13916 KB Output is correct
11 Correct 3 ms 13916 KB Output is correct
12 Correct 3 ms 13916 KB Output is correct
13 Correct 3 ms 13916 KB Output is correct
14 Correct 2 ms 13916 KB Output is correct
15 Correct 3 ms 13916 KB Output is correct
16 Correct 3 ms 13916 KB Output is correct
17 Correct 3 ms 13964 KB Output is correct
18 Correct 3 ms 13916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13916 KB Output is correct
2 Correct 3 ms 13916 KB Output is correct
3 Correct 2 ms 13916 KB Output is correct
4 Correct 2 ms 13916 KB Output is correct
5 Correct 3 ms 13916 KB Output is correct
6 Correct 2 ms 13916 KB Output is correct
7 Correct 3 ms 13916 KB Output is correct
8 Correct 4 ms 13912 KB Output is correct
9 Correct 3 ms 14168 KB Output is correct
10 Correct 2 ms 13916 KB Output is correct
11 Correct 3 ms 13916 KB Output is correct
12 Correct 3 ms 13916 KB Output is correct
13 Correct 3 ms 13916 KB Output is correct
14 Correct 2 ms 13916 KB Output is correct
15 Correct 3 ms 13916 KB Output is correct
16 Correct 3 ms 13916 KB Output is correct
17 Correct 3 ms 13964 KB Output is correct
18 Correct 3 ms 13916 KB Output is correct
19 Correct 3 ms 13916 KB Output is correct
20 Correct 3 ms 13916 KB Output is correct
21 Correct 3 ms 14168 KB Output is correct
22 Correct 4 ms 17500 KB Output is correct
23 Correct 5 ms 17500 KB Output is correct
24 Correct 5 ms 17756 KB Output is correct
25 Correct 4 ms 16728 KB Output is correct
26 Correct 4 ms 16476 KB Output is correct
27 Correct 4 ms 15964 KB Output is correct
28 Correct 4 ms 14012 KB Output is correct
29 Correct 4 ms 16476 KB Output is correct
30 Correct 4 ms 16476 KB Output is correct
31 Correct 5 ms 16988 KB Output is correct
32 Correct 4 ms 16984 KB Output is correct
33 Correct 4 ms 17756 KB Output is correct
34 Correct 4 ms 17244 KB Output is correct
35 Correct 4 ms 18012 KB Output is correct
36 Correct 5 ms 17936 KB Output is correct
37 Correct 5 ms 16988 KB Output is correct
38 Correct 4 ms 16368 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13916 KB Output is correct
2 Correct 3 ms 13916 KB Output is correct
3 Correct 2 ms 13916 KB Output is correct
4 Correct 2 ms 13916 KB Output is correct
5 Correct 3 ms 13916 KB Output is correct
6 Correct 2 ms 13916 KB Output is correct
7 Correct 3 ms 13916 KB Output is correct
8 Correct 4 ms 13912 KB Output is correct
9 Correct 3 ms 14168 KB Output is correct
10 Correct 2 ms 13916 KB Output is correct
11 Correct 3 ms 13916 KB Output is correct
12 Correct 3 ms 13916 KB Output is correct
13 Correct 3 ms 13916 KB Output is correct
14 Correct 2 ms 13916 KB Output is correct
15 Correct 3 ms 13916 KB Output is correct
16 Correct 3 ms 13916 KB Output is correct
17 Correct 3 ms 13964 KB Output is correct
18 Correct 3 ms 13916 KB Output is correct
19 Correct 95 ms 20052 KB Output is correct
20 Correct 101 ms 21140 KB Output is correct
21 Correct 90 ms 20816 KB Output is correct
22 Correct 83 ms 21480 KB Output is correct
23 Correct 57 ms 21624 KB Output is correct
24 Correct 42 ms 21252 KB Output is correct
25 Correct 91 ms 23888 KB Output is correct
26 Correct 76 ms 26700 KB Output is correct
27 Correct 124 ms 25188 KB Output is correct
28 Correct 173 ms 36692 KB Output is correct
29 Correct 186 ms 36208 KB Output is correct
30 Correct 123 ms 26260 KB Output is correct
31 Correct 111 ms 25852 KB Output is correct
32 Correct 131 ms 25932 KB Output is correct
33 Correct 156 ms 24944 KB Output is correct
34 Correct 119 ms 25112 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 13916 KB Output is correct
2 Correct 3 ms 13916 KB Output is correct
3 Correct 2 ms 13916 KB Output is correct
4 Correct 2 ms 13916 KB Output is correct
5 Correct 3 ms 13916 KB Output is correct
6 Correct 2 ms 13916 KB Output is correct
7 Correct 3 ms 13916 KB Output is correct
8 Correct 4 ms 13912 KB Output is correct
9 Correct 3 ms 14168 KB Output is correct
10 Correct 2 ms 13916 KB Output is correct
11 Correct 3 ms 13916 KB Output is correct
12 Correct 3 ms 13916 KB Output is correct
13 Correct 3 ms 13916 KB Output is correct
14 Correct 2 ms 13916 KB Output is correct
15 Correct 3 ms 13916 KB Output is correct
16 Correct 3 ms 13916 KB Output is correct
17 Correct 3 ms 13964 KB Output is correct
18 Correct 3 ms 13916 KB Output is correct
19 Correct 3 ms 13916 KB Output is correct
20 Correct 3 ms 13916 KB Output is correct
21 Correct 3 ms 14168 KB Output is correct
22 Correct 4 ms 17500 KB Output is correct
23 Correct 5 ms 17500 KB Output is correct
24 Correct 5 ms 17756 KB Output is correct
25 Correct 4 ms 16728 KB Output is correct
26 Correct 4 ms 16476 KB Output is correct
27 Correct 4 ms 15964 KB Output is correct
28 Correct 4 ms 14012 KB Output is correct
29 Correct 4 ms 16476 KB Output is correct
30 Correct 4 ms 16476 KB Output is correct
31 Correct 5 ms 16988 KB Output is correct
32 Correct 4 ms 16984 KB Output is correct
33 Correct 4 ms 17756 KB Output is correct
34 Correct 4 ms 17244 KB Output is correct
35 Correct 4 ms 18012 KB Output is correct
36 Correct 5 ms 17936 KB Output is correct
37 Correct 5 ms 16988 KB Output is correct
38 Correct 4 ms 16368 KB Output is correct
39 Correct 95 ms 20052 KB Output is correct
40 Correct 101 ms 21140 KB Output is correct
41 Correct 90 ms 20816 KB Output is correct
42 Correct 83 ms 21480 KB Output is correct
43 Correct 57 ms 21624 KB Output is correct
44 Correct 42 ms 21252 KB Output is correct
45 Correct 91 ms 23888 KB Output is correct
46 Correct 76 ms 26700 KB Output is correct
47 Correct 124 ms 25188 KB Output is correct
48 Correct 173 ms 36692 KB Output is correct
49 Correct 186 ms 36208 KB Output is correct
50 Correct 123 ms 26260 KB Output is correct
51 Correct 111 ms 25852 KB Output is correct
52 Correct 131 ms 25932 KB Output is correct
53 Correct 156 ms 24944 KB Output is correct
54 Correct 119 ms 25112 KB Output is correct
55 Correct 9 ms 14428 KB Output is correct
56 Correct 12 ms 14340 KB Output is correct
57 Correct 61 ms 20572 KB Output is correct
58 Correct 28 ms 21188 KB Output is correct
59 Correct 81 ms 27220 KB Output is correct
60 Correct 358 ms 41572 KB Output is correct
61 Correct 149 ms 25368 KB Output is correct
62 Correct 138 ms 27876 KB Output is correct
63 Correct 205 ms 27952 KB Output is correct
64 Correct 339 ms 28232 KB Output is correct
65 Correct 131 ms 26708 KB Output is correct
66 Correct 299 ms 36180 KB Output is correct
67 Correct 79 ms 29388 KB Output is correct
68 Correct 171 ms 30272 KB Output is correct
69 Correct 173 ms 28116 KB Output is correct
70 Correct 194 ms 30256 KB Output is correct