답안 #1067149

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1067149 2024-08-20T12:04:37 Z TrinhKhanhDung 경주 (Race) (IOI11_race) C++14
100 / 100
297 ms 37204 KB
#include <bits/stdc++.h>
#include "race.h"
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(1e9 + 19972207)

using namespace std;

template<class T1, class T2>
    bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}

template<class T1, class T2>
    bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}

template<class T1, class T2>
    void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}

template<class T1, class T2>
    void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}

template<class T>
    void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}

const int MAX = 2e5 + 10, MAXVAL = 1e6 + 10;

int N, K;
vector<pair<int, int>> adj[MAX];
int nChild[MAX], ans = INF, dp[MAXVAL];
bool isDel[MAX];

void input(){
    cin >> N >> K;
    for(int i=1; i<N; i++){
        int u, v, c;
        cin >> u >> v >> c;
        adj[u].push_back(make_pair(v, c));
        adj[v].push_back(make_pair(u, c));
    }
}

void countChild(int u, int p){
    nChild[u] = 1;
    for(auto o: adj[u]){
        int v = o.fi;
        if(v != p && !isDel[v]){
            countChild(v, u);
            nChild[u] += nChild[v];
        }
    }
}

int centroid(int u, int p, int n){
    for(auto o: adj[u]){
        int v = o.fi;
        if(v != p && !isDel[v]){
            if(nChild[v] * 2 > n) return centroid(v, u, n);
        }
    }
    return u;
}

void dfs(int u, int p, int dist, int height, bool filling){

    if(dist > K) return;
    if(!filling){
        minimize(ans, dp[K - dist] + height);
    }
    else{
        minimize(dp[dist], height);
    }
    for(auto o: adj[u]){
        int v = o.fi;
        int newDist = dist + o.se;
        if(v != p && !isDel[v])
            dfs(v, u, newDist, height + 1, filling);
    }
}

void reset(int u, int p, int dist){
    if(dist > K) return;
    dp[dist] = INF;
    for(auto o: adj[u]){
        int v = o.fi;
        int newDist = dist + o.se;
        if(v != p && !isDel[v])
            reset(v, u, newDist);
    }
}

void calc(int ver){
    countChild(ver, 0);
    int u = centroid(ver, 0, nChild[ver]);
    
    dp[0] = 0;
    for(auto o: adj[u]){
        int v = o.fi;
        int dist = o.se;
        if(!isDel[v]){
            dfs(v, u, dist, 1, 0);
            dfs(v, u, dist, 1, 1);
        }
    }
    reset(u, 0, 0);

    isDel[u] = true;
    for(auto o: adj[u]){
        int v = o.fi;
        if(!isDel[v]) calc(v);
    }
}

int best_path(int n, int k, int H[][2], int L[]){
    N = n;
    K = k;
    for(int i=0; i+1<N; i++){
        int u = H[i][0];
        int v = H[i][1];
        int c = L[i];
        adj[u].push_back(make_pair(v, c));
        adj[v].push_back(make_pair(u, c));
    }
    memset(dp, 0x3f, sizeof dp);  
    calc(0); 
    return (ans >= INF ? -1 : ans);
}

// void solve(){
    
// }

// int main(){
//     ios_base::sync_with_stdio(0); cin.tie(0);
// //    freopen("mmgraph.inp", "r", stdin);
// //    freopen("mmgraph.out", "w", stdout);

//     int t = 1;
// //    cin >> t;
//     while(t--){
//         input();
//         solve();
//     }

