Submission #418702

# Submission time Handle Problem Language Result Execution time Memory
418702 2021-06-05T18:50:22 Z snasibov05 Race (IOI11_race) C++14
100 / 100
863 ms 47224 KB
#include "race.h"
#include <vector>
#include <unordered_map>

using namespace std;

#define oo 1000000000
#define pii pair<int, int>
#define pb push_back
#define f first
#define s second

int ans = oo;
const int nmax = 2e5 + 5;
vector<pii> ed[nmax];
bool used[nmax];
int size[nmax];
vector<pii> v;

void calcSize(int cur, int pr){
    size[cur] = 1;
    for (auto [x, w] : ed[cur]){
        if (used[x] || x == pr) continue;
        calcSize(x, cur);
        size[cur] += size[x];
    }
}

int findCentroid(int cur, int pr, int n){
    for (auto [x, w] : ed[cur]){
        if (used[x] || x == pr) continue;
        if (size[x] > n / 2){
            return findCentroid(x, cur, n);
        }
    }
    return cur;
}

void dfs(int cur, int pr, int k, int d, int l){
    if (d > k) return;
    v.pb({d, l});
    for (auto [x, w] : ed[cur]){
        if (x == pr || used[x]) continue;
        dfs(x, cur, k, d + w, l + 1);
    }
}

void calc(int cur, int k){
    calcSize(cur, -1);
    cur = findCentroid(cur, -1, size[cur]);

    unordered_map<int, int> dp;
    used[cur] = true;

    for (auto [x, w] : ed[cur]){
        if (used[x]) continue;
        v.clear();
        dfs(x, cur, k, w, 1);

        for (auto [d, l] : v){
            if (d == k) ans = min(ans, l);
            if (dp.count(k-d) == 0) continue;
            ans = min(ans, l + dp[k - d]);
        }
        for (auto [d, l] : v){
            if (dp[d] == 0) dp[d] = l;
            else dp[d] = min(dp[d], l);
        }
    }

    for (auto [x, w] : ed[cur]){
        if (used[x]) continue;
        calc(x, k);
    }
}

int best_path(int n, int k, int h[][2], int l[]){
    for (int i = 0; i < n - 1; ++i) {
        ed[h[i][0]].pb({h[i][1], l[i]});
        ed[h[i][1]].pb({h[i][0], l[i]});
    }

    calc(0, k);

    if (ans == oo) ans = -1;

    return ans;

}

Compilation message

