Submission #980672

# Submission time Handle Problem Language Result Execution time Memory
980672 2024-05-12T10:02:24 Z hieptk Race (IOI11_race) C++17
21 / 100
3000 ms 22372 KB
#include <iostream>
#include <vector>
#include <cmath>
#include <numeric>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <cstring>
#include <unordered_map>
#include <iomanip>
#include <queue>
#include <map>
#include <sstream>
#include <stack>
#include <bitset>
 
using ll = long long;
using ld = long double;
 
using namespace std;

const int nm = 200002;
const int inf = 1e9;

vector<pair<int, int>> adj[nm];
int H[nm][2], L[nm];
int Res;

unordered_map<int, int> bfs(int i, int p, int n, int k) {
  queue<int> q;
  q.push(i);
  vector<pair<int, int>> d(n, {inf, inf});
  d[i] = {0, 0};
  unordered_map<int, int> res;
  res[0] = 0;
  while (q.size()) {
    int i = q.front();
    q.pop();
    for (auto [j, w]: adj[i]) {
      if (j == p) continue;
      if (d[j].second > d[i].second + 1) {
        d[j].first = d[i].first + w;
        d[j].second = d[i].second + 1;
        if (!res.count(d[j].first)) res[d[j].first] = d[j].second;
        else res[d[j].first] = min(res[d[j].first], d[j].second);
        if (d[j].second < Res && d[j].first < k) q.push(j);
      }
    }
  }
  return res;
}

void check(int root, int n, int k) {
  unordered_map<int, int> res;
  for (auto [j, w]: adj[root]) {
    auto resj = bfs(j, root, n, k);
    for (auto [cost, len]: resj) {
      if (cost + w > k) continue;
      if (cost + w == k) {
          Res = min(Res, len + 1);
      } else if (res.count(k - cost - w)) {
          Res = min(Res, res[k - cost - w] + len + 1);
      }
    }
    for (auto [cost, len]: resj) {
        if (cost + w > k) continue;
        if (res.count(cost + w)) {
          res[cost + w] = min(res[cost + w], len + 1);
        } else {
          res[cost + w] = len + 1;
        }
    }
  }
}

void dfs(int i, int p, vector<int> &nchild, vector<int> &maxchild, unordered_set<int> &visited) {
  nchild[i] = 1;
  visited.insert(i);
  for (auto [j, w]: adj[i]) {
    if (j == p) continue;
    dfs(j, i, nchild, maxchild, visited);
    nchild[i] += nchild[j];
    maxchild[i] = max(maxchild[i], nchild[j]);
  }
}

int findCenter(int root, int n) {
  vector<int> nchild(n), maxchild(n);
  unordered_set<int> visited;
  dfs(root, -1, nchild, maxchild, visited);
  int s = nchild[root];
  if (s == 1) return -1;
  for (int i: visited) {
    int maxsplit = max(maxchild[i], s - nchild[i]);
    if (maxsplit <= s / 2) return i;
  }
  return -1;
}

void remove(int u) {
  for (auto [j, w]: adj[u]) {
    for (auto it = adj[j].begin(); it != adj[j].end(); ++it) {
      if (it->first == u) {
        adj[j].erase(it);
        break;
      }
    }
  }
}

void calc(int root, int n, int k) {
  int u = findCenter(root, n);
  // cout << "root " << root << " center " << u << "\n";
  if (u == -1) return;
  check(u, n, k);
  // cout << "root " << root << " center " << u << " " << Res << "\n";
  remove(u);
  for (auto [j, w]: adj[u]) {
    calc(j, n, k);
  }
}

int best_path(int n, int k, int H[][2], int L[]) {
    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]);
    }
    Res = n + 1;
    calc(0, n, k);
    return Res < n ? Res : -1;
}
 
