Submission #409563

# Submission time Handle Problem Language Result Execution time Memory
409563 2021-05-21T05:19:20 Z 600Mihnea Race (IOI11_race) C++17
100 / 100
457 ms 35768 KB
#include <bits/stdc++.h>
#include "race.h"

using namespace std;

const int N = 200000 + 7;
const int K = 1000000 + 7;
int last[K], mn[K], event;
int n, k, best, sz[N], total, dist[N];
bool vis[N];
vector<pair<int, int>> g[N];

int get_mn(int x) {
  if (last[x] != event) return N;
  return mn[x];
}

void upd(int x, int val) {
  if (last[x] != event) {
    last[x] = event;
    mn[x] = val;
  } else {
    mn[x] = min(mn[x], val);
  }
}

void build(int a, int p = -1) {
  sz[a] = 1;
  for (auto &edge : g[a]) {
    int b = edge.first, cost = edge.second;
    if (b != p && !vis[b]) {
      build(b, a);
      sz[a] += sz[b];
    }
  }
}

int fcen(int a, int p = -1) {
  int mx = total - sz[a];
  for (auto &edge : g[a]) {
    int b = edge.first, cost = edge.second;
    if (b != p && !vis[b]) {
      mx = max(mx, sz[b]);
    }
  }
  if (mx <= total / 2) {
    return a;
  }
  for (auto &edge : g[a]) {
    int b = edge.first, cost = edge.second;
    if (b != p && !vis[b] && mx == sz[b]) {
      return fcen(b, a);
    }
  }
}

vector<pair<int, int>> dists;

void dfs(int a, int p, int step) {
  if (dist[a] > k) {
    return;
  }
  dists.push_back({dist[a], step});
  for (auto &edge : g[a]) {
    int b = edge.first, cost = edge.second;
    if (b != p && !vis[b]) {
      dist[b] = dist[a] + cost;
      dfs(b, a, step + 1);
    }
  }
}

void solve(int a) {
  build(a);
  total = sz[a];
  a = fcen(a);
  vis[a] = 1;
  event++;
  for (auto &edge : g[a]) {
    int b = edge.first, cost = edge.second;
    if (!vis[b]) {
      dists.clear();
      dist[b] = cost;
      dfs(b, a, 1);
      for (auto &val : dists) {
        int other = k - val.first;
        if (val.first == k) {
          best = min(best, val.second);
        }
        best = min(best, get_mn(other) + val.second);
      }
      for (auto &val : dists) {
        upd(val.first, val.second);
      }
    }
  }
  for (auto &edge : g[a]) {
    int b = edge.first, cost = edge.second;
    if (!vis[b]) {
      solve(b);
    }
  }
}

int best_path(int nodes, int target, int edges[][2], int len_edges[]) {
  best = N;
  n = nodes;
  k = target;
  for (int i = 0; i < n; i++) {
    vis[i] = 0;
    g[i].clear();
  }
  for (int i = 0; i < n - 1; i++) {
    g[edges[i][0]].push_back({edges[i][1], len_edges[i]});
    g[edges[i][1]].push_back({edges[i][0], len_edges[i]});
  }
  solve(0);
  if (best == N) {
    best = -1;
  }
  return best;
}

Compilation message

race.cpp: In function 'void build(int, int)':
race.cpp:30:25: warning: unused variable 'cost' [-Wunused-variable]
   30 |     int b = edge.first, cost = edge.second;
      |                         ^~~~
race.cpp: In function 'int fcen(int, int)':
race.cpp:41:25: warning: unused variable 'cost' [-Wunused-variable]
   41 |     int b = edge.first, cost = edge.second;
      |                         ^~~~
race.cpp:50:25: warning: unused variable 'cost' [-Wunused-variable]
   50 |     int b = edge.first, cost = edge.second;
      |                         ^~~~
race.cpp: In function 'void solve(int)':
race.cpp:98:25: warning: unused variable 'cost' [-Wunused-variable]
   98 |     int b = edge.first, cost = edge.second;
      |                         ^~~~
race.cpp: In function 'int fcen(int, int)':
race.cpp:55:1: warning: control reaches end of non-void function [-Wreturn-type]
   55 | }
      | ^
