Submission #908768

# Submission time Handle Problem Language Result Execution time Memory
908768 2024-01-16T19:45:35 Z uped Race (IOI11_race) C++14
100 / 100
313 ms 37912 KB
#include "race.h"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int MAX = 2e5;

ll n, k;
vector<pair<int, int>> adj[MAX];
int sz[MAX];
vector<bool> removed;

int get_size(int n, int p = -1) {
  sz[n] = 1;
  for (auto& [x, _] : adj[n]){
    if (removed[x] || x == p) continue;
    sz[n] += get_size(x, n);
  }
  return sz[n];
}

int find_centroid(int desired, int n, int p = -1) {
  for (auto& [x, _] : adj[n]) {
    if (removed[x] || x == p) continue;
    if (sz[x] > desired) return find_centroid(desired, x, n);
  }
  return n;
}

int best = 1e9;
const int MAX_K = 1e6;
int m[MAX_K + 1];
vector<int> touched;

void solve(int n, int p, int d, ll l, bool filling) {
  if (d > best || l > k) return;
  if (filling) {
    if (m[l] != -1) {
      m[l] = min(m[l], d);
    } else {
      m[l] = d;
    }
    touched.push_back(l);
  } else {
    if (m[k - l] != -1) {
      best = min(best, d + m[k - l]);
    }
  }
  for (auto& [x, w] : adj[n]) {
    if (removed[x] || x == p) continue;
    solve(x, n, d + 1, l + w, filling);
  }
}

void decompose(int n) {
  int c = find_centroid(get_size(n) / 2, n);
  removed[c] = true;
  // if (touched.size() > 4000000) {
  //   cout << "hahaha";
  //   exit(-1);
  // }
 // int start = touched.size();
  m[0] = 0;
  for (auto& [x, w] : adj[c]) {
    if (!removed[x]) {
      solve(x, c, 1, w, false);
      solve(x, c, 1, w, true);
    }
    
  }
  for (int i = 0; i < touched.size(); ++i) {
    m[touched[i]] = -1;
  }
  touched.clear();

  for (auto& [x, _] : adj[c]) {
    if (removed[x]) continue;
    decompose(x);
  }
}


int best_path(int N, int K, int H[][2], int L[])
{
  
  //touched.reserve(4000000);
  memset(m, -1, sizeof(m));
  n = N;
  removed.assign(n, false);
  k = K;
  for (int i = 0; i < n - 1; ++i) {
    int w = L[i];
    int a = H[i][0], b = H[i][1];
    adj[a].emplace_back(b, w);
    adj[b].emplace_back(a, w);
  }
  decompose(0);
  if (best == 1e9) {
    return -1;
  } else {
    return best;
  }
}

Compilation message