race.cpp: In function 'void calcSize(int, int)':
race.cpp:22:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   22 |     for (auto [x, w] : ed[cur]){
      |               ^
race.cpp: In function 'int findCentroid(int, int, int)':
race.cpp:30:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   30 |     for (auto [x, w] : ed[cur]){
      |               ^
race.cpp: In function 'void dfs(int, int, int, int, int)':
race.cpp:42:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   42 |     for (auto [x, w] : ed[cur]){
      |               ^
race.cpp: In function 'void calc(int, int)':
race.cpp:55:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   55 |     for (auto [x, w] : ed[cur]){
      |               ^
race.cpp:60:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   60 |         for (auto [d, l] : v){
      |                   ^
race.cpp:65:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   65 |         for (auto [d, l] : v){
      |                   ^
race.cpp:71:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   71 |     for (auto [x, w] : ed[cur]){
      |               ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4984 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4984 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 3 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 4 ms 5068 KB Output is correct
22 Correct 5 ms 5068 KB Output is correct
23 Correct 4 ms 5068 KB Output is correct
24 Correct 4 ms 5068 KB Output is correct
25 Correct 4 ms 5068 KB Output is correct
26 Correct 4 ms 4940 KB Output is correct
27 Correct 3 ms 4940 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 4 ms 5116 KB Output is correct
30 Correct 4 ms 5068 KB Output is correct
31 Correct 5 ms 5128 KB Output is correct
32 Correct 5 ms 5108 KB Output is correct
33 Correct 4 ms 5068 KB Output is correct
34 Correct 4 ms 5068 KB Output is correct
35 Correct 4 ms 5068 KB Output is correct
36 Correct 4 ms 5068 KB Output is correct
37 Correct 4 ms 5068 KB Output is correct
38 Correct 4 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4984 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 180 ms 10412 KB Output is correct
20 Correct 165 ms 10308 KB Output is correct
21 Correct 182 ms 10596 KB Output is correct
22 Correct 168 ms 10676 KB Output is correct
23 Correct 114 ms 10688 KB Output is correct
24 Correct 80 ms 10372 KB Output is correct
25 Correct 168 ms 13892 KB Output is correct
26 Correct 161 ms 18120 KB Output is correct
27 Correct 251 ms 15916 KB Output is correct
28 Correct 338 ms 30020 KB Output is correct
29 Correct 328 ms 28940 KB Output is correct
30 Correct 236 ms 15808 KB Output is correct
31 Correct 237 ms 15828 KB Output is correct
32 Correct 300 ms 15936 KB Output is correct
33 Correct 310 ms 14792 KB Output is correct
34 Correct 199 ms 14732 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 3 ms 4940 KB Output is correct
4 Correct 4 ms 4940 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4984 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 3 ms 4940 KB Output is correct
16 Correct 3 ms 4940 KB Output is correct
17 Correct 3 ms 4940 KB Output is correct
18 Correct 3 ms 4940 KB Output is correct
19 Correct 3 ms 4940 KB Output is correct
20 Correct 3 ms 4940 KB Output is correct
21 Correct 4 ms 5068 KB Output is correct
22 Correct 5 ms 5068 KB Output is correct
23 Correct 4 ms 5068 KB Output is correct
24 Correct 4 ms 5068 KB Output is correct
25 Correct 4 ms 5068 KB Output is correct
26 Correct 4 ms 4940 KB Output is correct
27 Correct 3 ms 4940 KB Output is correct
28 Correct 4 ms 5068 KB Output is correct
29 Correct 4 ms 5116 KB Output is correct
30 Correct 4 ms 5068 KB Output is correct
31 Correct 5 ms 5128 KB Output is correct
32 Correct 5 ms 5108 KB Output is correct
33 Correct 4 ms 5068 KB Output is correct
34 Correct 4 ms 5068 KB Output is correct
35 Correct 4 ms 5068 KB Output is correct
36 Correct 4 ms 5068 KB Output is correct
37 Correct 4 ms 5068 KB Output is correct
38 Correct 4 ms 5120 KB Output is correct
39 Correct 180 ms 10412 KB Output is correct
40 Correct 165 ms 10308 KB Output is correct
41 Correct 182 ms 10596 KB Output is correct
42 Correct 168 ms 10676 KB Output is correct
43 Correct 114 ms 10688 KB Output is correct
44 Correct 80 ms 10372 KB Output is correct
45 Correct 168 ms 13892 KB Output is correct
46 Correct 161 ms 18120 KB Output is correct
47 Correct 251 ms 15916 KB Output is correct
48 Correct 338 ms 30020 KB Output is correct
49 Correct 328 ms 28940 KB Output is correct
50 Correct 236 ms 15808 KB Output is correct
51 Correct 237 ms 15828 KB Output is correct
52 Correct 300 ms 15936 KB Output is correct
53 Correct 310 ms 14792 KB Output is correct
54 Correct 199 ms 14732 KB Output is correct
55 Correct 18 ms 5844 KB Output is correct
56 Correct 16 ms 5636 KB Output is correct
57 Correct 148 ms 11060 KB Output is correct
58 Correct 46 ms 10480 KB Output is correct
59 Correct 270 ms 24184 KB Output is correct
60 Correct 863 ms 47224 KB Output is correct
61 Correct 309 ms 16196 KB Output is correct
62 Correct 314 ms 15968 KB Output is correct
63 Correct 389 ms 16092 KB Output is correct
64 Correct 731 ms 26440 KB Output is correct
65 Correct 306 ms 15908 KB Output is correct
66 Correct 641 ms 27596 KB Output is correct
67 Correct 140 ms 16144 KB Output is correct
68 Correct 391 ms 21964 KB Output is correct
69 Correct 414 ms 22084 KB Output is correct
70 Correct 383 ms 21588 KB Output is correct