Submission #318297

# Submission time Handle Problem Language Result Execution time Memory
318297 2020-11-01T02:41:12 Z tjdgus4384 Race (IOI11_race) C++14
100 / 100
779 ms 49624 KB
#include "race.h"
#include<bits/stdc++.h>
using namespace std;
vector<int> v[200001], c[200001], chk1, chk2;
bool visited[200001];
int sz[200001], ans = 1000000000, n, k;
int mindep[1000001], newdep[1000001];

int getSize(int x, int y){
    sz[x] = 1;
    for(int i = 0;i < v[x].size();i++){
        if(v[x][i] != y && !visited[v[x][i]]) sz[x] += getSize(v[x][i], x);
    }
    return sz[x];
}

int getCentroid(int x, int y, int z){
    for(int i = 0;i < v[x].size();i++){
        if(v[x][i] != y && sz[v[x][i]]*2 > z && !visited[v[x][i]]) return getCentroid(v[x][i], x, z);
    }
    return x;
}

void dfs(int x, int y, int dep, int cnt){
    if(dep == k) ans = min(ans, cnt);
    if(dep > k) return;
    if(mindep[k-dep] != 1e9) ans = min(ans, cnt + mindep[k-dep]);
    newdep[dep] = min(newdep[dep], cnt);
    chk1.push_back(dep);
    chk2.push_back(dep);
    for(int i = 0;i < v[x].size();i++){
        if(v[x][i] != y && !visited[v[x][i]]) dfs(v[x][i], x, dep+c[x][i], cnt+1);
    }
}

void dnc(int x){
    int cent = getCentroid(x, -1, getSize(x, -1));
    for(int i = 0;i < v[cent].size();i++){
        if(!visited[v[cent][i]]){
            dfs(v[cent][i], cent, c[cent][i], 1);
            for(int j = 0;j < chk1.size();j++) mindep[chk1[j]] = min(mindep[chk1[j]], newdep[chk1[j]]);
            chk1.clear();
        }
    }
    for(int j = 0;j < chk2.size();j++) mindep[chk2[j]] = newdep[chk2[j]] = 1e9;
    chk2.clear();
    visited[cent] = true;
    for(int i = 0;i < v[cent].size();i++) if(!visited[v[cent][i]]) dnc(v[cent][i]);
}

int best_path(int N, int K, int H[][2], int L[]){
    n = N, k = K;
    for(int i = 0;i < N-1;i++){
        v[H[i][0]].push_back(H[i][1]);
        c[H[i][0]].push_back(L[i]);
        v[H[i][1]].push_back(H[i][0]);
        c[H[i][1]].push_back(L[i]);
    }
    for(int i = 0;i <= K;i++) mindep[i] = newdep[i] = 1e9;
    dnc(0);
    return ans == 1e9 ? -1 : ans;
}

Compilation message

