#include "race.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define all(v) v.begin(), v.end()
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2")
vector<vector<pair<int, int>>> adj;
vector<int> centroid;
vector<int> sz;
int k;
int szDfs(int a, int p){
sz[a] = 1;
for (auto &[b, _] : adj[a]) {
if(b == p || centroid[b]) continue;
sz[a] += szDfs(b, a);
}
return sz[a];
}
int getCentroid(int a, int p, int curSz){
for (auto &[b, _] : adj[a]) {
if(b == p || centroid[b]) continue;
if(sz[b] * 2 > curSz) return getCentroid(b, a, curSz);
}
return a;
}
void dDfs(int a, int p, int d, int dc, vector<pair<int, int>> &dst){
dst.push_back({dc, d});
for (auto &[b, c] : adj[a]) {
if(b == p || centroid[b]) continue;
dDfs(b, a, d + 1, dc + c, dst);
}
}
int getBest(int a){
multiset<pair<int, int>> cur;
cur.insert({0, 0});
vector<vector<pair<int, int>>> ch;
int res = INT_MAX;
int ctd = getCentroid(a, a, szDfs(a, a));
centroid[ctd] = true;
for (auto [b, c] : adj[ctd]) {
if(centroid[b]) continue;
ch.push_back({});
dDfs(b, b, 1, c, ch.back());
}
for (auto &&i : ch) for (auto &&j : i){
cur.insert(j);
}
for (auto &&i : ch) {
for (auto &&j : i) cur.erase(cur.find(j));
for (auto &&j : i) {
if(cur.lower_bound({k - j.first, 0}) == cur.end()) continue;
pair<int, int> x = *cur.lower_bound({k - j.first, 0});
if(x.first == k - j.first){
res = min(res, x.second + j.second);
}
}
for (auto &&j : i) cur.insert(j);
}
for (auto &[b, _] : adj[ctd]) {
if(centroid[b]) continue;
res = min(res, getBest(b));
}
return res;
}
int best_path(int N, int K, int H[][2], int L[])
{
k = K;
adj.assign(N, {});
sz.assign(N, 0);
centroid.assign(N, false);
for (int i = 0; i < N - 1; i++) {
adj[H[i][0]].push_back({H[i][1], L[i]});
adj[H[i][1]].push_back({H[i][0], L[i]});
}
int ret = getBest(0);
if(ret == INT_MAX) return -1;
return ret;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2492 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2472 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2552 KB |
Output is correct |
9 |
Correct |
1 ms |
2732 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 |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 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 |
2492 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2472 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2552 KB |
Output is correct |
9 |
Correct |
1 ms |
2732 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 |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 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 |
2 ms |
2652 KB |
Output is correct |
22 |
Correct |
3 ms |
2652 KB |
Output is correct |
23 |
Correct |
3 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2664 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
3 ms |
2652 KB |
Output is correct |
30 |
Correct |
3 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
2 ms |
2652 KB |
Output is correct |
33 |
Correct |
3 ms |
2652 KB |
Output is correct |
34 |
Correct |
2 ms |
2652 KB |
Output is correct |
35 |
Correct |
2 ms |
2652 KB |
Output is correct |
36 |
Correct |
2 ms |
2500 KB |
Output is correct |
37 |
Correct |
2 ms |
2648 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 |
2492 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2472 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2552 KB |
Output is correct |
9 |
Correct |
1 ms |
2732 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 |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
474 ms |
22864 KB |
Output is correct |
20 |
Correct |
459 ms |
23064 KB |
Output is correct |
21 |
Correct |
463 ms |
23368 KB |
Output is correct |
22 |
Correct |
396 ms |
22964 KB |
Output is correct |
23 |
Correct |
551 ms |
22280 KB |
Output is correct |
24 |
Correct |
282 ms |
20172 KB |
Output is correct |
25 |
Correct |
467 ms |
27968 KB |
Output is correct |
26 |
Correct |
347 ms |
29784 KB |
Output is correct |
27 |
Correct |
460 ms |
33084 KB |
Output is correct |
28 |
Correct |
1045 ms |
55056 KB |
Output is correct |
29 |
Correct |
1066 ms |
54316 KB |
Output is correct |
30 |
Correct |
461 ms |
32908 KB |
Output is correct |
31 |
Correct |
459 ms |
33092 KB |
Output is correct |
32 |
Correct |
487 ms |
32896 KB |
Output is correct |
33 |
Correct |
788 ms |
41404 KB |
Output is correct |
34 |
Correct |
1000 ms |
40892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2492 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2472 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2552 KB |
Output is correct |
9 |
Correct |
1 ms |
2732 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 |
2652 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 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 |
2 ms |
2652 KB |
Output is correct |
22 |
Correct |
3 ms |
2652 KB |
Output is correct |
23 |
Correct |
3 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
2 ms |
2664 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
2 ms |
2652 KB |
Output is correct |
28 |
Correct |
2 ms |
2652 KB |
Output is correct |
29 |
Correct |
3 ms |
2652 KB |
Output is correct |
30 |
Correct |
3 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
2 ms |
2652 KB |
Output is correct |
33 |
Correct |
3 ms |
2652 KB |
Output is correct |
34 |
Correct |
2 ms |
2652 KB |
Output is correct |
35 |
Correct |
2 ms |
2652 KB |
Output is correct |
36 |
Correct |
2 ms |
2500 KB |
Output is correct |
37 |
Correct |
2 ms |
2648 KB |
Output is correct |
38 |
Correct |
2 ms |
2652 KB |
Output is correct |
39 |
Correct |
474 ms |
22864 KB |
Output is correct |
40 |
Correct |
459 ms |
23064 KB |
Output is correct |
41 |
Correct |
463 ms |
23368 KB |
Output is correct |
42 |
Correct |
396 ms |
22964 KB |
Output is correct |
43 |
Correct |
551 ms |
22280 KB |
Output is correct |
44 |
Correct |
282 ms |
20172 KB |
Output is correct |
45 |
Correct |
467 ms |
27968 KB |
Output is correct |
46 |
Correct |
347 ms |
29784 KB |
Output is correct |
47 |
Correct |
460 ms |
33084 KB |
Output is correct |
48 |
Correct |
1045 ms |
55056 KB |
Output is correct |
49 |
Correct |
1066 ms |
54316 KB |
Output is correct |
50 |
Correct |
461 ms |
32908 KB |
Output is correct |
51 |
Correct |
459 ms |
33092 KB |
Output is correct |
52 |
Correct |
487 ms |
32896 KB |
Output is correct |
53 |
Correct |
788 ms |
41404 KB |
Output is correct |
54 |
Correct |
1000 ms |
40892 KB |
Output is correct |
55 |
Correct |
28 ms |
4184 KB |
Output is correct |
56 |
Correct |
28 ms |
4396 KB |
Output is correct |
57 |
Correct |
466 ms |
22316 KB |
Output is correct |
58 |
Correct |
125 ms |
22940 KB |
Output is correct |
59 |
Correct |
399 ms |
29776 KB |
Output is correct |
60 |
Correct |
1100 ms |
54240 KB |
Output is correct |
61 |
Correct |
472 ms |
32948 KB |
Output is correct |
62 |
Correct |
464 ms |
33072 KB |
Output is correct |
63 |
Correct |
501 ms |
32852 KB |
Output is correct |
64 |
Correct |
1055 ms |
42948 KB |
Output is correct |
65 |
Correct |
915 ms |
41852 KB |
Output is correct |
66 |
Correct |
1036 ms |
52408 KB |
Output is correct |
67 |
Correct |
270 ms |
43684 KB |
Output is correct |
68 |
Correct |
550 ms |
38812 KB |
Output is correct |
69 |
Correct |
545 ms |
39212 KB |
Output is correct |
70 |
Correct |
488 ms |
37256 KB |
Output is correct |