답안 #592715

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
592715 2022-07-09T13:52:53 Z ikaurov 경주 (Race) (IOI11_race) C++17
100 / 100
422 ms 74704 KB
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define all(arr) (arr).begin(), (arr).end()
#define ll long long
#define ld long double
#define pb push_back
#define sz(x) int((x).size())
#define fi first
#define se second
#define endl '\n'

const int N = 2e5 + 20, INF = 1e9;

vector<pair<int, int>> g[N];
map<ll, int> min_edges[N];
ll delta_length[N], delta_cnt[N];
ll ans = INF, K;

void dfs(int v, int p){
  int maxguy = -1, maxw;
  for (auto [u, w] : g[v]){
    if (u == p) continue;
    dfs(u, v);
    if (maxguy == -1 || sz(min_edges[maxguy]) < sz(min_edges[u])) maxguy = u, maxw = w;
  }
  if (maxguy != -1){
    min_edges[v] = move(min_edges[maxguy]), delta_length[v] = delta_length[maxguy] + maxw,
    delta_cnt[v] = delta_cnt[maxguy] + 1;
  }
  for (auto [u, w] : g[v]){
    if (u == p) continue;
    if (u != maxguy){
      for (auto [len, cnt] : min_edges[u]){
        ll len1 = len + delta_length[u] + w - delta_length[v];
        int cnt1 = cnt + delta_cnt[u] + 1 - delta_cnt[v];
        if (min_edges[v].count(K - len1 - 2 * delta_length[v])){
          ans = min(ans, cnt1 + 2 * delta_cnt[v] + min_edges[v][K - len1 - 2 * delta_length[v]]);
        }
      }
      for (auto [len, cnt] : min_edges[u]){
        ll len1 = len + delta_length[u] + w - delta_length[v];
        int cnt1 = cnt + delta_cnt[u] + 1 - delta_cnt[v];
        if (min_edges[v].count(len1)) min_edges[v][len1] = min(min_edges[v][len1], cnt1);
        else min_edges[v][len1] = cnt1;
      }
      min_edges[u].clear();
    }
  }
  min_edges[v][-delta_length[v]] = -delta_cnt[v];
  if (min_edges[v].count(K - delta_length[v])) ans = min(ans, min_edges[v][K - delta_length[v]] + delta_cnt[v]);
}

int best_path(int n, int k, int h[][2], int l[]){
  for (int i = 0; i < n; i++){
    g[i].clear();
    min_edges[i].clear();
    delta_length[i] = delta_cnt[i] = 0;
  }
  K = k, ans = INF;
  for (int i = 0; i < n - 1; i++){
    g[h[i][0]].pb({h[i][1], l[i]});
    g[h[i][1]].pb({h[i][0], l[i]});
  }
  dfs(0, 0);
  return ans == INF? -1 : ans;
}

