#include <bits/stdc++.h>
#include "race.h"
using namespace std;
#define fi first
#define se second
#define _size(x) (int)x.size()
#define BIT(i, x) ((x >> i) & 1)
#define MASK(n) ((1 << n) - 1)
#define REP(i, n) for (int i = 0, _n = (n); i < _n; i++)
#define FOR(i, a, b) for (int i = a, _b = (b); i <= _b; i++)
#define FORD(i, a, b) for (int i = a, _b = (b); i >= _b; i--)
#define FORB1(i, mask) for (int i = mask; i > 0; i ^= i & - i)
#define FORB0(i, n, mask) for (int i = ((1 << n) - 1) ^ mask; i > 0; i ^= i & - i)
#define FORALL(i, a) for (auto i: a)
#define fastio ios_base::sync_with_stdio(0); cin.tie(0);
const int inf = 1e9;
struct edgeNode {
int u, v;
long long w;
};
int ans = inf;
vector<int> used, depth, inSt;
vector<long long> dist;
vector<edgeNode> edge;
vector<vector<int>> adj;
vector<set<pair<long long, int>>> st;
void connect(int id) {
adj[edge[id].u].push_back(id);
adj[edge[id].v].push_back(id);
}
void dfs(int u, long long k) {
int bestV = - 1;
vector<int> vtx;
FORALL(id, adj[u]) {
if (used[id]) continue;
used[id]++;
int v = edge[id].u + edge[id].v - u;
long long w = edge[id].w;
depth[v] = depth[u] + 1;
dist[v] = dist[u] + w;
dfs(v, k);
if (bestV == - 1 || _size(st[bestV]) < _size(st[inSt[v]])) bestV = inSt[v];
vtx.push_back(v);
}
if (bestV == - 1) {
inSt[u] = u;
st[u].insert({dist[u], depth[u]});
return ;
}
FORALL(v, vtx) {
if (inSt[v] == bestV) continue;
FORALL(e, st[inSt[v]]) {
auto it = st[bestV].lower_bound({k + dist[u] * 2 - e.fi, 0});
if (it != st[bestV].end() && it->fi == k + dist[u] * 2 - e.fi) ans = min(ans, e.se + it->se - 2 * depth[u]);
}
FORALL(e, st[inSt[v]]) {
if (e.fi - dist[u] > k) continue;
st[bestV].insert(e);
}
}
auto it = st[bestV].lower_bound({k + dist[u], 0});
if (it != st[bestV].end() && it->fi == k + dist[u]) ans = min(ans, it->se - depth[u]);
inSt[u] = bestV;
st[bestV].insert({dist[u], depth[u]});
}
int best_path(int n, int k, int h[][2], int l[]) {
edge.resize(n - 1);
adj.resize(n);
REP(i, n - 1) {
edge[i] = {h[i][0], h[i][1], l[i]};
connect(i);
}
used.resize(n - 1);
depth.resize(n);
dist.resize(n);
inSt.resize(n);
st.resize(n);
dfs(0, k);
return ans == inf ? - 1 : ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2392 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 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 |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2392 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 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 |
1 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 |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2492 KB |
Output is correct |
28 |
Correct |
1 ms |
2652 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
1 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2652 KB |
Output is correct |
33 |
Correct |
1 ms |
2652 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
1 ms |
2652 KB |
Output is correct |
36 |
Correct |
1 ms |
2652 KB |
Output is correct |
37 |
Correct |
1 ms |
2652 KB |
Output is correct |
38 |
Correct |
1 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 |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2392 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
105 ms |
33964 KB |
Output is correct |
20 |
Correct |
104 ms |
35208 KB |
Output is correct |
21 |
Correct |
108 ms |
35000 KB |
Output is correct |
22 |
Correct |
95 ms |
34128 KB |
Output is correct |
23 |
Correct |
69 ms |
30984 KB |
Output is correct |
24 |
Correct |
76 ms |
34348 KB |
Output is correct |
25 |
Correct |
98 ms |
36688 KB |
Output is correct |
26 |
Correct |
75 ms |
46740 KB |
Output is correct |
27 |
Correct |
131 ms |
48212 KB |
Output is correct |
28 |
Correct |
195 ms |
88136 KB |
Output is correct |
29 |
Correct |
187 ms |
85068 KB |
Output is correct |
30 |
Correct |
172 ms |
48212 KB |
Output is correct |
31 |
Correct |
147 ms |
48464 KB |
Output is correct |
32 |
Correct |
180 ms |
48256 KB |
Output is correct |
33 |
Correct |
117 ms |
47812 KB |
Output is correct |
34 |
Correct |
110 ms |
48688 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 |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2392 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 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 |
1 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 |
1 ms |
2652 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2492 KB |
Output is correct |
28 |
Correct |
1 ms |
2652 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
1 ms |
2652 KB |
Output is correct |
32 |
Correct |
1 ms |
2652 KB |
Output is correct |
33 |
Correct |
1 ms |
2652 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
1 ms |
2652 KB |
Output is correct |
36 |
Correct |
1 ms |
2652 KB |
Output is correct |
37 |
Correct |
1 ms |
2652 KB |
Output is correct |
38 |
Correct |
1 ms |
2652 KB |
Output is correct |
39 |
Correct |
105 ms |
33964 KB |
Output is correct |
40 |
Correct |
104 ms |
35208 KB |
Output is correct |
41 |
Correct |
108 ms |
35000 KB |
Output is correct |
42 |
Correct |
95 ms |
34128 KB |
Output is correct |
43 |
Correct |
69 ms |
30984 KB |
Output is correct |
44 |
Correct |
76 ms |
34348 KB |
Output is correct |
45 |
Correct |
98 ms |
36688 KB |
Output is correct |
46 |
Correct |
75 ms |
46740 KB |
Output is correct |
47 |
Correct |
131 ms |
48212 KB |
Output is correct |
48 |
Correct |
195 ms |
88136 KB |
Output is correct |
49 |
Correct |
187 ms |
85068 KB |
Output is correct |
50 |
Correct |
172 ms |
48212 KB |
Output is correct |
51 |
Correct |
147 ms |
48464 KB |
Output is correct |
52 |
Correct |
180 ms |
48256 KB |
Output is correct |
53 |
Correct |
117 ms |
47812 KB |
Output is correct |
54 |
Correct |
110 ms |
48688 KB |
Output is correct |
55 |
Correct |
11 ms |
6232 KB |
Output is correct |
56 |
Correct |
7 ms |
5112 KB |
Output is correct |
57 |
Correct |
57 ms |
30948 KB |
Output is correct |
58 |
Correct |
41 ms |
26524 KB |
Output is correct |
59 |
Correct |
71 ms |
46368 KB |
Output is correct |
60 |
Correct |
194 ms |
86864 KB |
Output is correct |
61 |
Correct |
164 ms |
51540 KB |
Output is correct |
62 |
Correct |
152 ms |
51700 KB |
Output is correct |
63 |
Correct |
182 ms |
51644 KB |
Output is correct |
64 |
Correct |
317 ms |
105040 KB |
Output is correct |
65 |
Correct |
163 ms |
48748 KB |
Output is correct |
66 |
Correct |
231 ms |
78656 KB |
Output is correct |
67 |
Correct |
130 ms |
49604 KB |
Output is correct |
68 |
Correct |
272 ms |
66656 KB |
Output is correct |
69 |
Correct |
272 ms |
67412 KB |
Output is correct |
70 |
Correct |
252 ms |
64084 KB |
Output is correct |