Submission #939849

# Submission time Handle Problem Language Result Execution time Memory
939849 2024-03-06T20:14:38 Z duckindog Race (IOI11_race) C++17
100 / 100
271 ms 35524 KB
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
 
using namespace std;
 
#ifndef LOCAL
#include "race.h"
#endif
 
const int N = 200'000 + 1;
int n, k;
int answer = 1e7;
vector<pair<int, int>> ad[N];
 
int tsz;
int sz[N];
bool mk[N];
int cnt;
void dfs1(int u, int pre = -1) { 
  sz[u] = 1;
  for (const auto& [v, w] : ad[u]) { 
    if (v == pre || mk[v]) continue;
    dfs1(v, u);
    sz[u] += sz[v];
  }
  tsz = sz[u];
}
 
int dfs2(int u, int pre = -1) { 
  for (const auto& [v, w] : ad[u]) { 
    if (v == pre || mk[v]) continue;
    if (sz[v] > tsz / 2) return dfs2(v, u);
  }
  return u;
}

vector<int> undo;
vector<pair<int, int>> lst;
int d[1'000'010];

void dfs3(int u, int pre, int dis, int wei) { 
  lst.push_back({dis, wei});
  undo.push_back(wei);
  for (const auto& [v, w] : ad[u]) 
    if (v != pre && !mk[v]) dfs3(v, u, dis + 1, wei + w);
  
}

void dfs4(int u, int pre = -1) { 
  dfs1(u, pre);
  mk[u = dfs2(u, pre)] = true;
  
  for (const auto& i : undo) if (i <= k) d[i] = 1e7;
  d[0] = 0;
  undo.clear();
 
  for (const auto& [v, w] : ad[u]) {
    if (v == pre || mk[v]) continue;
    
    lst.clear(); dfs3(v, u, 1, w);
 
    for (const auto& [dis, wei] : lst) if (wei <= k) answer = min(answer, dis + d[k - wei]);
    for (const auto& [dis, wei] : lst) if (wei <= k) d[wei] = min(d[wei], dis);
  }
  
  for (const auto& [v, w] : ad[u]) if (v != pre && !mk[v]) dfs4(v, u);
}
 
int best_path(int a, int b, int h[][2], int l[]) { 
  n = a; k = b;
  for (int i = 0; i < n - 1; ++i) { 
    int u = h[i][0], v = h[i][1], w = l[i];
    ad[u].push_back({v, w});
    ad[v].push_back({u, w});
  }
  memset(d, 40, sizeof d);
  dfs4(0);
  return answer > n ? -1 : answer;
} 
 