race.cpp: In function 'int getSize(int, int)':
race.cpp:11:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   11 |     for(int i = 0;i < v[x].size();i++){
      |                   ~~^~~~~~~~~~~~~
race.cpp: In function 'int getCentroid(int, int, int)':
race.cpp:18:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   18 |     for(int i = 0;i < v[x].size();i++){
      |                   ~~^~~~~~~~~~~~~
race.cpp: In function 'void dfs(int, int, int, int)':
race.cpp:31:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   31 |     for(int i = 0;i < v[x].size();i++){
      |                   ~~^~~~~~~~~~~~~
race.cpp: In function 'void dnc(int)':
race.cpp:38:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   38 |     for(int i = 0;i < v[cent].size();i++){
      |                   ~~^~~~~~~~~~~~~~~~
race.cpp:41:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   41 |             for(int j = 0;j < chk1.size();j++) mindep[chk1[j]] = min(mindep[chk1[j]], newdep[chk1[j]]);
      |                           ~~^~~~~~~~~~~~~
race.cpp:45:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   45 |     for(int j = 0;j < chk2.size();j++) mindep[chk2[j]] = newdep[chk2[j]] = 1e9;
      |                   ~~^~~~~~~~~~~~~
race.cpp:48:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   48 |     for(int i = 0;i < v[cent].size();i++) if(!visited[v[cent][i]]) dnc(v[cent][i]);
      |                   ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9708 KB Output is correct
2 Correct 7 ms 9708 KB Output is correct
3 Correct 7 ms 9708 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Correct 7 ms 9708 KB Output is correct
7 Correct 7 ms 9708 KB Output is correct
8 Correct 7 ms 9708 KB Output is correct
9 Correct 7 ms 9708 KB Output is correct
10 Correct 7 ms 9836 KB Output is correct
11 Correct 7 ms 9708 KB Output is correct
12 Correct 7 ms 9708 KB Output is correct
13 Correct 7 ms 9708 KB Output is correct
14 Correct 8 ms 9708 KB Output is correct
15 Correct 7 ms 9708 KB Output is correct
16 Correct 8 ms 9708 KB Output is correct
17 Correct 7 ms 9708 KB Output is correct
18 Correct 7 ms 9708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9708 KB Output is correct
2 Correct 7 ms 9708 KB Output is correct
3 Correct 7 ms 9708 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Correct 7 ms 9708 KB Output is correct
7 Correct 7 ms 9708 KB Output is correct
8 Correct 7 ms 9708 KB Output is correct
9 Correct 7 ms 9708 KB Output is correct
10 Correct 7 ms 9836 KB Output is correct
11 Correct 7 ms 9708 KB Output is correct
12 Correct 7 ms 9708 KB Output is correct
13 Correct 7 ms 9708 KB Output is correct
14 Correct 8 ms 9708 KB Output is correct
15 Correct 7 ms 9708 KB Output is correct
16 Correct 8 ms 9708 KB Output is correct
17 Correct 7 ms 9708 KB Output is correct
18 Correct 7 ms 9708 KB Output is correct
19 Correct 7 ms 9708 KB Output is correct
20 Correct 7 ms 9708 KB Output is correct
21 Correct 8 ms 9836 KB Output is correct
22 Correct 13 ms 17004 KB Output is correct
23 Correct 12 ms 15724 KB Output is correct
24 Correct 12 ms 16768 KB Output is correct
25 Correct 13 ms 16492 KB Output is correct
26 Correct 11 ms 12524 KB Output is correct
27 Correct 12 ms 16108 KB Output is correct
28 Correct 9 ms 11500 KB Output is correct
29 Correct 9 ms 12396 KB Output is correct
30 Correct 10 ms 12800 KB Output is correct
31 Correct 11 ms 14956 KB Output is correct
32 Correct 12 ms 15468 KB Output is correct
33 Correct 12 ms 15980 KB Output is correct
34 Correct 12 ms 14700 KB Output is correct
35 Correct 12 ms 16236 KB Output is correct
36 Correct 13 ms 17132 KB Output is correct
37 Correct 13 ms 16236 KB Output is correct
38 Correct 10 ms 13824 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9708 KB Output is correct
2 Correct 7 ms 9708 KB Output is correct
3 Correct 7 ms 9708 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Correct 7 ms 9708 KB Output is correct
7 Correct 7 ms 9708 KB Output is correct
8 Correct 7 ms 9708 KB Output is correct
9 Correct 7 ms 9708 KB Output is correct
10 Correct 7 ms 9836 KB Output is correct
11 Correct 7 ms 9708 KB Output is correct
12 Correct 7 ms 9708 KB Output is correct
13 Correct 7 ms 9708 KB Output is correct
14 Correct 8 ms 9708 KB Output is correct
15 Correct 7 ms 9708 KB Output is correct
16 Correct 8 ms 9708 KB Output is correct
17 Correct 7 ms 9708 KB Output is correct
18 Correct 7 ms 9708 KB Output is correct
19 Correct 209 ms 19556 KB Output is correct
20 Correct 203 ms 19300 KB Output is correct
21 Correct 222 ms 19576 KB Output is correct
22 Correct 245 ms 19984 KB Output is correct
23 Correct 132 ms 19172 KB Output is correct
24 Correct 101 ms 19556 KB Output is correct
25 Correct 191 ms 22496 KB Output is correct
26 Correct 123 ms 26080 KB Output is correct
27 Correct 314 ms 29796 KB Output is correct
28 Correct 413 ms 41312 KB Output is correct
29 Correct 407 ms 40292 KB Output is correct
30 Correct 313 ms 29792 KB Output is correct
31 Correct 311 ms 29792 KB Output is correct
32 Correct 387 ms 29924 KB Output is correct
33 Correct 364 ms 28644 KB Output is correct
34 Correct 310 ms 29556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9708 KB Output is correct
2 Correct 7 ms 9708 KB Output is correct
3 Correct 7 ms 9708 KB Output is correct
4 Correct 7 ms 9708 KB Output is correct
5 Correct 7 ms 9708 KB Output is correct
6 Correct 7 ms 9708 KB Output is correct
7 Correct 7 ms 9708 KB Output is correct
8 Correct 7 ms 9708 KB Output is correct
9 Correct 7 ms 9708 KB Output is correct
10 Correct 7 ms 9836 KB Output is correct
11 Correct 7 ms 9708 KB Output is correct
12 Correct 7 ms 9708 KB Output is correct
13 Correct 7 ms 9708 KB Output is correct
14 Correct 8 ms 9708 KB Output is correct
15 Correct 7 ms 9708 KB Output is correct
16 Correct 8 ms 9708 KB Output is correct
17 Correct 7 ms 9708 KB Output is correct
18 Correct 7 ms 9708 KB Output is correct
19 Correct 7 ms 9708 KB Output is correct
20 Correct 7 ms 9708 KB Output is correct
21 Correct 8 ms 9836 KB Output is correct
22 Correct 13 ms 17004 KB Output is correct
23 Correct 12 ms 15724 KB Output is correct
24 Correct 12 ms 16768 KB Output is correct
25 Correct 13 ms 16492 KB Output is correct
26 Correct 11 ms 12524 KB Output is correct
27 Correct 12 ms 16108 KB Output is correct
28 Correct 9 ms 11500 KB Output is correct
29 Correct 9 ms 12396 KB Output is correct
30 Correct 10 ms 12800 KB Output is correct
31 Correct 11 ms 14956 KB Output is correct
32 Correct 12 ms 15468 KB Output is correct
33 Correct 12 ms 15980 KB Output is correct
34 Correct 12 ms 14700 KB Output is correct
35 Correct 12 ms 16236 KB Output is correct
36 Correct 13 ms 17132 KB Output is correct
37 Correct 13 ms 16236 KB Output is correct
38 Correct 10 ms 13824 KB Output is correct
39 Correct 209 ms 19556 KB Output is correct
40 Correct 203 ms 19300 KB Output is correct
41 Correct 222 ms 19576 KB Output is correct
42 Correct 245 ms 19984 KB Output is correct
43 Correct 132 ms 19172 KB Output is correct
44 Correct 101 ms 19556 KB Output is correct
45 Correct 191 ms 22496 KB Output is correct
46 Correct 123 ms 26080 KB Output is correct
47 Correct 314 ms 29796 KB Output is correct
48 Correct 413 ms 41312 KB Output is correct
49 Correct 407 ms 40292 KB Output is correct
50 Correct 313 ms 29792 KB Output is correct
51 Correct 311 ms 29792 KB Output is correct
52 Correct 387 ms 29924 KB Output is correct
53 Correct 364 ms 28644 KB Output is correct
54 Correct 310 ms 29556 KB Output is correct
55 Correct 16 ms 10860 KB Output is correct
56 Correct 17 ms 10732 KB Output is correct
57 Correct 112 ms 19808 KB Output is correct
58 Correct 53 ms 20180 KB Output is correct
59 Correct 119 ms 27692 KB Output is correct
60 Correct 623 ms 49624 KB Output is correct
61 Correct 329 ms 30184 KB Output is correct
62 Correct 347 ms 38632 KB Output is correct
63 Correct 452 ms 38628 KB Output is correct
64 Correct 779 ms 34568 KB Output is correct
65 Correct 414 ms 29924 KB Output is correct
66 Correct 583 ms 46308 KB Output is correct
67 Correct 225 ms 39732 KB Output is correct
68 Correct 437 ms 38768 KB Output is correct
69 Correct 457 ms 39024 KB Output is correct
70 Correct 419 ms 37856 KB Output is correct