답안 #339255

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
339255 2020-12-25T00:47:45 Z rocks03 경주 (Race) (IOI11_race) C++14
100 / 100
1783 ms 59956 KB
//#pragma GCC target("avx2")
//#pragma GCC optimization("O3")
//#pragma GCC optimization("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define pb push_back
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(), x.end()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MAXN = 5e5+100;
int K, sz[MAXN];
vector<pii> g[MAXN];
bool vis[MAXN];

void centroid(int v, int p, int csz, int& ans){
    sz[v] = 1; bool is = true;
    for(auto [u, w] : g[v]){
        if(u == p || vis[u]) continue;
        centroid(u, v, csz, ans);
        sz[v] += sz[u];
        if(sz[u] * 2 > csz){
            is = false;
        }
    }
    if((csz - sz[v]) * 2 <= csz && is){
        ans = v;
    }
}

void dfs(int v, int p, int d, int len, map<int, int>& m){
    if(d > K) return;
    if(!m.count(d) || m[d] > len){
        m[d] = len;
    }
    for(auto [u, w] : g[v]){
        if(u == p || vis[u])
            continue;
        dfs(u, v, d + w, len + 1, m);
    }
}

int solve(int v, int p, int csz){
    centroid(v, p, csz, v);
    int ans = INT_MAX;
    map<int, int> m;
    m[0] = 0;
    for(int i = 0; i < SZ(g[v]); i++){
        int u = g[v][i].ff, w = g[v][i].ss;
        if(u == p || vis[u]) continue;
        map<int, int> mch;
        dfs(u, v, w, 1, mch);
        for(auto [value, x] : mch){
            if(m.count(K - value)){
                ans = min(ans, m[K - value] + x);
            }
        }
        for(auto [value, x] : mch){
            if(!m.count(value) || m[value] > x){
                m[value] = x;
            }
        }
    }
    vis[v] = true;
    for(auto [u, w] : g[v]){
        if(u == p || vis[u])
            continue;
        ans = min(ans, solve(u, v, sz[u]));
    }
    return ans;
}

int best_path(int N, int K, int H[][2], int L[]){
    :: K = K;
    for(int i = 0; i < N - 1; i++){
        int u = H[i][0], v = H[i][1];
        g[u].pb({v, L[i]});
        g[v].pb({u, L[i]});
    }
    int ans = solve(0, -1, N);
    if(ans == INT_MAX){
        ans = -1;
    }
    return ans;
}

Compilation message