#ifdef LOCAL
int h[N][2], l[N];
int32_t main() {
  cin.tie(0)->sync_with_stdio(0);
 
  cin >> n >> k;
  for (int i = 0; i < n - 1; ++i) {
    cin >> h[i][0] >> h[i][1];
    cin >> l[i];
  }
  cout << best_path(n, k, h, l) << "\n";
}
#endif
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13656 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 4 ms 13660 KB Output is correct
5 Correct 3 ms 13660 KB Output is correct
6 Correct 3 ms 13660 KB Output is correct
7 Correct 3 ms 13660 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 3 ms 13488 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 3 ms 13660 KB Output is correct
12 Correct 3 ms 13660 KB Output is correct
13 Correct 3 ms 13656 KB Output is correct
14 Correct 3 ms 13660 KB Output is correct
15 Correct 3 ms 13660 KB Output is correct
16 Correct 3 ms 13660 KB Output is correct
17 Correct 3 ms 13660 KB Output is correct
18 Correct 3 ms 13660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13656 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 4 ms 13660 KB Output is correct
5 Correct 3 ms 13660 KB Output is correct
6 Correct 3 ms 13660 KB Output is correct
7 Correct 3 ms 13660 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 3 ms 13488 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 3 ms 13660 KB Output is correct
12 Correct 3 ms 13660 KB Output is correct
13 Correct 3 ms 13656 KB Output is correct
14 Correct 3 ms 13660 KB Output is correct
15 Correct 3 ms 13660 KB Output is correct
16 Correct 3 ms 13660 KB Output is correct
17 Correct 3 ms 13660 KB Output is correct
18 Correct 3 ms 13660 KB Output is correct
19 Correct 3 ms 13660 KB Output is correct
20 Correct 3 ms 13656 KB Output is correct
21 Correct 3 ms 13660 KB Output is correct
22 Correct 4 ms 13492 KB Output is correct
23 Correct 3 ms 13656 KB Output is correct
24 Correct 3 ms 13656 KB Output is correct
25 Correct 3 ms 13660 KB Output is correct
26 Correct 3 ms 13660 KB Output is correct
27 Correct 4 ms 13660 KB Output is correct
28 Correct 3 ms 13660 KB Output is correct
29 Correct 3 ms 13660 KB Output is correct
30 Correct 3 ms 13676 KB Output is correct
31 Correct 4 ms 13660 KB Output is correct
32 Correct 3 ms 13660 KB Output is correct
33 Correct 4 ms 13656 KB Output is correct
34 Correct 3 ms 13660 KB Output is correct
35 Correct 3 ms 13668 KB Output is correct
36 Correct 4 ms 13656 KB Output is correct
37 Correct 3 ms 13660 KB Output is correct
38 Correct 3 ms 13660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13656 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 4 ms 13660 KB Output is correct
5 Correct 3 ms 13660 KB Output is correct
6 Correct 3 ms 13660 KB Output is correct
7 Correct 3 ms 13660 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 3 ms 13488 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 3 ms 13660 KB Output is correct
12 Correct 3 ms 13660 KB Output is correct
13 Correct 3 ms 13656 KB Output is correct
14 Correct 3 ms 13660 KB Output is correct
15 Correct 3 ms 13660 KB Output is correct
16 Correct 3 ms 13660 KB Output is correct
17 Correct 3 ms 13660 KB Output is correct
18 Correct 3 ms 13660 KB Output is correct
19 Correct 78 ms 20560 KB Output is correct
20 Correct 84 ms 21420 KB Output is correct
21 Correct 78 ms 21328 KB Output is correct
22 Correct 70 ms 21960 KB Output is correct
23 Correct 66 ms 22092 KB Output is correct
24 Correct 44 ms 21724 KB Output is correct
25 Correct 114 ms 25936 KB Output is correct
26 Correct 75 ms 25420 KB Output is correct
27 Correct 119 ms 26060 KB Output is correct
28 Correct 223 ms 35272 KB Output is correct
29 Correct 262 ms 35488 KB Output is correct
30 Correct 123 ms 26592 KB Output is correct
31 Correct 113 ms 26544 KB Output is correct
32 Correct 153 ms 26584 KB Output is correct
33 Correct 160 ms 26180 KB Output is correct
34 Correct 153 ms 26676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13656 KB Output is correct
2 Correct 3 ms 13660 KB Output is correct
3 Correct 3 ms 13660 KB Output is correct
4 Correct 4 ms 13660 KB Output is correct
5 Correct 3 ms 13660 KB Output is correct
6 Correct 3 ms 13660 KB Output is correct
7 Correct 3 ms 13660 KB Output is correct
8 Correct 3 ms 13660 KB Output is correct
9 Correct 3 ms 13488 KB Output is correct
10 Correct 3 ms 13660 KB Output is correct
11 Correct 3 ms 13660 KB Output is correct
12 Correct 3 ms 13660 KB Output is correct
13 Correct 3 ms 13656 KB Output is correct
14 Correct 3 ms 13660 KB Output is correct
15 Correct 3 ms 13660 KB Output is correct
16 Correct 3 ms 13660 KB Output is correct
17 Correct 3 ms 13660 KB Output is correct
18 Correct 3 ms 13660 KB Output is correct
19 Correct 3 ms 13660 KB Output is correct
20 Correct 3 ms 13656 KB Output is correct
21 Correct 3 ms 13660 KB Output is correct
22 Correct 4 ms 13492 KB Output is correct
23 Correct 3 ms 13656 KB Output is correct
24 Correct 3 ms 13656 KB Output is correct
25 Correct 3 ms 13660 KB Output is correct
26 Correct 3 ms 13660 KB Output is correct
27 Correct 4 ms 13660 KB Output is correct
28 Correct 3 ms 13660 KB Output is correct
29 Correct 3 ms 13660 KB Output is correct
30 Correct 3 ms 13676 KB Output is correct
31 Correct 4 ms 13660 KB Output is correct
32 Correct 3 ms 13660 KB Output is correct
33 Correct 4 ms 13656 KB Output is correct
34 Correct 3 ms 13660 KB Output is correct
35 Correct 3 ms 13668 KB Output is correct
36 Correct 4 ms 13656 KB Output is correct
37 Correct 3 ms 13660 KB Output is correct
38 Correct 3 ms 13660 KB Output is correct
39 Correct 78 ms 20560 KB Output is correct
40 Correct 84 ms 21420 KB Output is correct
41 Correct 78 ms 21328 KB Output is correct
42 Correct 70 ms 21960 KB Output is correct
43 Correct 66 ms 22092 KB Output is correct
44 Correct 44 ms 21724 KB Output is correct
45 Correct 114 ms 25936 KB Output is correct
46 Correct 75 ms 25420 KB Output is correct
47 Correct 119 ms 26060 KB Output is correct
48 Correct 223 ms 35272 KB Output is correct
49 Correct 262 ms 35488 KB Output is correct
50 Correct 123 ms 26592 KB Output is correct
51 Correct 113 ms 26544 KB Output is correct
52 Correct 153 ms 26584 KB Output is correct
53 Correct 160 ms 26180 KB Output is correct
54 Correct 153 ms 26676 KB Output is correct
55 Correct 8 ms 14168 KB Output is correct
56 Correct 9 ms 14172 KB Output is correct
57 Correct 69 ms 21028 KB Output is correct
58 Correct 30 ms 21720 KB Output is correct
59 Correct 72 ms 25676 KB Output is correct
60 Correct 229 ms 35524 KB Output is correct
61 Correct 124 ms 26056 KB Output is correct
62 Correct 118 ms 26580 KB Output is correct
63 Correct 133 ms 26584 KB Output is correct
64 Correct 222 ms 26312 KB Output is correct
65 Correct 160 ms 28232 KB Output is correct
66 Correct 271 ms 33940 KB Output is correct
67 Correct 69 ms 28536 KB Output is correct
68 Correct 162 ms 27888 KB Output is correct
69 Correct 169 ms 25908 KB Output is correct
70 Correct 140 ms 27376 KB Output is correct