#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define sp << ' ' <<
#define nl << '\n'
#define v e.first
#define w e.second
#include "race.h"
const int MAXN = 2e5, INF = 1e9;
int ans = INF, k;
vector<vector<pair<int, int>>> g(MAXN);
vector<int> s(MAXN), len(1000001, INF);
vector<bool> r(MAXN);
vector<int> toUpdate;
int calcSize(int u, int p){
s[u] = 1;
for(auto &e : g[u]) if(v != p and !r[v]) s[u] += calcSize(v, u);
return s[u];
}
void calcDist(int u, int p, int currLength, int currDist, int root){
if(currDist>k) return;
if(len[currDist]==INF) toUpdate.push_back(currDist);
len[currDist] = min(len[currDist], currLength);
for(auto &e : g[u]) if(v != p and !r[v]) calcDist(v, u, currLength+1, currDist+w, root);
}
void dfs(int u, int p, int currLength, int currDist, int root){
if(currDist>k) return;
ans = min(ans, len[k-currDist] + currLength);
for(auto &e : g[u]) if(v != p and !r[v]) dfs(v, u, currLength+1, currDist+w, root);
}
int find(int u, int p, int treeSize){
for(auto &e : g[u]) if(v != p and !r[v] and s[v] > treeSize/2) return find(v, u, treeSize);
return u;
}
void decompose(int u){
int c = find(u, u, calcSize(u, u));
r[c] = true;
for(auto &e : g[c]){
if(r[v]) continue;
dfs(v, c, 1, w, c);
calcDist(v, c, 1, w, c);
}
ans = min(ans, len[k]);
while(!toUpdate.empty()) len[toUpdate.back()] = INF, toUpdate.pop_back();
for(auto &e : g[c]) if(!r[v]) decompose(v);
}
int best_path(int N, int K, int H[][2], int L[]){
k = K;
for(int i=0; i<N-1; ++i){
g[H[i][0]].emplace_back(H[i][1], L[i]);
g[H[i][1]].emplace_back(H[i][0], L[i]);
}
decompose(0);
if(ans>N) ans = -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9680 KB |
Output is correct |
6 |
Correct |
6 ms |
9676 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9676 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9676 KB |
Output is correct |
13 |
Correct |
6 ms |
9680 KB |
Output is correct |
14 |
Correct |
6 ms |
9676 KB |
Output is correct |
15 |
Correct |
6 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
6 ms |
9652 KB |
Output is correct |
18 |
Correct |
6 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9680 KB |
Output is correct |
6 |
Correct |
6 ms |
9676 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9676 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9676 KB |
Output is correct |
13 |
Correct |
6 ms |
9680 KB |
Output is correct |
14 |
Correct |
6 ms |
9676 KB |
Output is correct |
15 |
Correct |
6 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
6 ms |
9652 KB |
Output is correct |
18 |
Correct |
6 ms |
9676 KB |
Output is correct |
19 |
Correct |
7 ms |
9676 KB |
Output is correct |
20 |
Correct |
6 ms |
9676 KB |
Output is correct |
21 |
Correct |
6 ms |
9676 KB |
Output is correct |
22 |
Correct |
7 ms |
9676 KB |
Output is correct |
23 |
Correct |
7 ms |
9676 KB |
Output is correct |
24 |
Correct |
7 ms |
9676 KB |
Output is correct |
25 |
Correct |
7 ms |
9676 KB |
Output is correct |
26 |
Correct |
8 ms |
9676 KB |
Output is correct |
27 |
Correct |
7 ms |
9676 KB |
Output is correct |
28 |
Correct |
7 ms |
9792 KB |
Output is correct |
29 |
Correct |
7 ms |
9676 KB |
Output is correct |
30 |
Correct |
7 ms |
9676 KB |
Output is correct |
31 |
Correct |
7 ms |
9804 KB |
Output is correct |
32 |
Correct |
7 ms |
9676 KB |
Output is correct |
33 |
Correct |
7 ms |
9676 KB |
Output is correct |
34 |
Correct |
6 ms |
9804 KB |
Output is correct |
35 |
Correct |
7 ms |
9804 KB |
Output is correct |
36 |
Correct |
7 ms |
9804 KB |
Output is correct |
37 |
Correct |
7 ms |
9804 KB |
Output is correct |
38 |
Correct |
7 ms |
9676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9680 KB |
Output is correct |
6 |
Correct |
6 ms |
9676 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9676 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9676 KB |
Output is correct |
13 |
Correct |
6 ms |
9680 KB |
Output is correct |
14 |
Correct |
6 ms |
9676 KB |
Output is correct |
15 |
Correct |
6 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
6 ms |
9652 KB |
Output is correct |
18 |
Correct |
6 ms |
9676 KB |
Output is correct |
19 |
Correct |
146 ms |
14476 KB |
Output is correct |
20 |
Correct |
142 ms |
14528 KB |
Output is correct |
21 |
Correct |
166 ms |
14488 KB |
Output is correct |
22 |
Correct |
172 ms |
14556 KB |
Output is correct |
23 |
Correct |
95 ms |
14788 KB |
Output is correct |
24 |
Correct |
72 ms |
14708 KB |
Output is correct |
25 |
Correct |
165 ms |
18196 KB |
Output is correct |
26 |
Correct |
108 ms |
21840 KB |
Output is correct |
27 |
Correct |
208 ms |
19632 KB |
Output is correct |
28 |
Correct |
333 ms |
37000 KB |
Output is correct |
29 |
Correct |
367 ms |
35884 KB |
Output is correct |
30 |
Correct |
215 ms |
22552 KB |
Output is correct |
31 |
Correct |
206 ms |
22536 KB |
Output is correct |
32 |
Correct |
298 ms |
22660 KB |
Output is correct |
33 |
Correct |
243 ms |
21428 KB |
Output is correct |
34 |
Correct |
212 ms |
22340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9676 KB |
Output is correct |
3 |
Correct |
6 ms |
9676 KB |
Output is correct |
4 |
Correct |
6 ms |
9676 KB |
Output is correct |
5 |
Correct |
6 ms |
9680 KB |
Output is correct |
6 |
Correct |
6 ms |
9676 KB |
Output is correct |
7 |
Correct |
6 ms |
9676 KB |
Output is correct |
8 |
Correct |
6 ms |
9676 KB |
Output is correct |
9 |
Correct |
6 ms |
9676 KB |
Output is correct |
10 |
Correct |
6 ms |
9676 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
6 ms |
9676 KB |
Output is correct |
13 |
Correct |
6 ms |
9680 KB |
Output is correct |
14 |
Correct |
6 ms |
9676 KB |
Output is correct |
15 |
Correct |
6 ms |
9676 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
6 ms |
9652 KB |
Output is correct |
18 |
Correct |
6 ms |
9676 KB |
Output is correct |
19 |
Correct |
7 ms |
9676 KB |
Output is correct |
20 |
Correct |
6 ms |
9676 KB |
Output is correct |
21 |
Correct |
6 ms |
9676 KB |
Output is correct |
22 |
Correct |
7 ms |
9676 KB |
Output is correct |
23 |
Correct |
7 ms |
9676 KB |
Output is correct |
24 |
Correct |
7 ms |
9676 KB |
Output is correct |
25 |
Correct |
7 ms |
9676 KB |
Output is correct |
26 |
Correct |
8 ms |
9676 KB |
Output is correct |
27 |
Correct |
7 ms |
9676 KB |
Output is correct |
28 |
Correct |
7 ms |
9792 KB |
Output is correct |
29 |
Correct |
7 ms |
9676 KB |
Output is correct |
30 |
Correct |
7 ms |
9676 KB |
Output is correct |
31 |
Correct |
7 ms |
9804 KB |
Output is correct |
32 |
Correct |
7 ms |
9676 KB |
Output is correct |
33 |
Correct |
7 ms |
9676 KB |
Output is correct |
34 |
Correct |
6 ms |
9804 KB |
Output is correct |
35 |
Correct |
7 ms |
9804 KB |
Output is correct |
36 |
Correct |
7 ms |
9804 KB |
Output is correct |
37 |
Correct |
7 ms |
9804 KB |
Output is correct |
38 |
Correct |
7 ms |
9676 KB |
Output is correct |
39 |
Correct |
146 ms |
14476 KB |
Output is correct |
40 |
Correct |
142 ms |
14528 KB |
Output is correct |
41 |
Correct |
166 ms |
14488 KB |
Output is correct |
42 |
Correct |
172 ms |
14556 KB |
Output is correct |
43 |
Correct |
95 ms |
14788 KB |
Output is correct |
44 |
Correct |
72 ms |
14708 KB |
Output is correct |
45 |
Correct |
165 ms |
18196 KB |
Output is correct |
46 |
Correct |
108 ms |
21840 KB |
Output is correct |
47 |
Correct |
208 ms |
19632 KB |
Output is correct |
48 |
Correct |
333 ms |
37000 KB |
Output is correct |
49 |
Correct |
367 ms |
35884 KB |
Output is correct |
50 |
Correct |
215 ms |
22552 KB |
Output is correct |
51 |
Correct |
206 ms |
22536 KB |
Output is correct |
52 |
Correct |
298 ms |
22660 KB |
Output is correct |
53 |
Correct |
243 ms |
21428 KB |
Output is correct |
54 |
Correct |
212 ms |
22340 KB |
Output is correct |
55 |
Correct |
15 ms |
10408 KB |
Output is correct |
56 |
Correct |
16 ms |
10264 KB |
Output is correct |
57 |
Correct |
94 ms |
16092 KB |
Output is correct |
58 |
Correct |
45 ms |
15868 KB |
Output is correct |
59 |
Correct |
123 ms |
23752 KB |
Output is correct |
60 |
Correct |
565 ms |
37264 KB |
Output is correct |
61 |
Correct |
228 ms |
22756 KB |
Output is correct |
62 |
Correct |
267 ms |
22640 KB |
Output is correct |
63 |
Correct |
334 ms |
22688 KB |
Output is correct |
64 |
Correct |
631 ms |
23512 KB |
Output is correct |
65 |
Correct |
335 ms |
23428 KB |
Output is correct |
66 |
Correct |
491 ms |
33420 KB |
Output is correct |
67 |
Correct |
145 ms |
23220 KB |
Output is correct |
68 |
Correct |
362 ms |
24004 KB |
Output is correct |
69 |
Correct |
344 ms |
24132 KB |
Output is correct |
70 |
Correct |
326 ms |
23488 KB |
Output is correct |