Submission #1097323

# Submission time Handle Problem Language Result Execution time Memory
1097323 2024-10-06T20:42:08 Z greenbinjack Race (IOI11_race) C++17
100 / 100
683 ms 39440 KB
#include <bits/stdc++.h>
using namespace std;

#define all(v) v.begin(), v.end()

using LL = long long; 

const int N = 2E5 + 69;

vector < pair <int, int> > G[N];
int k, mx_depth, sz[N], pa[N], processed[N], l[N];
map <int, int> mp;
int ans = INT_MAX;

int dfs (int u, int p) {
  sz[u] = 1;
  for(auto [v, idx] : G[u]) if (v != p and !processed[v]) {
    sz[u] += dfs(v, u);
  }
  return sz[u];
}
int centroid (int u, int p, int n) {
  for(auto [v, idx] : G[u]) if(v != p and !processed[v]) {
    if (sz[v] > n / 2) return centroid (v, u, n);
  }
  return u;
}
void get_cnt (int u, int p, bool filling, int len, int depth = 1) {
  // cerr << ((filling) ? "FILLING " : "QUERY ") << u << ' ' << p << ' ' << len << ' ' << depth << '\n';
  if (len > k) return;
  // mx_depth = max (mx_depth, depth);
  if (filling) {
    if (mp.count (len)) mp[len] = min (mp[len], depth);
    else mp[len] = depth;
    // cerr << "UPDATED: " << len << ' ' << mp[len] << endl;
  }
  else {
    // cerr << "QUERYING " << k - len << '\n';
    if (mp.count (k - len)) ans = min (ans, mp[k - len] + depth);
  }
  for (auto [next, idx] : G[u]) if (next != p and !processed[next]) {
    get_cnt (next, u, filling, len + l[idx], depth + 1);
  }
}
void build (int u = 0, int p = -1) {
  int n = dfs (u, p);
  int c = centroid (u, p, n);
  processed[c] = 1;

  // cerr << c << endl;
  // mx_depth = 0;
  for (auto [next, idx] : G[c]) if (next != p and !processed[next]) {
    get_cnt (next, c, false, l[idx]);
    // for (auto [l, r] : mp) cerr << l << ' ' << r << endl;
    get_cnt (next, c, true, l[idx]);
  }
  if (mp.count (k)) ans = min (ans, mp[k]);

  mp.clear ();
  for (auto [next, idx] : G[c]) {
    if (!processed[next]) build (next, c);
  }
}

int best_path (int N, int K, int H[][2], int L[]) {
  k = K;
  for (int i = 0; i < N; i++) l[i] = L[i];
  for (int i = 0; i < N - 1; i++) {
    G[H[i][0]].push_back ({H[i][1], i});
    G[H[i][1]].push_back ({H[i][0], i});
  }
  build ();
  if (ans == INT_MAX) ans = -1;
  return ans;
}

// int main() {
//   cin.tie(nullptr) -> ios_base::sync_with_stdio(false);

//   int n, kk;
//   cin >> n >> kk;
//   int H[n][2], L[n];
//   for (int i = 0; i < n - 1; i++) cin >> H[i][0] >> H[i][1];
//   for (int i = 0; i < n; i++) cin >> L[i];

//   cout << best_path (n, kk, H, L) << '\n';
  
