#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
using namespace std;
using li = long long;
using ld = long double;
using pi = pair<int, int>;
using pli = pair<li, li>;
#define all(c) c.begin(), c.end()
#define prec(n) setprecision(n) << fixed
template <typename K, typename V>
class map_m : public map<K, V> {
public:
auto emplace_minimal(K k, V v) {
auto [it, success] = this->try_emplace(k, v);
if (!success && it->second > v) it->second = v;
return it;
}
};
class Tree {
public:
int V, R;
vector<vector<pair<int, li>>> adj, tr;
vector<int> sz, pr, ofl;
vector<map_m<li, int>> mp;
vector<li> ofw;
explicit Tree(int _V)
: V(_V), adj(V), tr(V), sz(V), pr(V), ofl(V), mp(V), ofw(V) {
iota(all(pr), 0);
}
void emplace_edge(int u, int v, li w) {
adj[u].emplace_back(v, w);
adj[v].emplace_back(u, w);
}
void compile(int _R) {
R = _R;
function<void(int, optional<int>)> dfs = [&](int h, optional<int> p) {
sz[h] = 1;
for (auto [t, W] : adj[h]) {
if (p && t == *p) continue;
dfs(t, h);
tr[h].emplace_back(t, W);
if (sz[t] > sz[tr[h][0].first]) swap(tr[h][0], tr[h].back());
sz[h] += sz[t];
}
if (!tr[h].empty()) pr[h] = pr[tr[h][0].first];
adj[h].clear();
adj[h].shrink_to_fit();
};
dfs(R, nullopt);
}
optional<int> solve(int h, li K) {
optional<int> ret;
multiset<pair<li, int>> courses;
for (auto [t, W] : tr[h]) {
auto sol = solve(t, K);
if (sol) {
if (!ret)
ret = sol;
else
ret = min(*ret, *sol);
}
auto it = mp[pr[t]].find(K - W - ofw[pr[t]]);
if (it != mp[pr[t]].end()) {
if (!ret)
ret = ofl[pr[t]] + 1 + it->second;
else
ret = min(*ret, ofl[pr[t]] + 1 + it->second);
}
}
for (auto [t, W] : tr[h]) {
if (pr[h] != pr[t]) {
auto it = mp[pr[t]].begin();
while (it != mp[pr[t]].end()) {
if (ret && it->second + ofl[pr[t]] + 1 > *ret) {
it = mp[pr[t]].erase(it);
continue;
}
++it;
}
for (auto [w, l] : mp[pr[t]])
courses.emplace(W + w + ofw[pr[t]], l + ofl[pr[t]] + 1);
}
}
for (auto [t, W] : tr[h]) {
if (pr[h] != pr[t]) {
for (auto [w, l] : mp[pr[t]])
courses.erase({W + w + ofw[pr[t]], l + ofl[pr[t]] + 1});
for (auto [w, l] : mp[pr[t]]) {
auto remain = K - W - w - ofw[pr[t]];
if (remain < 0) break;
auto it = courses.lower_bound({remain, -1});
if (it != courses.end() && it->first == remain) {
if (!ret)
ret = l + ofl[pr[t]] + 1 + it->second;
else
ret = min(*ret, l + ofl[pr[t]] + 1 + it->second);
}
}
for (auto [w, l] : mp[pr[t]])
courses.emplace(W + w + ofw[pr[t]], l + ofl[pr[t]] + 1);
}
}
if (h != pr[h]) {
auto [t, W] = tr[h][0];
for (auto [w, l] : courses) {
auto remain = K - W - w - ofw[pr[t]];
auto it = mp[pr[t]].find(remain);
if (it != mp[pr[t]].end()) {
if (!ret)
ret = l + ofl[pr[t]] + 1 + it->second;
else
ret = min(*ret, l + ofl[pr[t]] + 1 + it->second);
}
}
ofw[pr[h]] += tr[h][0].second;
ofl[pr[h]]++;
}
for (auto [t, W] : adj[h]) mp[pr[t]].clear();
for (auto [w, l] : courses)
mp[pr[h]].emplace_minimal(w - ofw[pr[h]], l - ofl[pr[h]]);
mp[pr[h]].emplace_minimal(-ofw[pr[h]], -ofl[pr[h]]);
auto it = mp[pr[h]].upper_bound(K - ofw[pr[h]]);
mp[pr[h]].erase(it, mp[pr[h]].end());
return ret;
}
};
int best_path(int N, int K, int H[][2], int L[]) {
Tree T(N);
for (int i = 0; i < N - 1; i++) {
T.emplace_edge(H[i][0], H[i][1], L[i]);
}
T.compile(0);
auto res = T.solve(0, K);
return res ? *res : -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
3 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2844 KB |
Output is correct |
26 |
Correct |
1 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2748 KB |
Output is correct |
31 |
Correct |
3 ms |
2652 KB |
Output is correct |
32 |
Correct |
2 ms |
3096 KB |
Output is correct |
33 |
Correct |
3 ms |
2652 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
1 ms |
2904 KB |
Output is correct |
36 |
Correct |
1 ms |
2652 KB |
Output is correct |
37 |
Correct |
1 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
151 ms |
29720 KB |
Output is correct |
20 |
Correct |
157 ms |
29736 KB |
Output is correct |
21 |
Correct |
145 ms |
29972 KB |
Output is correct |
22 |
Correct |
154 ms |
30036 KB |
Output is correct |
23 |
Correct |
96 ms |
24912 KB |
Output is correct |
24 |
Correct |
131 ms |
28448 KB |
Output is correct |
25 |
Correct |
84 ms |
36444 KB |
Output is correct |
26 |
Correct |
62 ms |
50512 KB |
Output is correct |
27 |
Correct |
165 ms |
39984 KB |
Output is correct |
28 |
Correct |
211 ms |
96076 KB |
Output is correct |
29 |
Correct |
198 ms |
91728 KB |
Output is correct |
30 |
Correct |
153 ms |
39960 KB |
Output is correct |
31 |
Correct |
181 ms |
40136 KB |
Output is correct |
32 |
Correct |
233 ms |
40016 KB |
Output is correct |
33 |
Correct |
202 ms |
38216 KB |
Output is correct |
34 |
Correct |
211 ms |
37620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2648 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
3 ms |
2652 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
1 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2844 KB |
Output is correct |
26 |
Correct |
1 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2748 KB |
Output is correct |
31 |
Correct |
3 ms |
2652 KB |
Output is correct |
32 |
Correct |
2 ms |
3096 KB |
Output is correct |
33 |
Correct |
3 ms |
2652 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
1 ms |
2904 KB |
Output is correct |
36 |
Correct |
1 ms |
2652 KB |
Output is correct |
37 |
Correct |
1 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2652 KB |
Output is correct |
39 |
Correct |
151 ms |
29720 KB |
Output is correct |
40 |
Correct |
157 ms |
29736 KB |
Output is correct |
41 |
Correct |
145 ms |
29972 KB |
Output is correct |
42 |
Correct |
154 ms |
30036 KB |
Output is correct |
43 |
Correct |
96 ms |
24912 KB |
Output is correct |
44 |
Correct |
131 ms |
28448 KB |
Output is correct |
45 |
Correct |
84 ms |
36444 KB |
Output is correct |
46 |
Correct |
62 ms |
50512 KB |
Output is correct |
47 |
Correct |
165 ms |
39984 KB |
Output is correct |
48 |
Correct |
211 ms |
96076 KB |
Output is correct |
49 |
Correct |
198 ms |
91728 KB |
Output is correct |
50 |
Correct |
153 ms |
39960 KB |
Output is correct |
51 |
Correct |
181 ms |
40136 KB |
Output is correct |
52 |
Correct |
233 ms |
40016 KB |
Output is correct |
53 |
Correct |
202 ms |
38216 KB |
Output is correct |
54 |
Correct |
211 ms |
37620 KB |
Output is correct |
55 |
Correct |
23 ms |
5464 KB |
Output is correct |
56 |
Correct |
11 ms |
4696 KB |
Output is correct |
57 |
Correct |
89 ms |
27996 KB |
Output is correct |
58 |
Correct |
89 ms |
30156 KB |
Output is correct |
59 |
Correct |
85 ms |
54744 KB |
Output is correct |
60 |
Correct |
272 ms |
105136 KB |
Output is correct |
61 |
Correct |
215 ms |
46160 KB |
Output is correct |
62 |
Correct |
167 ms |
49332 KB |
Output is correct |
63 |
Correct |
230 ms |
49212 KB |
Output is correct |
64 |
Correct |
709 ms |
100452 KB |
Output is correct |
65 |
Correct |
250 ms |
44344 KB |
Output is correct |
66 |
Correct |
220 ms |
85168 KB |
Output is correct |
67 |
Correct |
361 ms |
59844 KB |
Output is correct |
68 |
Correct |
344 ms |
56564 KB |
Output is correct |
69 |
Correct |
365 ms |
56868 KB |
Output is correct |
70 |
Correct |
326 ms |
54636 KB |
Output is correct |