race.cpp: In function 'int get_size(int, int)':
race.cpp:15:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   15 |   for (auto& [x, _] : adj[n]){
      |              ^
race.cpp: In function 'int find_centroid(int, int, int)':
race.cpp:23:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   23 |   for (auto& [x, _] : adj[n]) {
      |              ^
race.cpp: In function 'void solve(int, int, int, ll, bool)':
race.cpp:49:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   49 |   for (auto& [x, w] : adj[n]) {
      |              ^
race.cpp: In function 'void decompose(int)':
race.cpp:64:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   64 |   for (auto& [x, w] : adj[c]) {
      |              ^
race.cpp:71:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |   for (int i = 0; i < touched.size(); ++i) {
      |                   ~~^~~~~~~~~~~~~~~~
race.cpp:76:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   76 |   for (auto& [x, _] : adj[c]) {
      |              ^
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13404 KB Output is correct
2 Correct 3 ms 13404 KB Output is correct
3 Correct 4 ms 13404 KB Output is correct
4 Correct 4 ms 13404 KB Output is correct
5 Correct 4 ms 13556 KB Output is correct
6 Correct 3 ms 13404 KB Output is correct
7 Correct 3 ms 13404 KB Output is correct
8 Correct 3 ms 13460 KB Output is correct
9 Correct 3 ms 13404 KB Output is correct
10 Correct 3 ms 13404 KB Output is correct
11 Correct 3 ms 13404 KB Output is correct
12 Correct 3 ms 13404 KB Output is correct
13 Correct 4 ms 13404 KB Output is correct
14 Correct 3 ms 13404 KB Output is correct
15 Correct 3 ms 13404 KB Output is correct
16 Correct 3 ms 13404 KB Output is correct
17 Correct 3 ms 13404 KB Output is correct
18 Correct 3 ms 13456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13404 KB Output is correct
2 Correct 3 ms 13404 KB Output is correct
3 Correct 4 ms 13404 KB Output is correct
4 Correct 4 ms 13404 KB Output is correct
5 Correct 4 ms 13556 KB Output is correct
6 Correct 3 ms 13404 KB Output is correct
7 Correct 3 ms 13404 KB Output is correct
8 Correct 3 ms 13460 KB Output is correct
9 Correct 3 ms 13404 KB Output is correct
10 Correct 3 ms 13404 KB Output is correct
11 Correct 3 ms 13404 KB Output is correct
12 Correct 3 ms 13404 KB Output is correct
13 Correct 4 ms 13404 KB Output is correct
14 Correct 3 ms 13404 KB Output is correct
15 Correct 3 ms 13404 KB Output is correct
16 Correct 3 ms 13404 KB Output is correct
17 Correct 3 ms 13404 KB Output is correct
18 Correct 3 ms 13456 KB Output is correct
19 Correct 3 ms 13404 KB Output is correct
20 Correct 3 ms 13404 KB Output is correct
21 Correct 4 ms 13404 KB Output is correct
22 Correct 4 ms 13404 KB Output is correct
23 Correct 4 ms 13404 KB Output is correct
24 Correct 4 ms 13404 KB Output is correct
25 Correct 4 ms 13400 KB Output is correct
26 Correct 4 ms 13404 KB Output is correct
27 Correct 4 ms 13404 KB Output is correct
28 Correct 4 ms 13532 KB Output is correct
29 Correct 5 ms 13404 KB Output is correct
30 Correct 4 ms 13496 KB Output is correct
31 Correct 4 ms 13404 KB Output is correct
32 Correct 4 ms 13404 KB Output is correct
33 Correct 5 ms 13528 KB Output is correct
34 Correct 4 ms 13404 KB Output is correct
35 Correct 4 ms 13404 KB Output is correct
36 Correct 4 ms 13404 KB Output is correct
37 Correct 5 ms 13404 KB Output is correct
38 Correct 4 ms 13656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13404 KB Output is correct
2 Correct 3 ms 13404 KB Output is correct
3 Correct 4 ms 13404 KB Output is correct
4 Correct 4 ms 13404 KB Output is correct
5 Correct 4 ms 13556 KB Output is correct
6 Correct 3 ms 13404 KB Output is correct
7 Correct 3 ms 13404 KB Output is correct
8 Correct 3 ms 13460 KB Output is correct
9 Correct 3 ms 13404 KB Output is correct
10 Correct 3 ms 13404 KB Output is correct
11 Correct 3 ms 13404 KB Output is correct
12 Correct 3 ms 13404 KB Output is correct
13 Correct 4 ms 13404 KB Output is correct
14 Correct 3 ms 13404 KB Output is correct
15 Correct 3 ms 13404 KB Output is correct
16 Correct 3 ms 13404 KB Output is correct
17 Correct 3 ms 13404 KB Output is correct
18 Correct 3 ms 13456 KB Output is correct
19 Correct 73 ms 19268 KB Output is correct
20 Correct 74 ms 19368 KB Output is correct
21 Correct 73 ms 19432 KB Output is correct
22 Correct 74 ms 19796 KB Output is correct
23 Correct 52 ms 19540 KB Output is correct
24 Correct 41 ms 19500 KB Output is correct
25 Correct 87 ms 22096 KB Output is correct
26 Correct 75 ms 25764 KB Output is correct
27 Correct 107 ms 23120 KB Output is correct
28 Correct 169 ms 34428 KB Output is correct
29 Correct 195 ms 33524 KB Output is correct
30 Correct 142 ms 23264 KB Output is correct
31 Correct 127 ms 23320 KB Output is correct
32 Correct 143 ms 23380 KB Output is correct
33 Correct 160 ms 22088 KB Output is correct
34 Correct 113 ms 21884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13404 KB Output is correct
2 Correct 3 ms 13404 KB Output is correct
3 Correct 4 ms 13404 KB Output is correct
4 Correct 4 ms 13404 KB Output is correct
5 Correct 4 ms 13556 KB Output is correct
6 Correct 3 ms 13404 KB Output is correct
7 Correct 3 ms 13404 KB Output is correct
8 Correct 3 ms 13460 KB Output is correct
9 Correct 3 ms 13404 KB Output is correct
10 Correct 3 ms 13404 KB Output is correct
11 Correct 3 ms 13404 KB Output is correct
12 Correct 3 ms 13404 KB Output is correct
13 Correct 4 ms 13404 KB Output is correct
14 Correct 3 ms 13404 KB Output is correct
15 Correct 3 ms 13404 KB Output is correct
16 Correct 3 ms 13404 KB Output is correct
17 Correct 3 ms 13404 KB Output is correct
18 Correct 3 ms 13456 KB Output is correct
19 Correct 3 ms 13404 KB Output is correct
20 Correct 3 ms 13404 KB Output is correct
21 Correct 4 ms 13404 KB Output is correct
22 Correct 4 ms 13404 KB Output is correct
23 Correct 4 ms 13404 KB Output is correct
24 Correct 4 ms 13404 KB Output is correct
25 Correct 4 ms 13400 KB Output is correct
26 Correct 4 ms 13404 KB Output is correct
27 Correct 4 ms 13404 KB Output is correct
28 Correct 4 ms 13532 KB Output is correct
29 Correct 5 ms 13404 KB Output is correct
30 Correct 4 ms 13496 KB Output is correct
31 Correct 4 ms 13404 KB Output is correct
32 Correct 4 ms 13404 KB Output is correct
33 Correct 5 ms 13528 KB Output is correct
34 Correct 4 ms 13404 KB Output is correct
35 Correct 4 ms 13404 KB Output is correct
36 Correct 4 ms 13404 KB Output is correct
37 Correct 5 ms 13404 KB Output is correct
38 Correct 4 ms 13656 KB Output is correct
39 Correct 73 ms 19268 KB Output is correct
40 Correct 74 ms 19368 KB Output is correct
41 Correct 73 ms 19432 KB Output is correct
42 Correct 74 ms 19796 KB Output is correct
43 Correct 52 ms 19540 KB Output is correct
44 Correct 41 ms 19500 KB Output is correct
45 Correct 87 ms 22096 KB Output is correct
46 Correct 75 ms 25764 KB Output is correct
47 Correct 107 ms 23120 KB Output is correct
48 Correct 169 ms 34428 KB Output is correct
49 Correct 195 ms 33524 KB Output is correct
50 Correct 142 ms 23264 KB Output is correct
51 Correct 127 ms 23320 KB Output is correct
52 Correct 143 ms 23380 KB Output is correct
53 Correct 160 ms 22088 KB Output is correct
54 Correct 113 ms 21884 KB Output is correct
55 Correct 9 ms 13912 KB Output is correct
56 Correct 11 ms 13916 KB Output is correct
57 Correct 68 ms 20104 KB Output is correct
58 Correct 31 ms 20428 KB Output is correct
59 Correct 82 ms 27092 KB Output is correct
60 Correct 310 ms 37912 KB Output is correct
61 Correct 121 ms 26708 KB Output is correct
62 Correct 129 ms 27444 KB Output is correct
63 Correct 165 ms 27448 KB Output is correct
64 Correct 313 ms 27740 KB Output is correct
65 Correct 123 ms 26960 KB Output is correct
66 Correct 268 ms 34612 KB Output is correct
67 Correct 79 ms 28712 KB Output is correct
68 Correct 172 ms 26964 KB Output is correct
69 Correct 119 ms 27124 KB Output is correct
70 Correct 130 ms 26792 KB Output is correct