Submission #422808

# Submission time Handle Problem Language Result Execution time Memory
422808 2021-06-10T12:41:38 Z SuhaibSawalha1 Race (IOI11_race) C++17
100 / 100
1665 ms 43548 KB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;

vector<vector<array<int, 2>>> adj;
int n, k, ans = 2e9, nodes;
vector<int> sub;
bitset<200000> is_cent; 
map<int, int> mp2;

void precalc (int u, int p = -1) {
  sub[u] = 1;
  ++nodes;
  for (auto &v : adj[u]) {
    if (!is_cent[v[0]] && v[0] ^ p) {
      precalc(v[0], u);
      sub[u] += sub[v[0]];
    }
  }
}

int centroid (int u, int p = -1) {
  for (auto &v : adj[u]) {
    if (!is_cent[v[0]] && v[0] ^ p) {
      if (sub[v[0]] > nodes / 2) {
        return centroid(v[0], u);
      }
    }
  }
  return u;
}

void update (map<int, int> &m, int k, int v) {
  if (m.count(k)) {
    m[k] = min(m[k], v);
  }
  else {
    m[k] = v;
  }
}

void dfs (int u, int p, int sum, int d) {
  if (sum > k) {
    return;
  }
  update(mp2, sum, d);
  for (auto &v : adj[u]) {
    if (!is_cent[v[0]] && v[0] ^ p) {
      dfs(v[0], u, sum + v[1], d + 1);
    }
  }
}

void decompose (int u = 0) {
  nodes = 0;
  precalc(u);
  int cent = centroid(u);
  map<int, int> mp;
  mp[0] = 0;
  is_cent[cent] = 1;
  for (auto &v : adj[cent]) {
    if (is_cent[v[0]]) {
      continue;
    }
    mp2.clear();
    dfs(v[0], cent, v[1], 1);
    for (auto &p : mp2) {
      if (mp.count(k - p.first)) {
        ans = min(ans, p.second + mp[k - p.first]);
      }
    }
    for (auto &p : mp2) {
      update(mp, p.first, p.second);
    }
  }
  for (auto &v : adj[cent]) {
    if (!is_cent[v[0]]) {
      decompose(v[0]);
    }
  }
}

int best_path(int N, int K, int H[][2], int L[]) {
  n = N;
  k = K;
  adj.resize(n);
  sub.resize(n);
  for (int i = 0; i < n - 1; ++i) {
    adj[H[i][0]].push_back({H[i][1], L[i]});
    adj[H[i][1]].push_back({H[i][0], L[i]});
  }
  decompose();
  return ans == 2e9 ? -1 : ans;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 2 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 4 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 3 ms 460 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 3 ms 332 KB Output is correct
29 Correct 3 ms 332 KB Output is correct
30 Correct 3 ms 460 KB Output is correct
31 Correct 3 ms 460 KB Output is correct
32 Correct 3 ms 460 KB Output is correct
33 Correct 4 ms 460 KB Output is correct
34 Correct 3 ms 332 KB Output is correct
35 Correct 2 ms 332 KB Output is correct
36 Correct 3 ms 332 KB Output is correct
37 Correct 2 ms 332 KB Output is correct
38 Correct 3 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 289 ms 7924 KB Output is correct
20 Correct 287 ms 7924 KB Output is correct
21 Correct 362 ms 7916 KB Output is correct
22 Correct 288 ms 8000 KB Output is correct
23 Correct 127 ms 8148 KB Output is correct
24 Correct 169 ms 8104 KB Output is correct
25 Correct 178 ms 10664 KB Output is correct
26 Correct 171 ms 13636 KB Output is correct
27 Correct 332 ms 15676 KB Output is correct
28 Correct 504 ms 26992 KB Output is correct
29 Correct 388 ms 26096 KB Output is correct
30 Correct 362 ms 15680 KB Output is correct
31 Correct 326 ms 15632 KB Output is correct
32 Correct 413 ms 15684 KB Output is correct
33 Correct 549 ms 14584 KB Output is correct
34 Correct 304 ms 14492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 204 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 204 KB Output is correct
15 Correct 1 ms 204 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 204 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 2 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 4 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 3 ms 460 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
28 Correct 3 ms 332 KB Output is correct
29 Correct 3 ms 332 KB Output is correct
30 Correct 3 ms 460 KB Output is correct
31 Correct 3 ms 460 KB Output is correct
32 Correct 3 ms 460 KB Output is correct
33 Correct 4 ms 460 KB Output is correct
34 Correct 3 ms 332 KB Output is correct
35 Correct 2 ms 332 KB Output is correct
36 Correct 3 ms 332 KB Output is correct
37 Correct 2 ms 332 KB Output is correct
38 Correct 3 ms 332 KB Output is correct
39 Correct 289 ms 7924 KB Output is correct
40 Correct 287 ms 7924 KB Output is correct
41 Correct 362 ms 7916 KB Output is correct
42 Correct 288 ms 8000 KB Output is correct
43 Correct 127 ms 8148 KB Output is correct
44 Correct 169 ms 8104 KB Output is correct
45 Correct 178 ms 10664 KB Output is correct
46 Correct 171 ms 13636 KB Output is correct
47 Correct 332 ms 15676 KB Output is correct
48 Correct 504 ms 26992 KB Output is correct
49 Correct 388 ms 26096 KB Output is correct
50 Correct 362 ms 15680 KB Output is correct
51 Correct 326 ms 15632 KB Output is correct
52 Correct 413 ms 15684 KB Output is correct
53 Correct 549 ms 14584 KB Output is correct
54 Correct 304 ms 14492 KB Output is correct
55 Correct 35 ms 1432 KB Output is correct
56 Correct 17 ms 1080 KB Output is correct
57 Correct 181 ms 8228 KB Output is correct
58 Correct 60 ms 8084 KB Output is correct
59 Correct 557 ms 20588 KB Output is correct
60 Correct 1586 ms 43548 KB Output is correct
61 Correct 404 ms 15928 KB Output is correct
62 Correct 522 ms 15872 KB Output is correct
63 Correct 557 ms 15900 KB Output is correct
64 Correct 1665 ms 26604 KB Output is correct
65 Correct 307 ms 15760 KB Output is correct
66 Correct 1086 ms 25276 KB Output is correct
67 Correct 196 ms 15996 KB Output is correct
68 Correct 628 ms 22464 KB Output is correct
69 Correct 637 ms 22760 KB Output is correct
70 Correct 583 ms 21828 KB Output is correct