// source: https://oj.uz/problem/view/IOI11_race
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
const int mxN = 200000;
const int INF = 1e9;
int n, k, ans;
vector<pair<int, int>> adj[mxN];
map<int, int> paths[mxN];
pair<int, int> offset[mxN];
void upd(map<int, int>& m, int x, int y) {
auto it = m.find(x);
if (it == m.end())
m[x] = y;
else if (y < it->second)
it->second = y;
}
int qry(int u, int x) {
auto it = paths[u].find(x);
return it == paths[u].end() ? INF : it->second + offset[u].second;
}
void dfs(int u = 0, int p = -1) {
upd(paths[u], 0, 0);
for (pair<int, int> v : adj[u]) {
if (v.first == p)
continue;
dfs(v.first, u);
offset[v.first].first += v.second;
++offset[v.first].second;
if (paths[v.first].size() > paths[u].size()) {
swap(paths[u], paths[v.first]);
swap(offset[u], offset[v.first]);
}
for (auto& x : paths[v.first]) {
pair<int, int> real = {x.first + offset[v.first].first, x.second + offset[v.first].second};
if (real.first > k)
continue;
ans = min(ans, real.second + qry(u, k - real.first - offset[u].first));
}
for (auto& x : paths[v.first]) {
pair<int, int> real = {x.first + offset[v.first].first, x.second + offset[v.first].second};
if (real.first > k)
continue;
upd(paths[u], real.first - offset[u].first, real.second - offset[u].second);
}
map<int, int>().swap(paths[v.first]);
}
}
int best_path(int _n, int _k, int h[][2], int l[]) {
n = _n, k = _k, ans = INF;
for (int i = 0; i < n - 1; ++i) {
int u = h[i][0], v = h[i][1], w = l[i];
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
dfs();
return ans == INF ? -1 : ans;
}
/*
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, k; cin >> n >> k;
int h[n - 1][2];
int 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);
return 0;
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14444 KB |
Output is correct |
2 |
Correct |
11 ms |
14572 KB |
Output is correct |
3 |
Correct |
10 ms |
14392 KB |
Output is correct |
4 |
Correct |
10 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
10 ms |
14444 KB |
Output is correct |
7 |
Correct |
11 ms |
14444 KB |
Output is correct |
8 |
Correct |
10 ms |
14444 KB |
Output is correct |
9 |
Correct |
10 ms |
14444 KB |
Output is correct |
10 |
Correct |
10 ms |
14444 KB |
Output is correct |
11 |
Correct |
10 ms |
14572 KB |
Output is correct |
12 |
Correct |
10 ms |
14444 KB |
Output is correct |
13 |
Correct |
10 ms |
14444 KB |
Output is correct |
14 |
Correct |
10 ms |
14444 KB |
Output is correct |
15 |
Correct |
10 ms |
14444 KB |
Output is correct |
16 |
Correct |
10 ms |
14444 KB |
Output is correct |
17 |
Correct |
10 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14444 KB |
Output is correct |
2 |
Correct |
11 ms |
14572 KB |
Output is correct |
3 |
Correct |
10 ms |
14392 KB |
Output is correct |
4 |
Correct |
10 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
10 ms |
14444 KB |
Output is correct |
7 |
Correct |
11 ms |
14444 KB |
Output is correct |
8 |
Correct |
10 ms |
14444 KB |
Output is correct |
9 |
Correct |
10 ms |
14444 KB |
Output is correct |
10 |
Correct |
10 ms |
14444 KB |
Output is correct |
11 |
Correct |
10 ms |
14572 KB |
Output is correct |
12 |
Correct |
10 ms |
14444 KB |
Output is correct |
13 |
Correct |
10 ms |
14444 KB |
Output is correct |
14 |
Correct |
10 ms |
14444 KB |
Output is correct |
15 |
Correct |
10 ms |
14444 KB |
Output is correct |
16 |
Correct |
10 ms |
14444 KB |
Output is correct |
17 |
Correct |
10 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
19 |
Correct |
10 ms |
14444 KB |
Output is correct |
20 |
Correct |
10 ms |
14444 KB |
Output is correct |
21 |
Correct |
11 ms |
14572 KB |
Output is correct |
22 |
Correct |
11 ms |
14572 KB |
Output is correct |
23 |
Correct |
11 ms |
14572 KB |
Output is correct |
24 |
Correct |
11 ms |
14572 KB |
Output is correct |
25 |
Correct |
11 ms |
14572 KB |
Output is correct |
26 |
Correct |
11 ms |
14444 KB |
Output is correct |
27 |
Correct |
12 ms |
14444 KB |
Output is correct |
28 |
Correct |
11 ms |
14572 KB |
Output is correct |
29 |
Correct |
11 ms |
14572 KB |
Output is correct |
30 |
Correct |
11 ms |
14572 KB |
Output is correct |
31 |
Correct |
12 ms |
14572 KB |
Output is correct |
32 |
Correct |
11 ms |
14572 KB |
Output is correct |
33 |
Correct |
13 ms |
14572 KB |
Output is correct |
34 |
Correct |
11 ms |
14572 KB |
Output is correct |
35 |
Correct |
11 ms |
14700 KB |
Output is correct |
36 |
Correct |
11 ms |
14572 KB |
Output is correct |
37 |
Correct |
11 ms |
14572 KB |
Output is correct |
38 |
Correct |
11 ms |
14572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14444 KB |
Output is correct |
2 |
Correct |
11 ms |
14572 KB |
Output is correct |
3 |
Correct |
10 ms |
14392 KB |
Output is correct |
4 |
Correct |
10 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
10 ms |
14444 KB |
Output is correct |
7 |
Correct |
11 ms |
14444 KB |
Output is correct |
8 |
Correct |
10 ms |
14444 KB |
Output is correct |
9 |
Correct |
10 ms |
14444 KB |
Output is correct |
10 |
Correct |
10 ms |
14444 KB |
Output is correct |
11 |
Correct |
10 ms |
14572 KB |
Output is correct |
12 |
Correct |
10 ms |
14444 KB |
Output is correct |
13 |
Correct |
10 ms |
14444 KB |
Output is correct |
14 |
Correct |
10 ms |
14444 KB |
Output is correct |
15 |
Correct |
10 ms |
14444 KB |
Output is correct |
16 |
Correct |
10 ms |
14444 KB |
Output is correct |
17 |
Correct |
10 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
19 |
Correct |
123 ms |
20460 KB |
Output is correct |
20 |
Correct |
139 ms |
20460 KB |
Output is correct |
21 |
Correct |
119 ms |
20460 KB |
Output is correct |
22 |
Correct |
126 ms |
20588 KB |
Output is correct |
23 |
Correct |
105 ms |
20844 KB |
Output is correct |
24 |
Correct |
107 ms |
20588 KB |
Output is correct |
25 |
Correct |
108 ms |
34156 KB |
Output is correct |
26 |
Correct |
78 ms |
46444 KB |
Output is correct |
27 |
Correct |
196 ms |
26604 KB |
Output is correct |
28 |
Correct |
312 ms |
78328 KB |
Output is correct |
29 |
Correct |
300 ms |
75116 KB |
Output is correct |
30 |
Correct |
209 ms |
26760 KB |
Output is correct |
31 |
Correct |
210 ms |
26604 KB |
Output is correct |
32 |
Correct |
251 ms |
26604 KB |
Output is correct |
33 |
Correct |
228 ms |
26220 KB |
Output is correct |
34 |
Correct |
183 ms |
25836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
14444 KB |
Output is correct |
2 |
Correct |
11 ms |
14572 KB |
Output is correct |
3 |
Correct |
10 ms |
14392 KB |
Output is correct |
4 |
Correct |
10 ms |
14444 KB |
Output is correct |
5 |
Correct |
10 ms |
14444 KB |
Output is correct |
6 |
Correct |
10 ms |
14444 KB |
Output is correct |
7 |
Correct |
11 ms |
14444 KB |
Output is correct |
8 |
Correct |
10 ms |
14444 KB |
Output is correct |
9 |
Correct |
10 ms |
14444 KB |
Output is correct |
10 |
Correct |
10 ms |
14444 KB |
Output is correct |
11 |
Correct |
10 ms |
14572 KB |
Output is correct |
12 |
Correct |
10 ms |
14444 KB |
Output is correct |
13 |
Correct |
10 ms |
14444 KB |
Output is correct |
14 |
Correct |
10 ms |
14444 KB |
Output is correct |
15 |
Correct |
10 ms |
14444 KB |
Output is correct |
16 |
Correct |
10 ms |
14444 KB |
Output is correct |
17 |
Correct |
10 ms |
14444 KB |
Output is correct |
18 |
Correct |
10 ms |
14444 KB |
Output is correct |
19 |
Correct |
10 ms |
14444 KB |
Output is correct |
20 |
Correct |
10 ms |
14444 KB |
Output is correct |
21 |
Correct |
11 ms |
14572 KB |
Output is correct |
22 |
Correct |
11 ms |
14572 KB |
Output is correct |
23 |
Correct |
11 ms |
14572 KB |
Output is correct |
24 |
Correct |
11 ms |
14572 KB |
Output is correct |
25 |
Correct |
11 ms |
14572 KB |
Output is correct |
26 |
Correct |
11 ms |
14444 KB |
Output is correct |
27 |
Correct |
12 ms |
14444 KB |
Output is correct |
28 |
Correct |
11 ms |
14572 KB |
Output is correct |
29 |
Correct |
11 ms |
14572 KB |
Output is correct |
30 |
Correct |
11 ms |
14572 KB |
Output is correct |
31 |
Correct |
12 ms |
14572 KB |
Output is correct |
32 |
Correct |
11 ms |
14572 KB |
Output is correct |
33 |
Correct |
13 ms |
14572 KB |
Output is correct |
34 |
Correct |
11 ms |
14572 KB |
Output is correct |
35 |
Correct |
11 ms |
14700 KB |
Output is correct |
36 |
Correct |
11 ms |
14572 KB |
Output is correct |
37 |
Correct |
11 ms |
14572 KB |
Output is correct |
38 |
Correct |
11 ms |
14572 KB |
Output is correct |
39 |
Correct |
123 ms |
20460 KB |
Output is correct |
40 |
Correct |
139 ms |
20460 KB |
Output is correct |
41 |
Correct |
119 ms |
20460 KB |
Output is correct |
42 |
Correct |
126 ms |
20588 KB |
Output is correct |
43 |
Correct |
105 ms |
20844 KB |
Output is correct |
44 |
Correct |
107 ms |
20588 KB |
Output is correct |
45 |
Correct |
108 ms |
34156 KB |
Output is correct |
46 |
Correct |
78 ms |
46444 KB |
Output is correct |
47 |
Correct |
196 ms |
26604 KB |
Output is correct |
48 |
Correct |
312 ms |
78328 KB |
Output is correct |
49 |
Correct |
300 ms |
75116 KB |
Output is correct |
50 |
Correct |
209 ms |
26760 KB |
Output is correct |
51 |
Correct |
210 ms |
26604 KB |
Output is correct |
52 |
Correct |
251 ms |
26604 KB |
Output is correct |
53 |
Correct |
228 ms |
26220 KB |
Output is correct |
54 |
Correct |
183 ms |
25836 KB |
Output is correct |
55 |
Correct |
23 ms |
15468 KB |
Output is correct |
56 |
Correct |
18 ms |
15084 KB |
Output is correct |
57 |
Correct |
74 ms |
20716 KB |
Output is correct |
58 |
Correct |
58 ms |
20576 KB |
Output is correct |
59 |
Correct |
104 ms |
46572 KB |
Output is correct |
60 |
Correct |
311 ms |
76488 KB |
Output is correct |
61 |
Correct |
229 ms |
26604 KB |
Output is correct |
62 |
Correct |
216 ms |
26476 KB |
Output is correct |
63 |
Correct |
259 ms |
26604 KB |
Output is correct |
64 |
Correct |
497 ms |
32620 KB |
Output is correct |
65 |
Correct |
230 ms |
26092 KB |
Output is correct |
66 |
Correct |
305 ms |
67436 KB |
Output is correct |
67 |
Correct |
187 ms |
26588 KB |
Output is correct |
68 |
Correct |
370 ms |
33132 KB |
Output is correct |
69 |
Correct |
400 ms |
35948 KB |
Output is correct |
70 |
Correct |
351 ms |
32876 KB |
Output is correct |