//   return 0; 
// }
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4952 KB Output is correct
6 Correct 2 ms 5164 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 5180 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 2 ms 5152 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 2 ms 5032 KB Output is correct
14 Correct 2 ms 4956 KB Output is correct
15 Correct 2 ms 4956 KB Output is correct
16 Correct 2 ms 5008 KB Output is correct
17 Correct 2 ms 4956 KB Output is correct
18 Correct 2 ms 4956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4952 KB Output is correct
6 Correct 2 ms 5164 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 5180 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 2 ms 5152 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 2 ms 5032 KB Output is correct
14 Correct 2 ms 4956 KB Output is correct
15 Correct 2 ms 4956 KB Output is correct
16 Correct 2 ms 5008 KB Output is correct
17 Correct 2 ms 4956 KB Output is correct
18 Correct 2 ms 4956 KB Output is correct
19 Correct 2 ms 4956 KB Output is correct
20 Correct 3 ms 4952 KB Output is correct
21 Correct 3 ms 5212 KB Output is correct
22 Correct 3 ms 5212 KB Output is correct
23 Correct 2 ms 5212 KB Output is correct
24 Correct 2 ms 5212 KB Output is correct
25 Correct 3 ms 5212 KB Output is correct
26 Correct 3 ms 5212 KB Output is correct
27 Correct 2 ms 5244 KB Output is correct
28 Correct 3 ms 5164 KB Output is correct
29 Correct 3 ms 5212 KB Output is correct
30 Correct 3 ms 5212 KB Output is correct
31 Correct 3 ms 5212 KB Output is correct
32 Correct 3 ms 5212 KB Output is correct
33 Correct 3 ms 5212 KB Output is correct
34 Correct 2 ms 5212 KB Output is correct
35 Correct 3 ms 5212 KB Output is correct
36 Correct 3 ms 5212 KB Output is correct
37 Correct 3 ms 5212 KB Output is correct
38 Correct 5 ms 5212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4952 KB Output is correct
6 Correct 2 ms 5164 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 5180 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 2 ms 5152 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 2 ms 5032 KB Output is correct
14 Correct 2 ms 4956 KB Output is correct
15 Correct 2 ms 4956 KB Output is correct
16 Correct 2 ms 5008 KB Output is correct
17 Correct 2 ms 4956 KB Output is correct
18 Correct 2 ms 4956 KB Output is correct
19 Correct 135 ms 12508 KB Output is correct
20 Correct 120 ms 12264 KB Output is correct
21 Correct 132 ms 12424 KB Output is correct
22 Correct 137 ms 12624 KB Output is correct
23 Correct 58 ms 12624 KB Output is correct
24 Correct 57 ms 12888 KB Output is correct
25 Correct 94 ms 15188 KB Output is correct
26 Correct 74 ms 18000 KB Output is correct
27 Correct 131 ms 20232 KB Output is correct
28 Correct 166 ms 31568 KB Output is correct
29 Correct 159 ms 30544 KB Output is correct
30 Correct 124 ms 20048 KB Output is correct
31 Correct 118 ms 20048 KB Output is correct
32 Correct 131 ms 20256 KB Output is correct
33 Correct 164 ms 19024 KB Output is correct
34 Correct 117 ms 19908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Output is correct
2 Correct 2 ms 4956 KB Output is correct
3 Correct 2 ms 4956 KB Output is correct
4 Correct 2 ms 4956 KB Output is correct
5 Correct 2 ms 4952 KB Output is correct
6 Correct 2 ms 5164 KB Output is correct
7 Correct 2 ms 4956 KB Output is correct
8 Correct 2 ms 5180 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4956 KB Output is correct
11 Correct 2 ms 5152 KB Output is correct
12 Correct 2 ms 4956 KB Output is correct
13 Correct 2 ms 5032 KB Output is correct
14 Correct 2 ms 4956 KB Output is correct
15 Correct 2 ms 4956 KB Output is correct
16 Correct 2 ms 5008 KB Output is correct
17 Correct 2 ms 4956 KB Output is correct
18 Correct 2 ms 4956 KB Output is correct
19 Correct 2 ms 4956 KB Output is correct
20 Correct 3 ms 4952 KB Output is correct
21 Correct 3 ms 5212 KB Output is correct
22 Correct 3 ms 5212 KB Output is correct
23 Correct 2 ms 5212 KB Output is correct
24 Correct 2 ms 5212 KB Output is correct
25 Correct 3 ms 5212 KB Output is correct
26 Correct 3 ms 5212 KB Output is correct
27 Correct 2 ms 5244 KB Output is correct
28 Correct 3 ms 5164 KB Output is correct
29 Correct 3 ms 5212 KB Output is correct
30 Correct 3 ms 5212 KB Output is correct
31 Correct 3 ms 5212 KB Output is correct
32 Correct 3 ms 5212 KB Output is correct
33 Correct 3 ms 5212 KB Output is correct
34 Correct 2 ms 5212 KB Output is correct
35 Correct 3 ms 5212 KB Output is correct
36 Correct 3 ms 5212 KB Output is correct
37 Correct 3 ms 5212 KB Output is correct
38 Correct 5 ms 5212 KB Output is correct
39 Correct 135 ms 12508 KB Output is correct
40 Correct 120 ms 12264 KB Output is correct
41 Correct 132 ms 12424 KB Output is correct
42 Correct 137 ms 12624 KB Output is correct
43 Correct 58 ms 12624 KB Output is correct
44 Correct 57 ms 12888 KB Output is correct
45 Correct 94 ms 15188 KB Output is correct
46 Correct 74 ms 18000 KB Output is correct
47 Correct 131 ms 20232 KB Output is correct
48 Correct 166 ms 31568 KB Output is correct
49 Correct 159 ms 30544 KB Output is correct
50 Correct 124 ms 20048 KB Output is correct
51 Correct 118 ms 20048 KB Output is correct
52 Correct 131 ms 20256 KB Output is correct
53 Correct 164 ms 19024 KB Output is correct
54 Correct 117 ms 19908 KB Output is correct
55 Correct 20 ms 5976 KB Output is correct
56 Correct 11 ms 5720 KB Output is correct
57 Correct 89 ms 12668 KB Output is correct
58 Correct 29 ms 12428 KB Output is correct
59 Correct 213 ms 21732 KB Output is correct
60 Correct 604 ms 39440 KB Output is correct
61 Correct 175 ms 20588 KB Output is correct
62 Correct 189 ms 20492 KB Output is correct
63 Correct 215 ms 20304 KB Output is correct
64 Correct 683 ms 25128 KB Output is correct
65 Correct 116 ms 21052 KB Output is correct
66 Correct 394 ms 29012 KB Output is correct
67 Correct 72 ms 20928 KB Output is correct
68 Correct 281 ms 24656 KB Output is correct
69 Correct 301 ms 24660 KB Output is correct
70 Correct 288 ms 24072 KB Output is correct