//     return 0;
// }
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9048 KB Output is correct
2 Correct 4 ms 9048 KB Output is correct
3 Correct 3 ms 8840 KB Output is correct
4 Correct 3 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 3 ms 9052 KB Output is correct
11 Correct 3 ms 9052 KB Output is correct
12 Correct 4 ms 9048 KB Output is correct
13 Correct 4 ms 8984 KB Output is correct
14 Correct 4 ms 9304 KB Output is correct
15 Correct 3 ms 9052 KB Output is correct
16 Correct 3 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 9048 KB Output is correct
2 Correct 4 ms 9048 KB Output is correct
3 Correct 3 ms 8840 KB Output is correct
4 Correct 3 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 3 ms 9052 KB Output is correct
11 Correct 3 ms 9052 KB Output is correct
12 Correct 4 ms 9048 KB Output is correct
13 Correct 4 ms 8984 KB Output is correct
14 Correct 4 ms 9304 KB Output is correct
15 Correct 3 ms 9052 KB Output is correct
16 Correct 3 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 4 ms 9052 KB Output is correct
20 Correct 4 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 5 ms 9052 KB Output is correct
26 Correct 5 ms 9052 KB Output is correct
27 Correct 4 ms 9052 KB Output is correct
28 Correct 4 ms 9144 KB Output is correct
29 Correct 4 ms 9052 KB Output is correct
30 Correct 4 ms 9052 KB Output is correct
31 Correct 4 ms 9052 KB Output is correct
32 Correct 4 ms 8992 KB Output is correct
33 Correct 5 ms 9020 KB Output is correct
34 Correct 4 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 9048 KB Output is correct
2 Correct 4 ms 9048 KB Output is correct
3 Correct 3 ms 8840 KB Output is correct
4 Correct 3 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 3 ms 9052 KB Output is correct
11 Correct 3 ms 9052 KB Output is correct
12 Correct 4 ms 9048 KB Output is correct
13 Correct 4 ms 8984 KB Output is correct
14 Correct 4 ms 9304 KB Output is correct
15 Correct 3 ms 9052 KB Output is correct
16 Correct 3 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 88 ms 15700 KB Output is correct
20 Correct 86 ms 15700 KB Output is correct
21 Correct 94 ms 15636 KB Output is correct
22 Correct 81 ms 15708 KB Output is correct
23 Correct 45 ms 16064 KB Output is correct
24 Correct 38 ms 15964 KB Output is correct
25 Correct 95 ms 19312 KB Output is correct
26 Correct 70 ms 22868 KB Output is correct
27 Correct 94 ms 22612 KB Output is correct
28 Correct 148 ms 37204 KB Output is correct
29 Correct 153 ms 35972 KB Output is correct
30 Correct 94 ms 22820 KB Output is correct
31 Correct 99 ms 22680 KB Output is correct
32 Correct 109 ms 22868 KB Output is correct
33 Correct 140 ms 21588 KB Output is correct
34 Correct 106 ms 22388 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9048 KB Output is correct
2 Correct 4 ms 9048 KB Output is correct
3 Correct 3 ms 8840 KB Output is correct
4 Correct 3 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 3 ms 9052 KB Output is correct
11 Correct 3 ms 9052 KB Output is correct
12 Correct 4 ms 9048 KB Output is correct
13 Correct 4 ms 8984 KB Output is correct
14 Correct 4 ms 9304 KB Output is correct
15 Correct 3 ms 9052 KB Output is correct
16 Correct 3 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 4 ms 9052 KB Output is correct
20 Correct 4 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 5 ms 9052 KB Output is correct
26 Correct 5 ms 9052 KB Output is correct
27 Correct 4 ms 9052 KB Output is correct
28 Correct 4 ms 9144 KB Output is correct
29 Correct 4 ms 9052 KB Output is correct
30 Correct 4 ms 9052 KB Output is correct
31 Correct 4 ms 9052 KB Output is correct
32 Correct 4 ms 8992 KB Output is correct
33 Correct 5 ms 9020 KB Output is correct
34 Correct 4 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 88 ms 15700 KB Output is correct
40 Correct 86 ms 15700 KB Output is correct
41 Correct 94 ms 15636 KB Output is correct
42 Correct 81 ms 15708 KB Output is correct
43 Correct 45 ms 16064 KB Output is correct
44 Correct 38 ms 15964 KB Output is correct
45 Correct 95 ms 19312 KB Output is correct
46 Correct 70 ms 22868 KB Output is correct
47 Correct 94 ms 22612 KB Output is correct
48 Correct 148 ms 37204 KB Output is correct
49 Correct 153 ms 35972 KB Output is correct
50 Correct 94 ms 22820 KB Output is correct
51 Correct 99 ms 22680 KB Output is correct
52 Correct 109 ms 22868 KB Output is correct
53 Correct 140 ms 21588 KB Output is correct
54 Correct 106 ms 22388 KB Output is correct
55 Correct 9 ms 9564 KB Output is correct
56 Correct 10 ms 9504 KB Output is correct
57 Correct 50 ms 15864 KB Output is correct
58 Correct 24 ms 15652 KB Output is correct
59 Correct 74 ms 22960 KB Output is correct
60 Correct 297 ms 36180 KB Output is correct
61 Correct 104 ms 22936 KB Output is correct
62 Correct 117 ms 22720 KB Output is correct
63 Correct 147 ms 22864 KB Output is correct
64 Correct 282 ms 22940 KB Output is correct
65 Correct 105 ms 23636 KB Output is correct
66 Correct 250 ms 33520 KB Output is correct
67 Correct 62 ms 23492 KB Output is correct
68 Correct 142 ms 23592 KB Output is correct
69 Correct 136 ms 23832 KB Output is correct
70 Correct 132 ms 22864 KB Output is correct