Submission #97859

# Submission time Handle Problem Language Result Execution time Memory
97859 2019-02-19T03:32:31 Z kitsu_hi Race (IOI11_race) C++14
100 / 100
2225 ms 44700 KB
#include "race.h"
#include<bits/stdc++.h>
using namespace std;

vector< vector< pair<int, int> > > edges;
int size_of_subtree[200005], blocked[200005];
map <int, int> big, small;
int answer = INT_MAX;

void get_size_of_subtree(int current_node, int parent) {
  size_of_subtree[current_node] = 1;
  for (auto child : edges[current_node]) {
    if (child.first != parent) {
      get_size_of_subtree(child.first, current_node);
      size_of_subtree[current_node] += size_of_subtree[child.first];
    } 
  }
}

int get_centroid(int current_node) {
  for (auto child : edges[current_node]) {
    if (blocked[child.first] == 0 && size_of_subtree[child.first] > size_of_subtree[current_node] / 2) {
      int total_size = size_of_subtree[current_node];
      size_of_subtree[current_node] -= size_of_subtree[child.first];
      size_of_subtree[child.first] = total_size;
      return get_centroid(child.first); 
    }
  }
  return current_node;
}

void dfs(int current_node, int parent, int sum, int depth) {
  if (small.find(sum) == small.end()) small[sum] = depth;
  else small[sum] = min(small[sum], depth);
  for (auto child : edges[current_node]) {
    if (blocked[child.first] == 0 && child.first != parent) {
      dfs(child.first, current_node, sum + child.second, depth + 1);
    }
  }
}

void solve(int current_node, int sum) {
  big.clear();
  current_node = get_centroid(current_node);
  blocked[current_node] = 1;
  big[0] = 0;
  for (auto child : edges[current_node]) {
    small.clear();
    if (blocked[child.first] == 1) continue;
    dfs(child.first, current_node, child.second, 1);
    for (auto value : small) {
      if (big.find(sum - value.first) != big.end()) answer = min(answer, big[sum - value.first] + small[value.first]);
    }
    for (auto value : small) {
      if (big.find(value.first) == big.end()) big[value.first] = small[value.first];
      else big[value.first] = min(big[value.first], small[value.first]);
    }
  }
  for (auto child : edges[current_node]) {
    if (blocked[child.first] == 0) solve(child.first, sum); 
  }
}

int best_path(int N, int K, int H[][2], int L[])
{
  edges.resize(N+1);
  for (int i = 0; i < N - 1; i++) {
    edges[H[i][0]].push_back(make_pair(H[i][1], L[i]));
    edges[H[i][1]].push_back(make_pair(H[i][0], L[i]));
  }
  get_size_of_subtree(0, -1);
  solve(0, K);
  if (answer == INT_MAX) answer = -1;
  return answer;
}

# Verdict Execution time Memory Grader output
1 Correct 3 ms 380 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 380 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
21 Correct 5 ms 512 KB Output is correct
22 Correct 5 ms 512 KB Output is correct
23 Correct 4 ms 512 KB Output is correct
24 Correct 5 ms 512 KB Output is correct
25 Correct 6 ms 512 KB Output is correct
26 Correct 6 ms 512 KB Output is correct
27 Correct 3 ms 484 KB Output is correct
28 Correct 6 ms 512 KB Output is correct
29 Correct 4 ms 512 KB Output is correct
30 Correct 6 ms 512 KB Output is correct
31 Correct 5 ms 512 KB Output is correct
32 Correct 6 ms 512 KB Output is correct
33 Correct 6 ms 512 KB Output is correct
34 Correct 6 ms 512 KB Output is correct
35 Correct 5 ms 512 KB Output is correct
36 Correct 5 ms 512 KB Output is correct
37 Correct 5 ms 512 KB Output is correct
38 Correct 4 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 380 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 396 ms 9712 KB Output is correct
20 Correct 396 ms 9720 KB Output is correct
21 Correct 380 ms 9720 KB Output is correct
22 Correct 356 ms 9848 KB Output is correct
23 Correct 545 ms 10144 KB Output is correct
24 Correct 304 ms 9988 KB Output is correct
25 Correct 305 ms 13688 KB Output is correct
26 Correct 121 ms 15484 KB Output is correct
27 Correct 570 ms 19772 KB Output is correct
28 Correct 2225 ms 44700 KB Output is correct
29 Correct 2151 ms 44024 KB Output is correct
30 Correct 721 ms 19804 KB Output is correct
31 Correct 606 ms 19740 KB Output is correct
32 Correct 835 ms 19884 KB Output is correct
33 Correct 1132 ms 18668 KB Output is correct
34 Correct 2102 ms 30840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 380 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 3 ms 384 KB Output is correct
11 Correct 3 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 384 KB Output is correct
14 Correct 3 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 3 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
21 Correct 5 ms 512 KB Output is correct
22 Correct 5 ms 512 KB Output is correct
23 Correct 4 ms 512 KB Output is correct
24 Correct 5 ms 512 KB Output is correct
25 Correct 6 ms 512 KB Output is correct
26 Correct 6 ms 512 KB Output is correct
27 Correct 3 ms 484 KB Output is correct
28 Correct 6 ms 512 KB Output is correct
29 Correct 4 ms 512 KB Output is correct
30 Correct 6 ms 512 KB Output is correct
31 Correct 5 ms 512 KB Output is correct
32 Correct 6 ms 512 KB Output is correct
33 Correct 6 ms 512 KB Output is correct
34 Correct 6 ms 512 KB Output is correct
35 Correct 5 ms 512 KB Output is correct
36 Correct 5 ms 512 KB Output is correct
37 Correct 5 ms 512 KB Output is correct
38 Correct 4 ms 512 KB Output is correct
39 Correct 396 ms 9712 KB Output is correct
40 Correct 396 ms 9720 KB Output is correct
41 Correct 380 ms 9720 KB Output is correct
42 Correct 356 ms 9848 KB Output is correct
43 Correct 545 ms 10144 KB Output is correct
44 Correct 304 ms 9988 KB Output is correct
45 Correct 305 ms 13688 KB Output is correct
46 Correct 121 ms 15484 KB Output is correct
47 Correct 570 ms 19772 KB Output is correct
48 Correct 2225 ms 44700 KB Output is correct
49 Correct 2151 ms 44024 KB Output is correct
50 Correct 721 ms 19804 KB Output is correct
51 Correct 606 ms 19740 KB Output is correct
52 Correct 835 ms 19884 KB Output is correct
53 Correct 1132 ms 18668 KB Output is correct
54 Correct 2102 ms 30840 KB Output is correct
55 Correct 47 ms 1672 KB Output is correct
56 Correct 23 ms 1280 KB Output is correct
57 Correct 179 ms 9848 KB Output is correct
58 Correct 71 ms 9708 KB Output is correct
59 Correct 638 ms 21376 KB Output is correct
60 Correct 2187 ms 43388 KB Output is correct
61 Correct 664 ms 21244 KB Output is correct
62 Correct 569 ms 19800 KB Output is correct
63 Correct 819 ms 19892 KB Output is correct
64 Correct 2135 ms 26536 KB Output is correct
65 Correct 1874 ms 30328 KB Output is correct
66 Correct 2155 ms 41948 KB Output is correct
67 Correct 293 ms 20200 KB Output is correct
68 Correct 1188 ms 28236 KB Output is correct
69 Correct 1136 ms 28536 KB Output is correct
70 Correct 970 ms 27228 KB Output is correct