#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <climits>
#include <cmath>
#include <fstream>
#include <queue>
#include <stack>
#include <bitset>
#include "race.h"
using namespace std;
#define ll long long
//cin.tie(0);ios_base::sync_with_stdio(0);
const int MAXN = 2 * 1e5 + 5;
const int MAXV = 1e6 + 5;
const int INF = 1e9;
int n;
ll k;
vector<pair<int, int>> adjList[MAXN];
bool found[MAXN];
int idx = -1;
int s[MAXN];
int vals[MAXV];
set<int> used;
int ans = INF;
int tot = 0;
queue<pair<int, int>> add;
void cnt(int src, int p) {
tot++;
for (auto i : adjList[src]) {
if (i.first == p || found[i.first]) {
continue;
}
cnt(i.first, src);
}
}
void centroid(int src, int p) {
s[src] = 1;
int mxVal = 0;
for (auto i : adjList[src]) {
if (i.first == p || found[i.first]) {
continue;
}
centroid(i.first, src);
s[src] += s[i.first];
mxVal = max(mxVal, s[i.first]);
}
if (mxVal <= tot / 2 && tot - s[src] <= tot / 2) {
idx = src;
}
}
void dfs1(int src, int p, int cur, int paths) {
for (auto i : adjList[src]) {
if (i.first == p || found[i.first]) {
continue;
}
int nxt = cur + i.second;
if (nxt > k) {
continue;
}
ans = min(ans, vals[k - nxt] + paths + 1);
add.push({ nxt, paths + 1 });
dfs1(i.first, src, nxt, paths + 1);
if (src == idx) {
while (add.size() > 0) {
int rn = add.front().first;
vals[rn] = min(vals[rn], add.front().second);
used.insert(rn);
add.pop();
}
}
}
}
void dfs(int src, int p) {
idx = -1;
tot = 0;
cnt(src, p);
centroid(src, p);
if (tot == 1) {
return;
}
found[idx] = true;
dfs1(idx, idx, 0, 0);
for (auto i : used) {
vals[i] = INF;
}
used.clear();
for (auto i : adjList[idx]) {
if (i.first == p || found[i.first]) {
continue;
}
dfs(i.first, src);
}
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N;
k = K;
for (int i = 0; i < n - 1; i++) {
adjList[H[i][0]].push_back({ H[i][1], L[i] });
adjList[H[i][1]].push_back({ H[i][0], L[i] });
}
for (int i = 0; i < MAXV; i++) {
vals[i] = INF;
}
vals[0] = 0;
dfs(0, 0);
if (ans > n) {
return -1;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8940 KB |
Output is correct |
2 |
Correct |
8 ms |
8940 KB |
Output is correct |
3 |
Correct |
8 ms |
8940 KB |
Output is correct |
4 |
Correct |
7 ms |
8940 KB |
Output is correct |
5 |
Correct |
7 ms |
9088 KB |
Output is correct |
6 |
Correct |
7 ms |
8940 KB |
Output is correct |
7 |
Correct |
7 ms |
8932 KB |
Output is correct |
8 |
Correct |
7 ms |
8940 KB |
Output is correct |
9 |
Correct |
7 ms |
8940 KB |
Output is correct |
10 |
Correct |
9 ms |
8940 KB |
Output is correct |
11 |
Correct |
7 ms |
8940 KB |
Output is correct |
12 |
Correct |
7 ms |
8940 KB |
Output is correct |
13 |
Correct |
8 ms |
9068 KB |
Output is correct |
14 |
Correct |
7 ms |
8940 KB |
Output is correct |
15 |
Correct |
7 ms |
8940 KB |
Output is correct |
16 |
Correct |
7 ms |
8940 KB |
Output is correct |
17 |
Correct |
7 ms |
9088 KB |
Output is correct |
18 |
Correct |
7 ms |
8940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8940 KB |
Output is correct |
2 |
Correct |
8 ms |
8940 KB |
Output is correct |
3 |
Correct |
8 ms |
8940 KB |
Output is correct |
4 |
Correct |
7 ms |
8940 KB |
Output is correct |
5 |
Correct |
7 ms |
9088 KB |
Output is correct |
6 |
Correct |
7 ms |
8940 KB |
Output is correct |
7 |
Correct |
7 ms |
8932 KB |
Output is correct |
8 |
Correct |
7 ms |
8940 KB |
Output is correct |
9 |
Correct |
7 ms |
8940 KB |
Output is correct |
10 |
Correct |
9 ms |
8940 KB |
Output is correct |
11 |
Correct |
7 ms |
8940 KB |
Output is correct |
12 |
Correct |
7 ms |
8940 KB |
Output is correct |
13 |
Correct |
8 ms |
9068 KB |
Output is correct |
14 |
Correct |
7 ms |
8940 KB |
Output is correct |
15 |
Correct |
7 ms |
8940 KB |
Output is correct |
16 |
Correct |
7 ms |
8940 KB |
Output is correct |
17 |
Correct |
7 ms |
9088 KB |
Output is correct |
18 |
Correct |
7 ms |
8940 KB |
Output is correct |
19 |
Correct |
7 ms |
8940 KB |
Output is correct |
20 |
Correct |
7 ms |
8940 KB |
Output is correct |
21 |
Correct |
8 ms |
9196 KB |
Output is correct |
22 |
Correct |
8 ms |
9068 KB |
Output is correct |
23 |
Correct |
8 ms |
9068 KB |
Output is correct |
24 |
Correct |
8 ms |
9068 KB |
Output is correct |
25 |
Correct |
8 ms |
9068 KB |
Output is correct |
26 |
Correct |
7 ms |
9068 KB |
Output is correct |
27 |
Correct |
7 ms |
9068 KB |
Output is correct |
28 |
Correct |
8 ms |
9068 KB |
Output is correct |
29 |
Correct |
8 ms |
9068 KB |
Output is correct |
30 |
Correct |
9 ms |
9068 KB |
Output is correct |
31 |
Correct |
8 ms |
9068 KB |
Output is correct |
32 |
Correct |
9 ms |
9068 KB |
Output is correct |
33 |
Correct |
9 ms |
9068 KB |
Output is correct |
34 |
Correct |
8 ms |
9068 KB |
Output is correct |
35 |
Correct |
8 ms |
9196 KB |
Output is correct |
36 |
Correct |
8 ms |
9068 KB |
Output is correct |
37 |
Correct |
9 ms |
9100 KB |
Output is correct |
38 |
Correct |
9 ms |
9068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8940 KB |
Output is correct |
2 |
Correct |
8 ms |
8940 KB |
Output is correct |
3 |
Correct |
8 ms |
8940 KB |
Output is correct |
4 |
Correct |
7 ms |
8940 KB |
Output is correct |
5 |
Correct |
7 ms |
9088 KB |
Output is correct |
6 |
Correct |
7 ms |
8940 KB |
Output is correct |
7 |
Correct |
7 ms |
8932 KB |
Output is correct |
8 |
Correct |
7 ms |
8940 KB |
Output is correct |
9 |
Correct |
7 ms |
8940 KB |
Output is correct |
10 |
Correct |
9 ms |
8940 KB |
Output is correct |
11 |
Correct |
7 ms |
8940 KB |
Output is correct |
12 |
Correct |
7 ms |
8940 KB |
Output is correct |
13 |
Correct |
8 ms |
9068 KB |
Output is correct |
14 |
Correct |
7 ms |
8940 KB |
Output is correct |
15 |
Correct |
7 ms |
8940 KB |
Output is correct |
16 |
Correct |
7 ms |
8940 KB |
Output is correct |
17 |
Correct |
7 ms |
9088 KB |
Output is correct |
18 |
Correct |
7 ms |
8940 KB |
Output is correct |
19 |
Correct |
245 ms |
15460 KB |
Output is correct |
20 |
Correct |
222 ms |
15596 KB |
Output is correct |
21 |
Correct |
209 ms |
16100 KB |
Output is correct |
22 |
Correct |
213 ms |
16100 KB |
Output is correct |
23 |
Correct |
116 ms |
16152 KB |
Output is correct |
24 |
Correct |
88 ms |
15844 KB |
Output is correct |
25 |
Correct |
168 ms |
19172 KB |
Output is correct |
26 |
Correct |
127 ms |
23396 KB |
Output is correct |
27 |
Correct |
265 ms |
22628 KB |
Output is correct |
28 |
Correct |
386 ms |
37092 KB |
Output is correct |
29 |
Correct |
393 ms |
35940 KB |
Output is correct |
30 |
Correct |
268 ms |
22868 KB |
Output is correct |
31 |
Correct |
258 ms |
22884 KB |
Output is correct |
32 |
Correct |
307 ms |
22624 KB |
Output is correct |
33 |
Correct |
357 ms |
21508 KB |
Output is correct |
34 |
Correct |
279 ms |
21476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
8940 KB |
Output is correct |
2 |
Correct |
8 ms |
8940 KB |
Output is correct |
3 |
Correct |
8 ms |
8940 KB |
Output is correct |
4 |
Correct |
7 ms |
8940 KB |
Output is correct |
5 |
Correct |
7 ms |
9088 KB |
Output is correct |
6 |
Correct |
7 ms |
8940 KB |
Output is correct |
7 |
Correct |
7 ms |
8932 KB |
Output is correct |
8 |
Correct |
7 ms |
8940 KB |
Output is correct |
9 |
Correct |
7 ms |
8940 KB |
Output is correct |
10 |
Correct |
9 ms |
8940 KB |
Output is correct |
11 |
Correct |
7 ms |
8940 KB |
Output is correct |
12 |
Correct |
7 ms |
8940 KB |
Output is correct |
13 |
Correct |
8 ms |
9068 KB |
Output is correct |
14 |
Correct |
7 ms |
8940 KB |
Output is correct |
15 |
Correct |
7 ms |
8940 KB |
Output is correct |
16 |
Correct |
7 ms |
8940 KB |
Output is correct |
17 |
Correct |
7 ms |
9088 KB |
Output is correct |
18 |
Correct |
7 ms |
8940 KB |
Output is correct |
19 |
Correct |
7 ms |
8940 KB |
Output is correct |
20 |
Correct |
7 ms |
8940 KB |
Output is correct |
21 |
Correct |
8 ms |
9196 KB |
Output is correct |
22 |
Correct |
8 ms |
9068 KB |
Output is correct |
23 |
Correct |
8 ms |
9068 KB |
Output is correct |
24 |
Correct |
8 ms |
9068 KB |
Output is correct |
25 |
Correct |
8 ms |
9068 KB |
Output is correct |
26 |
Correct |
7 ms |
9068 KB |
Output is correct |
27 |
Correct |
7 ms |
9068 KB |
Output is correct |
28 |
Correct |
8 ms |
9068 KB |
Output is correct |
29 |
Correct |
8 ms |
9068 KB |
Output is correct |
30 |
Correct |
9 ms |
9068 KB |
Output is correct |
31 |
Correct |
8 ms |
9068 KB |
Output is correct |
32 |
Correct |
9 ms |
9068 KB |
Output is correct |
33 |
Correct |
9 ms |
9068 KB |
Output is correct |
34 |
Correct |
8 ms |
9068 KB |
Output is correct |
35 |
Correct |
8 ms |
9196 KB |
Output is correct |
36 |
Correct |
8 ms |
9068 KB |
Output is correct |
37 |
Correct |
9 ms |
9100 KB |
Output is correct |
38 |
Correct |
9 ms |
9068 KB |
Output is correct |
39 |
Correct |
245 ms |
15460 KB |
Output is correct |
40 |
Correct |
222 ms |
15596 KB |
Output is correct |
41 |
Correct |
209 ms |
16100 KB |
Output is correct |
42 |
Correct |
213 ms |
16100 KB |
Output is correct |
43 |
Correct |
116 ms |
16152 KB |
Output is correct |
44 |
Correct |
88 ms |
15844 KB |
Output is correct |
45 |
Correct |
168 ms |
19172 KB |
Output is correct |
46 |
Correct |
127 ms |
23396 KB |
Output is correct |
47 |
Correct |
265 ms |
22628 KB |
Output is correct |
48 |
Correct |
386 ms |
37092 KB |
Output is correct |
49 |
Correct |
393 ms |
35940 KB |
Output is correct |
50 |
Correct |
268 ms |
22868 KB |
Output is correct |
51 |
Correct |
258 ms |
22884 KB |
Output is correct |
52 |
Correct |
307 ms |
22624 KB |
Output is correct |
53 |
Correct |
357 ms |
21508 KB |
Output is correct |
54 |
Correct |
279 ms |
21476 KB |
Output is correct |
55 |
Correct |
21 ms |
9708 KB |
Output is correct |
56 |
Correct |
20 ms |
9708 KB |
Output is correct |
57 |
Correct |
141 ms |
16100 KB |
Output is correct |
58 |
Correct |
50 ms |
15448 KB |
Output is correct |
59 |
Correct |
258 ms |
26596 KB |
Output is correct |
60 |
Correct |
868 ms |
45032 KB |
Output is correct |
61 |
Correct |
310 ms |
23012 KB |
Output is correct |
62 |
Correct |
350 ms |
22756 KB |
Output is correct |
63 |
Correct |
438 ms |
22884 KB |
Output is correct |
64 |
Correct |
1089 ms |
27112 KB |
Output is correct |
65 |
Correct |
388 ms |
22716 KB |
Output is correct |
66 |
Correct |
794 ms |
33640 KB |
Output is correct |
67 |
Correct |
166 ms |
23120 KB |
Output is correct |
68 |
Correct |
510 ms |
26340 KB |
Output is correct |
69 |
Correct |
490 ms |
26340 KB |
Output is correct |
70 |
Correct |
478 ms |
25828 KB |
Output is correct |