// signed main(){
//   ios_base::sync_with_stdio(0);
//   cin.tie(0);
//   // cout.precision(20);
//   int n, k;
//   cin >> n >> k;
//   int h[n - 1][2], l[n - 1];
//   for (int i = 0; i < n - 1; i++){
//     cin >> h[i][0] >> h[i][1];
//   }
//   for (int i = 0; i < n - 1; i++){
//     cin >> l[i];
//   }
//   cout << best_path(n, k, h, l) << endl;
// }
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 11 ms 14360 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 8 ms 14420 KB Output is correct
6 Correct 7 ms 14420 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 8 ms 14452 KB Output is correct
9 Correct 8 ms 14420 KB Output is correct
10 Correct 8 ms 14420 KB Output is correct
11 Correct 8 ms 14420 KB Output is correct
12 Correct 8 ms 14420 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 7 ms 14420 KB Output is correct
15 Correct 7 ms 14420 KB Output is correct
16 Correct 10 ms 14420 KB Output is correct
17 Correct 8 ms 14404 KB Output is correct
18 Correct 10 ms 14420 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 11 ms 14360 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 8 ms 14420 KB Output is correct
6 Correct 7 ms 14420 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 8 ms 14452 KB Output is correct
9 Correct 8 ms 14420 KB Output is correct
10 Correct 8 ms 14420 KB Output is correct
11 Correct 8 ms 14420 KB Output is correct
12 Correct 8 ms 14420 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 7 ms 14420 KB Output is correct
15 Correct 7 ms 14420 KB Output is correct
16 Correct 10 ms 14420 KB Output is correct
17 Correct 8 ms 14404 KB Output is correct
18 Correct 10 ms 14420 KB Output is correct
19 Correct 8 ms 14420 KB Output is correct
20 Correct 8 ms 14408 KB Output is correct
21 Correct 8 ms 14420 KB Output is correct
22 Correct 8 ms 14548 KB Output is correct
23 Correct 8 ms 14548 KB Output is correct
24 Correct 13 ms 14548 KB Output is correct
25 Correct 9 ms 14548 KB Output is correct
26 Correct 8 ms 14548 KB Output is correct
27 Correct 9 ms 14420 KB Output is correct
28 Correct 9 ms 14548 KB Output is correct
29 Correct 9 ms 14548 KB Output is correct
30 Correct 9 ms 14548 KB Output is correct
31 Correct 11 ms 14440 KB Output is correct
32 Correct 9 ms 14548 KB Output is correct
33 Correct 8 ms 14548 KB Output is correct
34 Correct 9 ms 14548 KB Output is correct
35 Correct 9 ms 14548 KB Output is correct
36 Correct 11 ms 14548 KB Output is correct
37 Correct 11 ms 14548 KB Output is correct
38 Correct 10 ms 14548 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 11 ms 14360 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 8 ms 14420 KB Output is correct
6 Correct 7 ms 14420 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 8 ms 14452 KB Output is correct
9 Correct 8 ms 14420 KB Output is correct
10 Correct 8 ms 14420 KB Output is correct
11 Correct 8 ms 14420 KB Output is correct
12 Correct 8 ms 14420 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 7 ms 14420 KB Output is correct
15 Correct 7 ms 14420 KB Output is correct
16 Correct 10 ms 14420 KB Output is correct
17 Correct 8 ms 14404 KB Output is correct
18 Correct 10 ms 14420 KB Output is correct
19 Correct 93 ms 20812 KB Output is correct
20 Correct 103 ms 20920 KB Output is correct
21 Correct 96 ms 20884 KB Output is correct
22 Correct 114 ms 20900 KB Output is correct
23 Correct 150 ms 21320 KB Output is correct
24 Correct 106 ms 21200 KB Output is correct
25 Correct 83 ms 30140 KB Output is correct
26 Correct 50 ms 37452 KB Output is correct
27 Correct 199 ms 33840 KB Output is correct
28 Correct 206 ms 72876 KB Output is correct
29 Correct 228 ms 71332 KB Output is correct
30 Correct 193 ms 34024 KB Output is correct
31 Correct 202 ms 33868 KB Output is correct
32 Correct 199 ms 33852 KB Output is correct
33 Correct 207 ms 27068 KB Output is correct
34 Correct 310 ms 39664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 14420 KB Output is correct
2 Correct 8 ms 14420 KB Output is correct
3 Correct 11 ms 14360 KB Output is correct
4 Correct 8 ms 14420 KB Output is correct
5 Correct 8 ms 14420 KB Output is correct
6 Correct 7 ms 14420 KB Output is correct
7 Correct 8 ms 14420 KB Output is correct
8 Correct 8 ms 14452 KB Output is correct
9 Correct 8 ms 14420 KB Output is correct
10 Correct 8 ms 14420 KB Output is correct
11 Correct 8 ms 14420 KB Output is correct
12 Correct 8 ms 14420 KB Output is correct
13 Correct 7 ms 14420 KB Output is correct
14 Correct 7 ms 14420 KB Output is correct
15 Correct 7 ms 14420 KB Output is correct
16 Correct 10 ms 14420 KB Output is correct
17 Correct 8 ms 14404 KB Output is correct
18 Correct 10 ms 14420 KB Output is correct
19 Correct 8 ms 14420 KB Output is correct
20 Correct 8 ms 14408 KB Output is correct
21 Correct 8 ms 14420 KB Output is correct
22 Correct 8 ms 14548 KB Output is correct
23 Correct 8 ms 14548 KB Output is correct
24 Correct 13 ms 14548 KB Output is correct
25 Correct 9 ms 14548 KB Output is correct
26 Correct 8 ms 14548 KB Output is correct
27 Correct 9 ms 14420 KB Output is correct
28 Correct 9 ms 14548 KB Output is correct
29 Correct 9 ms 14548 KB Output is correct
30 Correct 9 ms 14548 KB Output is correct
31 Correct 11 ms 14440 KB Output is correct
32 Correct 9 ms 14548 KB Output is correct
33 Correct 8 ms 14548 KB Output is correct
34 Correct 9 ms 14548 KB Output is correct
35 Correct 9 ms 14548 KB Output is correct
36 Correct 11 ms 14548 KB Output is correct
37 Correct 11 ms 14548 KB Output is correct
38 Correct 10 ms 14548 KB Output is correct
39 Correct 93 ms 20812 KB Output is correct
40 Correct 103 ms 20920 KB Output is correct
41 Correct 96 ms 20884 KB Output is correct
42 Correct 114 ms 20900 KB Output is correct
43 Correct 150 ms 21320 KB Output is correct
44 Correct 106 ms 21200 KB Output is correct
45 Correct 83 ms 30140 KB Output is correct
46 Correct 50 ms 37452 KB Output is correct
47 Correct 199 ms 33840 KB Output is correct
48 Correct 206 ms 72876 KB Output is correct
49 Correct 228 ms 71332 KB Output is correct
50 Correct 193 ms 34024 KB Output is correct
51 Correct 202 ms 33868 KB Output is correct
52 Correct 199 ms 33852 KB Output is correct
53 Correct 207 ms 27068 KB Output is correct
54 Correct 310 ms 39664 KB Output is correct
55 Correct 19 ms 15492 KB Output is correct
56 Correct 14 ms 15004 KB Output is correct
57 Correct 58 ms 21072 KB Output is correct
58 Correct 59 ms 28328 KB Output is correct
59 Correct 88 ms 45072 KB Output is correct
60 Correct 215 ms 74704 KB Output is correct
61 Correct 201 ms 38780 KB Output is correct
62 Correct 194 ms 36816 KB Output is correct
63 Correct 212 ms 36804 KB Output is correct
64 Correct 413 ms 38952 KB Output is correct
65 Correct 422 ms 44292 KB Output is correct
66 Correct 232 ms 71008 KB Output is correct
67 Correct 195 ms 43676 KB Output is correct
68 Correct 335 ms 42708 KB Output is correct
69 Correct 326 ms 43216 KB Output is correct
70 Correct 283 ms 41544 KB Output is correct