Submission #103409

# Submission time Handle Problem Language Result Execution time Memory
103409 2019-03-30T07:16:19 Z wxh010910 Race (IOI11_race) C++17
21 / 100
3000 ms 16180 KB
#include <bits/stdc++.h>
#include "race.h"

using namespace std;

int best_path(int N, int K, int H[][2], int L[]) {
  vector<vector<pair<int, int>>> adj(N);
  for (int i = 0; i < N - 1; ++i) {
    adj[H[i][0]].emplace_back(H[i][1], L[i]);
    adj[H[i][1]].emplace_back(H[i][0], L[i]);
  }
  int ans = N;
  vector<int> pr(N), sz(N), subtree(N);
  vector<bool> visit(N);
  function<void(int, int)> centroid_decomposition = [&](int x, int s) {
    int root = -1;
    pr[x] = -1;
    function<void(int)> find_root = [&](int x) {
      subtree[x] = 0;
      sz[x] = 1;
      for (auto p : adj[x]) {
        int y = p.first;
        if (!visit[y] && y != pr[x]) {
          pr[y] = x;
          find_root(y);
          sz[x] += sz[y];
          subtree[x] = max(subtree[x], sz[y]);
        }
      }
      subtree[x] = max(subtree[x], s - sz[x]);
      if (root == -1 || subtree[x] < subtree[root]) {
        root = x;
      }
    };
    find_root(x);
    visit[root = x] = true;
    if (pr[x] != -1) {
      sz[pr[x]] = s - sz[x];
    }
    pr[x] = -1;
    unordered_map<int, int> f;
    f[0] = 0;
    function<void(int, int, int)> query = [&](int x, int d, int v) {
      if (v > K) {
        return;
      }
      if (f.count(K - v)) {
        ans = min(ans, f[K - v] + d);
      }
      for (auto p : adj[x]) {
        int y = p.first, w = p.second;
        if (!visit[y] && y != pr[x]) {
          pr[y] = x;
          query(y, d + 1, v + w);
        }
      }
    };
    function<void(int, int, int)> modify = [&](int x, int d, int v) {
      if (v > K) {
        return;
      }
      if (f.count(v)) {
        f[v] = min(f[v], d);
      } else {
        f[v] = d;
      }
      for (auto p : adj[x]) {
        int y = p.first, w = p.second;
        if (!visit[y] && y != pr[x]) {
          pr[y] = x;
          modify(y, d + 1, v + w);
        }
      }
    };
    for (auto p : adj[x]) {
      int y = p.first, w = p.second;
      if (!visit[y]) {
        pr[y] = x;
        query(y, 1, w);
        modify(y, 1, w);
      }
    }
    for (auto p : adj[x]) {
      int y = p.first;
      if (!visit[y]) {
        centroid_decomposition(y, sz[y]);
      }
    }
  };
  centroid_decomposition(0, N);
  if (ans == N) {
    ans = -1;
  }
  return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 640 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 356 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 3 ms 512 KB Output is correct
16 Correct 3 ms 512 KB Output is correct
17 Correct 3 ms 512 KB Output is correct
18 Correct 5 ms 512 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 640 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 356 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 3 ms 512 KB Output is correct
16 Correct 3 ms 512 KB Output is correct
17 Correct 3 ms 512 KB Output is correct
18 Correct 5 ms 512 KB Output is correct
19 Correct 3 ms 384 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
21 Correct 4 ms 512 KB Output is correct
22 Correct 4 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 3 ms 384 KB Output is correct
25 Correct 6 ms 640 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 4 ms 384 KB Output is correct
28 Correct 4 ms 512 KB Output is correct
29 Correct 4 ms 512 KB Output is correct
30 Correct 7 ms 640 KB Output is correct
31 Correct 5 ms 768 KB Output is correct
32 Correct 5 ms 768 KB Output is correct
33 Correct 8 ms 896 KB Output is correct
34 Correct 10 ms 896 KB Output is correct
35 Correct 12 ms 1072 KB Output is correct
36 Correct 12 ms 1408 KB Output is correct
37 Correct 29 ms 2708 KB Output is correct
38 Correct 18 ms 1792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 640 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 356 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 3 ms 512 KB Output is correct
16 Correct 3 ms 512 KB Output is correct
17 Correct 3 ms 512 KB Output is correct
18 Correct 5 ms 512 KB Output is correct
19 Correct 975 ms 10356 KB Output is correct
20 Correct 875 ms 10488 KB Output is correct
21 Correct 824 ms 10460 KB Output is correct
22 Correct 846 ms 10496 KB Output is correct
23 Correct 474 ms 10364 KB Output is correct
24 Correct 241 ms 10232 KB Output is correct
25 Execution timed out 3034 ms 16180 KB Time limit exceeded
26 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 640 KB Output is correct
2 Correct 3 ms 384 KB Output is correct
3 Correct 3 ms 384 KB Output is correct
4 Correct 4 ms 384 KB Output is correct
5 Correct 3 ms 384 KB Output is correct
6 Correct 3 ms 512 KB Output is correct
7 Correct 2 ms 384 KB Output is correct
8 Correct 3 ms 384 KB Output is correct
9 Correct 3 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 3 ms 356 KB Output is correct
12 Correct 3 ms 384 KB Output is correct
13 Correct 3 ms 512 KB Output is correct
14 Correct 2 ms 512 KB Output is correct
15 Correct 3 ms 512 KB Output is correct
16 Correct 3 ms 512 KB Output is correct
17 Correct 3 ms 512 KB Output is correct
18 Correct 5 ms 512 KB Output is correct
19 Correct 3 ms 384 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
21 Correct 4 ms 512 KB Output is correct
22 Correct 4 ms 384 KB Output is correct
23 Correct 5 ms 384 KB Output is correct
24 Correct 3 ms 384 KB Output is correct
25 Correct 6 ms 640 KB Output is correct
26 Correct 5 ms 384 KB Output is correct
27 Correct 4 ms 384 KB Output is correct
28 Correct 4 ms 512 KB Output is correct
29 Correct 4 ms 512 KB Output is correct
30 Correct 7 ms 640 KB Output is correct
31 Correct 5 ms 768 KB Output is correct
32 Correct 5 ms 768 KB Output is correct
33 Correct 8 ms 896 KB Output is correct
34 Correct 10 ms 896 KB Output is correct
35 Correct 12 ms 1072 KB Output is correct
36 Correct 12 ms 1408 KB Output is correct
37 Correct 29 ms 2708 KB Output is correct
38 Correct 18 ms 1792 KB Output is correct
39 Correct 975 ms 10356 KB Output is correct
40 Correct 875 ms 10488 KB Output is correct
41 Correct 824 ms 10460 KB Output is correct
42 Correct 846 ms 10496 KB Output is correct
43 Correct 474 ms 10364 KB Output is correct
44 Correct 241 ms 10232 KB Output is correct
45 Execution timed out 3034 ms 16180 KB Time limit exceeded
46 Halted 0 ms 0 KB -