// int main() {
//     #ifdef LOCAL
//     freopen("input.txt", "r", stdin);
//     #endif
//     ios::sync_with_stdio(0);
//     cin.tie(0);
//     int n, k;
//     cin >> n >> k;
//     for (int i = 0; i < n - 1; ++i) cin >> H[i][0] >> H[i][1] >> L[i];
//     cout << best_path(n, k, H, L) << "\n";
// }
# Verdict Execution time Memory Grader output
1 Correct 3 ms 11100 KB Output is correct
2 Correct 2 ms 11100 KB Output is correct
3 Correct 2 ms 11100 KB Output is correct
4 Correct 3 ms 10936 KB Output is correct
5 Correct 2 ms 11100 KB Output is correct
6 Correct 3 ms 11100 KB Output is correct
7 Correct 2 ms 11096 KB Output is correct
8 Correct 2 ms 11100 KB Output is correct
9 Correct 2 ms 11100 KB Output is correct
10 Correct 2 ms 11100 KB Output is correct
11 Correct 2 ms 10936 KB Output is correct
12 Correct 3 ms 11100 KB Output is correct
13 Correct 2 ms 11128 KB Output is correct
14 Correct 3 ms 11100 KB Output is correct
15 Correct 3 ms 11100 KB Output is correct
16 Correct 3 ms 11100 KB Output is correct
17 Correct 2 ms 11100 KB Output is correct
18 Correct 2 ms 11096 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 11100 KB Output is correct
2 Correct 2 ms 11100 KB Output is correct
3 Correct 2 ms 11100 KB Output is correct
4 Correct 3 ms 10936 KB Output is correct
5 Correct 2 ms 11100 KB Output is correct
6 Correct 3 ms 11100 KB Output is correct
7 Correct 2 ms 11096 KB Output is correct
8 Correct 2 ms 11100 KB Output is correct
9 Correct 2 ms 11100 KB Output is correct
10 Correct 2 ms 11100 KB Output is correct
11 Correct 2 ms 10936 KB Output is correct
12 Correct 3 ms 11100 KB Output is correct
13 Correct 2 ms 11128 KB Output is correct
14 Correct 3 ms 11100 KB Output is correct
15 Correct 3 ms 11100 KB Output is correct
16 Correct 3 ms 11100 KB Output is correct
17 Correct 2 ms 11100 KB Output is correct
18 Correct 2 ms 11096 KB Output is correct
19 Correct 2 ms 11100 KB Output is correct
20 Correct 2 ms 11100 KB Output is correct
21 Correct 5 ms 11100 KB Output is correct
22 Correct 4 ms 11100 KB Output is correct
23 Correct 4 ms 11100 KB Output is correct
24 Correct 4 ms 11100 KB Output is correct
25 Correct 5 ms 11096 KB Output is correct
26 Correct 4 ms 11100 KB Output is correct
27 Correct 4 ms 11100 KB Output is correct
28 Correct 6 ms 11100 KB Output is correct
29 Correct 6 ms 11100 KB Output is correct
30 Correct 5 ms 11100 KB Output is correct
31 Correct 5 ms 11100 KB Output is correct
32 Correct 5 ms 11096 KB Output is correct
33 Correct 5 ms 11100 KB Output is correct
34 Correct 4 ms 11100 KB Output is correct
35 Correct 5 ms 11100 KB Output is correct
36 Correct 5 ms 11100 KB Output is correct
37 Correct 6 ms 11120 KB Output is correct
38 Correct 5 ms 11100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 11100 KB Output is correct
2 Correct 2 ms 11100 KB Output is correct
3 Correct 2 ms 11100 KB Output is correct
4 Correct 3 ms 10936 KB Output is correct
5 Correct 2 ms 11100 KB Output is correct
6 Correct 3 ms 11100 KB Output is correct
7 Correct 2 ms 11096 KB Output is correct
8 Correct 2 ms 11100 KB Output is correct
9 Correct 2 ms 11100 KB Output is correct
10 Correct 2 ms 11100 KB Output is correct
11 Correct 2 ms 10936 KB Output is correct
12 Correct 3 ms 11100 KB Output is correct
13 Correct 2 ms 11128 KB Output is correct
14 Correct 3 ms 11100 KB Output is correct
15 Correct 3 ms 11100 KB Output is correct
16 Correct 3 ms 11100 KB Output is correct
17 Correct 2 ms 11100 KB Output is correct
18 Correct 2 ms 11096 KB Output is correct
19 Execution timed out 3039 ms 22372 KB Time limit exceeded
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 11100 KB Output is correct
2 Correct 2 ms 11100 KB Output is correct
3 Correct 2 ms 11100 KB Output is correct
4 Correct 3 ms 10936 KB Output is correct
5 Correct 2 ms 11100 KB Output is correct
6 Correct 3 ms 11100 KB Output is correct
7 Correct 2 ms 11096 KB Output is correct
8 Correct 2 ms 11100 KB Output is correct
9 Correct 2 ms 11100 KB Output is correct
10 Correct 2 ms 11100 KB Output is correct
11 Correct 2 ms 10936 KB Output is correct
12 Correct 3 ms 11100 KB Output is correct
13 Correct 2 ms 11128 KB Output is correct
14 Correct 3 ms 11100 KB Output is correct
15 Correct 3 ms 11100 KB Output is correct
16 Correct 3 ms 11100 KB Output is correct
17 Correct 2 ms 11100 KB Output is correct
18 Correct 2 ms 11096 KB Output is correct
19 Correct 2 ms 11100 KB Output is correct
20 Correct 2 ms 11100 KB Output is correct
21 Correct 5 ms 11100 KB Output is correct
22 Correct 4 ms 11100 KB Output is correct
23 Correct 4 ms 11100 KB Output is correct
24 Correct 4 ms 11100 KB Output is correct
25 Correct 5 ms 11096 KB Output is correct
26 Correct 4 ms 11100 KB Output is correct
27 Correct 4 ms 11100 KB Output is correct
28 Correct 6 ms 11100 KB Output is correct
29 Correct 6 ms 11100 KB Output is correct
30 Correct 5 ms 11100 KB Output is correct
31 Correct 5 ms 11100 KB Output is correct
32 Correct 5 ms 11096 KB Output is correct
33 Correct 5 ms 11100 KB Output is correct
34 Correct 4 ms 11100 KB Output is correct
35 Correct 5 ms 11100 KB Output is correct
36 Correct 5 ms 11100 KB Output is correct
37 Correct 6 ms 11120 KB Output is correct
38 Correct 5 ms 11100 KB Output is correct
39 Execution timed out 3039 ms 22372 KB Time limit exceeded
40 Halted 0 ms 0 KB -