답안 #980696

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
980696 2024-05-12T10:28:33 Z hieptk 경주 (Race) (IOI11_race) C++17
100 / 100
2763 ms 71120 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;

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

unordered_map<int, int> bfs(int i, int p, int k) {
  queue<int> q;
  q.push(i);
  unordered_map<int, pair<int, int>> d;
  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.count(j)) {
        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) 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 k) {
  unordered_map<int, int> res;
  for (auto [j, w]: adj[root]) {
    auto resj = bfs(j, root, 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] > len + 1) {
          res[cost + w] = len + 1;
        }
    }
  }
}

void dfs(int i, int p, unordered_map<int, pair<int, int>> &child) {
  int nchild = 1;
  int maxchild = 0;
  for (auto [j, w]: adj[i]) {
    if (j == p) continue;
    dfs(j, i, child);
    nchild += child[j].first;
    maxchild = max(maxchild, child[j].first);
  }
  child[i] = {nchild, maxchild};
}

int findCenter(int root) {
  unordered_map<int, pair<int, int>> child;
  dfs(root, -1, child);
  int s = child[root].first;
  if (s == 1) return -1;
  for (auto [i, p]: child) {
    int maxsplit = max(p.second, s - p.first);
    if (maxsplit <= s / 2) return i;
  }
  return -1;
}

void remove(int u) {
  for (auto [j, w]: adj[u]) {
    adj[j].erase({u, w});
  }
}