# 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 3 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 4 ms 4940 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 4 ms 4940 KB Output is correct
10 Correct 4 ms 4940 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 4 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 3 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 4 ms 4940 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 4 ms 4940 KB Output is correct
10 Correct 4 ms 4940 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 4 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 7 ms 10000 KB Output is correct
23 Correct 8 ms 9420 KB Output is correct
24 Correct 8 ms 10828 KB Output is correct
25 Correct 7 ms 8524 KB Output is correct
26 Correct 6 ms 7628 KB Output is correct
27 Correct 4 ms 5068 KB Output is correct
28 Correct 5 ms 6604 KB Output is correct
29 Correct 6 ms 7628 KB Output is correct
30 Correct 6 ms 7884 KB Output is correct
31 Correct 6 ms 8652 KB Output is correct
32 Correct 6 ms 8780 KB Output is correct
33 Correct 7 ms 10316 KB Output is correct
34 Correct 7 ms 9548 KB Output is correct
35 Correct 8 ms 10852 KB Output is correct
36 Correct 8 ms 11596 KB Output is correct
37 Correct 7 ms 8828 KB Output is correct
38 Correct 6 ms 7372 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 3 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 4 ms 4940 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 4 ms 4940 KB Output is correct
10 Correct 4 ms 4940 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 4 ms 4940 KB Output is correct
19 Correct 131 ms 10848 KB Output is correct
20 Correct 118 ms 10776 KB Output is correct
21 Correct 131 ms 11068 KB Output is correct
22 Correct 135 ms 11176 KB Output is correct
23 Correct 90 ms 11024 KB Output is correct
24 Correct 62 ms 10880 KB Output is correct
25 Correct 129 ms 14276 KB Output is correct
26 Correct 105 ms 18468 KB Output is correct
27 Correct 189 ms 16712 KB Output is correct
28 Correct 281 ms 30920 KB Output is correct
29 Correct 298 ms 29820 KB Output is correct
30 Correct 181 ms 16708 KB Output is correct
31 Correct 174 ms 16680 KB Output is correct
32 Correct 218 ms 16636 KB Output is correct
33 Correct 237 ms 15680 KB Output is correct
34 Correct 194 ms 15428 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 3 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 4 ms 4940 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 4 ms 4940 KB Output is correct
10 Correct 4 ms 4940 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 4 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 7 ms 10000 KB Output is correct
23 Correct 8 ms 9420 KB Output is correct
24 Correct 8 ms 10828 KB Output is correct
25 Correct 7 ms 8524 KB Output is correct
26 Correct 6 ms 7628 KB Output is correct
27 Correct 4 ms 5068 KB Output is correct
28 Correct 5 ms 6604 KB Output is correct
29 Correct 6 ms 7628 KB Output is correct
30 Correct 6 ms 7884 KB Output is correct
31 Correct 6 ms 8652 KB Output is correct
32 Correct 6 ms 8780 KB Output is correct
33 Correct 7 ms 10316 KB Output is correct
34 Correct 7 ms 9548 KB Output is correct
35 Correct 8 ms 10852 KB Output is correct
36 Correct 8 ms 11596 KB Output is correct
37 Correct 7 ms 8828 KB Output is correct
38 Correct 6 ms 7372 KB Output is correct
39 Correct 131 ms 10848 KB Output is correct
40 Correct 118 ms 10776 KB Output is correct
41 Correct 131 ms 11068 KB Output is correct
42 Correct 135 ms 11176 KB Output is correct
43 Correct 90 ms 11024 KB Output is correct
44 Correct 62 ms 10880 KB Output is correct
45 Correct 129 ms 14276 KB Output is correct
46 Correct 105 ms 18468 KB Output is correct
47 Correct 189 ms 16712 KB Output is correct
48 Correct 281 ms 30920 KB Output is correct
49 Correct 298 ms 29820 KB Output is correct
50 Correct 181 ms 16708 KB Output is correct
51 Correct 174 ms 16680 KB Output is correct
52 Correct 218 ms 16636 KB Output is correct
53 Correct 237 ms 15680 KB Output is correct
54 Correct 194 ms 15428 KB Output is correct
55 Correct 11 ms 5708 KB Output is correct
56 Correct 12 ms 5656 KB Output is correct
57 Correct 90 ms 11296 KB Output is correct
58 Correct 41 ms 10904 KB Output is correct
59 Correct 127 ms 19280 KB Output is correct
60 Correct 423 ms 35768 KB Output is correct
61 Correct 199 ms 16708 KB Output is correct
62 Correct 201 ms 16652 KB Output is correct
63 Correct 264 ms 16708 KB Output is correct
64 Correct 457 ms 19552 KB Output is correct
65 Correct 248 ms 16580 KB Output is correct
66 Correct 408 ms 35180 KB Output is correct
67 Correct 127 ms 16856 KB Output is correct
68 Correct 302 ms 21968 KB Output is correct
69 Correct 294 ms 22336 KB Output is correct
70 Correct 262 ms 21464 KB Output is correct