race.cpp: In function 'void centroid(int, int, int, int&)':
race.cpp:23:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   23 |     for(auto [u, w] : g[v]){
      |              ^
race.cpp: In function 'void dfs(int, int, int, int, std::map<int, int>&)':
race.cpp:41:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   41 |     for(auto [u, w] : g[v]){
      |              ^
race.cpp: In function 'int solve(int, int, int)':
race.cpp:58:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   58 |         for(auto [value, x] : mch){
      |                  ^
race.cpp:63:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   63 |         for(auto [value, x] : mch){
      |                  ^
race.cpp:70:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   70 |     for(auto [u, w] : g[v]){
      |              ^
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12140 KB Output is correct
2 Correct 7 ms 12140 KB Output is correct
3 Correct 7 ms 12140 KB Output is correct
4 Correct 8 ms 12140 KB Output is correct
5 Correct 7 ms 12140 KB Output is correct
6 Correct 8 ms 12140 KB Output is correct
7 Correct 9 ms 12140 KB Output is correct
8 Correct 8 ms 12140 KB Output is correct
9 Correct 9 ms 12140 KB Output is correct
10 Correct 8 ms 12140 KB Output is correct
11 Correct 8 ms 12140 KB Output is correct
12 Correct 8 ms 12140 KB Output is correct
13 Correct 8 ms 12140 KB Output is correct
14 Correct 7 ms 12140 KB Output is correct
15 Correct 8 ms 12140 KB Output is correct
16 Correct 9 ms 12140 KB Output is correct
17 Correct 7 ms 12140 KB Output is correct
18 Correct 8 ms 12140 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12140 KB Output is correct
2 Correct 7 ms 12140 KB Output is correct
3 Correct 7 ms 12140 KB Output is correct
4 Correct 8 ms 12140 KB Output is correct
5 Correct 7 ms 12140 KB Output is correct
6 Correct 8 ms 12140 KB Output is correct
7 Correct 9 ms 12140 KB Output is correct
8 Correct 8 ms 12140 KB Output is correct
9 Correct 9 ms 12140 KB Output is correct
10 Correct 8 ms 12140 KB Output is correct
11 Correct 8 ms 12140 KB Output is correct
12 Correct 8 ms 12140 KB Output is correct
13 Correct 8 ms 12140 KB Output is correct
14 Correct 7 ms 12140 KB Output is correct
15 Correct 8 ms 12140 KB Output is correct
16 Correct 9 ms 12140 KB Output is correct
17 Correct 7 ms 12140 KB Output is correct
18 Correct 8 ms 12140 KB Output is correct
19 Correct 7 ms 12140 KB Output is correct
20 Correct 10 ms 12140 KB Output is correct
21 Correct 11 ms 12140 KB Output is correct
22 Correct 8 ms 12140 KB Output is correct
23 Correct 9 ms 12140 KB Output is correct
24 Correct 9 ms 12140 KB Output is correct
25 Correct 10 ms 12300 KB Output is correct
26 Correct 16 ms 12140 KB Output is correct
27 Correct 9 ms 12140 KB Output is correct
28 Correct 10 ms 12140 KB Output is correct
29 Correct 11 ms 12268 KB Output is correct
30 Correct 12 ms 12268 KB Output is correct
31 Correct 10 ms 12140 KB Output is correct
32 Correct 10 ms 12288 KB Output is correct
33 Correct 13 ms 12268 KB Output is correct
34 Correct 9 ms 12140 KB Output is correct
35 Correct 9 ms 12140 KB Output is correct
36 Correct 9 ms 12140 KB Output is correct
37 Correct 10 ms 12268 KB Output is correct
38 Correct 10 ms 12268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12140 KB Output is correct
2 Correct 7 ms 12140 KB Output is correct
3 Correct 7 ms 12140 KB Output is correct
4 Correct 8 ms 12140 KB Output is correct
5 Correct 7 ms 12140 KB Output is correct
6 Correct 8 ms 12140 KB Output is correct
7 Correct 9 ms 12140 KB Output is correct
8 Correct 8 ms 12140 KB Output is correct
9 Correct 9 ms 12140 KB Output is correct
10 Correct 8 ms 12140 KB Output is correct
11 Correct 8 ms 12140 KB Output is correct
12 Correct 8 ms 12140 KB Output is correct
13 Correct 8 ms 12140 KB Output is correct
14 Correct 7 ms 12140 KB Output is correct
15 Correct 8 ms 12140 KB Output is correct
16 Correct 9 ms 12140 KB Output is correct
17 Correct 7 ms 12140 KB Output is correct
18 Correct 8 ms 12140 KB Output is correct
19 Correct 294 ms 17516 KB Output is correct
20 Correct 316 ms 17516 KB Output is correct
21 Correct 320 ms 17516 KB Output is correct
22 Correct 299 ms 17516 KB Output is correct
23 Correct 135 ms 17644 KB Output is correct
24 Correct 114 ms 17516 KB Output is correct
25 Correct 181 ms 21928 KB Output is correct
26 Correct 115 ms 26348 KB Output is correct
27 Correct 301 ms 23084 KB Output is correct
28 Correct 388 ms 40636 KB Output is correct
29 Correct 367 ms 39280 KB Output is correct
30 Correct 318 ms 23020 KB Output is correct
31 Correct 315 ms 23020 KB Output is correct
32 Correct 342 ms 23156 KB Output is correct
33 Correct 544 ms 24052 KB Output is correct
34 Correct 277 ms 23788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 12140 KB Output is correct
2 Correct 7 ms 12140 KB Output is correct
3 Correct 7 ms 12140 KB Output is correct
4 Correct 8 ms 12140 KB Output is correct
5 Correct 7 ms 12140 KB Output is correct
6 Correct 8 ms 12140 KB Output is correct
7 Correct 9 ms 12140 KB Output is correct
8 Correct 8 ms 12140 KB Output is correct
9 Correct 9 ms 12140 KB Output is correct
10 Correct 8 ms 12140 KB Output is correct
11 Correct 8 ms 12140 KB Output is correct
12 Correct 8 ms 12140 KB Output is correct
13 Correct 8 ms 12140 KB Output is correct
14 Correct 7 ms 12140 KB Output is correct
15 Correct 8 ms 12140 KB Output is correct
16 Correct 9 ms 12140 KB Output is correct
17 Correct 7 ms 12140 KB Output is correct
18 Correct 8 ms 12140 KB Output is correct
19 Correct 7 ms 12140 KB Output is correct
20 Correct 10 ms 12140 KB Output is correct
21 Correct 11 ms 12140 KB Output is correct
22 Correct 8 ms 12140 KB Output is correct
23 Correct 9 ms 12140 KB Output is correct
24 Correct 9 ms 12140 KB Output is correct
25 Correct 10 ms 12300 KB Output is correct
26 Correct 16 ms 12140 KB Output is correct
27 Correct 9 ms 12140 KB Output is correct
28 Correct 10 ms 12140 KB Output is correct
29 Correct 11 ms 12268 KB Output is correct
30 Correct 12 ms 12268 KB Output is correct
31 Correct 10 ms 12140 KB Output is correct
32 Correct 10 ms 12288 KB Output is correct
33 Correct 13 ms 12268 KB Output is correct
34 Correct 9 ms 12140 KB Output is correct
35 Correct 9 ms 12140 KB Output is correct
36 Correct 9 ms 12140 KB Output is correct
37 Correct 10 ms 12268 KB Output is correct
38 Correct 10 ms 12268 KB Output is correct
39 Correct 294 ms 17516 KB Output is correct
40 Correct 316 ms 17516 KB Output is correct
41 Correct 320 ms 17516 KB Output is correct
42 Correct 299 ms 17516 KB Output is correct
43 Correct 135 ms 17644 KB Output is correct
44 Correct 114 ms 17516 KB Output is correct
45 Correct 181 ms 21928 KB Output is correct
46 Correct 115 ms 26348 KB Output is correct
47 Correct 301 ms 23084 KB Output is correct
48 Correct 388 ms 40636 KB Output is correct
49 Correct 367 ms 39280 KB Output is correct
50 Correct 318 ms 23020 KB Output is correct
51 Correct 315 ms 23020 KB Output is correct
52 Correct 342 ms 23156 KB Output is correct
53 Correct 544 ms 24052 KB Output is correct
54 Correct 277 ms 23788 KB Output is correct
55 Correct 35 ms 13164 KB Output is correct
56 Correct 24 ms 12780 KB Output is correct
57 Correct 177 ms 19052 KB Output is correct
58 Correct 63 ms 18784 KB Output is correct
59 Correct 554 ms 34712 KB Output is correct
60 Correct 1703 ms 59956 KB Output is correct
61 Correct 437 ms 26476 KB Output is correct
62 Correct 487 ms 26320 KB Output is correct
63 Correct 565 ms 26348 KB Output is correct
64 Correct 1783 ms 37688 KB Output is correct
65 Correct 362 ms 26864 KB Output is correct
66 Correct 1084 ms 40608 KB Output is correct
67 Correct 203 ms 26716 KB Output is correct
68 Correct 651 ms 33548 KB Output is correct
69 Correct 652 ms 33900 KB Output is correct
70 Correct 580 ms 33004 KB Output is correct