void calc(int root, int k) {
  int u = findCenter(root);
  // cout << "root " << root << " center " << u << "\n";
  if (u == -1) return;
  check(u, k);
  // cout << "root " << root << " center " << u << " " << Res << "\n";
  remove(u);
  for (auto [j, w]: adj[u]) {
    calc(j, 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(H[i][1], L[i]);
        adj[H[i][1]].emplace(H[i][0], L[i]);
    }
    Res = n;
    calc(0, 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";
// }
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 15704 KB Output is correct
2 Correct 4 ms 15704 KB Output is correct
3 Correct 3 ms 15708 KB Output is correct
4 Correct 4 ms 15708 KB Output is correct
5 Correct 3 ms 15708 KB Output is correct
6 Correct 4 ms 15708 KB Output is correct
7 Correct 4 ms 15704 KB Output is correct
8 Correct 4 ms 15708 KB Output is correct
9 Correct 3 ms 15704 KB Output is correct
10 Correct 4 ms 15828 KB Output is correct
11 Correct 3 ms 15708 KB Output is correct
12 Correct 3 ms 15708 KB Output is correct
13 Correct 4 ms 15708 KB Output is correct
14 Correct 3 ms 15828 KB Output is correct
15 Correct 3 ms 15856 KB Output is correct
16 Correct 3 ms 15708 KB Output is correct
17 Correct 3 ms 15708 KB Output is correct
18 Correct 3 ms 15708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 15704 KB Output is correct
2 Correct 4 ms 15704 KB Output is correct
3 Correct 3 ms 15708 KB Output is correct
4 Correct 4 ms 15708 KB Output is correct
5 Correct 3 ms 15708 KB Output is correct
6 Correct 4 ms 15708 KB Output is correct
7 Correct 4 ms 15704 KB Output is correct
8 Correct 4 ms 15708 KB Output is correct
9 Correct 3 ms 15704 KB Output is correct
10 Correct 4 ms 15828 KB Output is correct
11 Correct 3 ms 15708 KB Output is correct
12 Correct 3 ms 15708 KB Output is correct
13 Correct 4 ms 15708 KB Output is correct
14 Correct 3 ms 15828 KB Output is correct
15 Correct 3 ms 15856 KB Output is correct
16 Correct 3 ms 15708 KB Output is correct
17 Correct 3 ms 15708 KB Output is correct
18 Correct 3 ms 15708 KB Output is correct
19 Correct 3 ms 15708 KB Output is correct
20 Correct 3 ms 15800 KB Output is correct
21 Correct 5 ms 15964 KB Output is correct
22 Correct 5 ms 15964 KB Output is correct
23 Correct 5 ms 15960 KB Output is correct
24 Correct 5 ms 16216 KB Output is correct
25 Correct 6 ms 15964 KB Output is correct
26 Correct 5 ms 15964 KB Output is correct
27 Correct 4 ms 15836 KB Output is correct
28 Correct 5 ms 15960 KB Output is correct
29 Correct 6 ms 15964 KB Output is correct
30 Correct 6 ms 15960 KB Output is correct
31 Correct 6 ms 16216 KB Output is correct
32 Correct 8 ms 15964 KB Output is correct
33 Correct 6 ms 15964 KB Output is correct
34 Correct 5 ms 15984 KB Output is correct
35 Correct 6 ms 15964 KB Output is correct
36 Correct 5 ms 15964 KB Output is correct
37 Correct 6 ms 15964 KB Output is correct
38 Correct 6 ms 15996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 15704 KB Output is correct
2 Correct 4 ms 15704 KB Output is correct
3 Correct 3 ms 15708 KB Output is correct
4 Correct 4 ms 15708 KB Output is correct
5 Correct 3 ms 15708 KB Output is correct
6 Correct 4 ms 15708 KB Output is correct
7 Correct 4 ms 15704 KB Output is correct
8 Correct 4 ms 15708 KB Output is correct
9 Correct 3 ms 15704 KB Output is correct
10 Correct 4 ms 15828 KB Output is correct
11 Correct 3 ms 15708 KB Output is correct
12 Correct 3 ms 15708 KB Output is correct
13 Correct 4 ms 15708 KB Output is correct
14 Correct 3 ms 15828 KB Output is correct
15 Correct 3 ms 15856 KB Output is correct
16 Correct 3 ms 15708 KB Output is correct
17 Correct 3 ms 15708 KB Output is correct
18 Correct 3 ms 15708 KB Output is correct
19 Correct 437 ms 32100 KB Output is correct
20 Correct 426 ms 32100 KB Output is correct
21 Correct 502 ms 31876 KB Output is correct
22 Correct 459 ms 32008 KB Output is correct
23 Correct 308 ms 31880 KB Output is correct
24 Correct 238 ms 32100 KB Output is correct
25 Correct 524 ms 36968 KB Output is correct
26 Correct 456 ms 41316 KB Output is correct
27 Correct 445 ms 46060 KB Output is correct
28 Correct 1012 ms 64968 KB Output is correct
29 Correct 1115 ms 63460 KB Output is correct
30 Correct 581 ms 46216 KB Output is correct
31 Correct 502 ms 46084 KB Output is correct
32 Correct 583 ms 46372 KB Output is correct
33 Correct 1027 ms 46340 KB Output is correct
34 Correct 688 ms 46272 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 15704 KB Output is correct
2 Correct 4 ms 15704 KB Output is correct
3 Correct 3 ms 15708 KB Output is correct
4 Correct 4 ms 15708 KB Output is correct
5 Correct 3 ms 15708 KB Output is correct
6 Correct 4 ms 15708 KB Output is correct
7 Correct 4 ms 15704 KB Output is correct
8 Correct 4 ms 15708 KB Output is correct
9 Correct 3 ms 15704 KB Output is correct
10 Correct 4 ms 15828 KB Output is correct
11 Correct 3 ms 15708 KB Output is correct
12 Correct 3 ms 15708 KB Output is correct
13 Correct 4 ms 15708 KB Output is correct
14 Correct 3 ms 15828 KB Output is correct
15 Correct 3 ms 15856 KB Output is correct
16 Correct 3 ms 15708 KB Output is correct
17 Correct 3 ms 15708 KB Output is correct
18 Correct 3 ms 15708 KB Output is correct
19 Correct 3 ms 15708 KB Output is correct
20 Correct 3 ms 15800 KB Output is correct
21 Correct 5 ms 15964 KB Output is correct
22 Correct 5 ms 15964 KB Output is correct
23 Correct 5 ms 15960 KB Output is correct
24 Correct 5 ms 16216 KB Output is correct
25 Correct 6 ms 15964 KB Output is correct
26 Correct 5 ms 15964 KB Output is correct
27 Correct 4 ms 15836 KB Output is correct
28 Correct 5 ms 15960 KB Output is correct
29 Correct 6 ms 15964 KB Output is correct
30 Correct 6 ms 15960 KB Output is correct
31 Correct 6 ms 16216 KB Output is correct
32 Correct 8 ms 15964 KB Output is correct
33 Correct 6 ms 15964 KB Output is correct
34 Correct 5 ms 15984 KB Output is correct
35 Correct 6 ms 15964 KB Output is correct
36 Correct 5 ms 15964 KB Output is correct
37 Correct 6 ms 15964 KB Output is correct
38 Correct 6 ms 15996 KB Output is correct
39 Correct 437 ms 32100 KB Output is correct
40 Correct 426 ms 32100 KB Output is correct
41 Correct 502 ms 31876 KB Output is correct
42 Correct 459 ms 32008 KB Output is correct
43 Correct 308 ms 31880 KB Output is correct
44 Correct 238 ms 32100 KB Output is correct
45 Correct 524 ms 36968 KB Output is correct
46 Correct 456 ms 41316 KB Output is correct
47 Correct 445 ms 46060 KB Output is correct
48 Correct 1012 ms 64968 KB Output is correct
49 Correct 1115 ms 63460 KB Output is correct
50 Correct 581 ms 46216 KB Output is correct
51 Correct 502 ms 46084 KB Output is correct
52 Correct 583 ms 46372 KB Output is correct
53 Correct 1027 ms 46340 KB Output is correct
54 Correct 688 ms 46272 KB Output is correct
55 Correct 34 ms 16988 KB Output is correct
56 Correct 35 ms 16988 KB Output is correct
57 Correct 408 ms 32096 KB Output is correct
58 Correct 109 ms 32100 KB Output is correct
59 Correct 677 ms 43628 KB Output is correct
60 Correct 2763 ms 71120 KB Output is correct
61 Correct 629 ms 46312 KB Output is correct
62 Correct 876 ms 48236 KB Output is correct
63 Correct 881 ms 48304 KB Output is correct
64 Correct 2084 ms 48276 KB Output is correct
65 Correct 751 ms 49364 KB Output is correct
66 Correct 1838 ms 60572 KB Output is correct
67 Correct 413 ms 49472 KB Output is correct
68 Correct 728 ms 49220 KB Output is correct
69 Correct 769 ms 46272 KB Output is correct
70 Correct 634 ms 47860 KB Output is correct