#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#define INF 1e9
using namespace std;
typedef long long ll;
const int MAX = 1e6 + 5;
int n, k, ans = INF, dp[MAX];
vector<int>cnt(MAX, INF);
bool visited[MAX];
vector<int>v;
vector<pair<int, int>>adj[MAX];
int get_size(int s, int e) {
dp[s] = 1;
for (auto u : adj[s]) {
if (u.first == e || visited[u.first]) continue;
get_size(u.first, s);
dp[s] += dp[u.first];
}
return dp[s];
}
int get_centroid(int s, int e, int n) {
for (auto u : adj[s]) {
if (u.first == e || visited[u.first])continue;
if (dp[u.first] * 2 > n) return get_centroid(u.first, s, n);
}
return s;
}
void dfs(int s, int e, bool flag, int sum, int dep) {
if (sum > k)return;
if (!flag) {
ll need = k - sum;
if (cnt[need] != 1e9) {
ans = min(ans, dep + cnt[need]);
}
}
else {
cnt[sum] = min(cnt[sum], dep);
v.push_back(sum);
}
for (auto u : adj[s]) {
if (u.first == e || visited[u.first])continue;
dfs(u.first, s, flag, sum + u.second, dep + 1);
}
}
void centroid_decomposition(int node) {
int centroid = get_centroid(node, -1, get_size(node, -1));
visited[centroid] = true;
cnt[0] = 0;
for (pair<int, int> u : adj[centroid]) {
if (!visited[u.first]) {
dfs(u.first, centroid, false, u.second, 1);
dfs(u.first, centroid, true, u.second, 1);
}
}
for (auto u : v) cnt[u] = INF;
v.clear();
for (auto u : adj[centroid]) {
if (!visited[u.first])centroid_decomposition(u.first);
}
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N; k = K;
for (int i = 0; i < n - 1; i++) {
int u = H[i][0], v = H[i][1], w = L[i];
adj[u].push_back({ v, w });
adj[v].push_back({ u, w });
}
centroid_decomposition(1);
if (ans >= 1e9) return -1;
else return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
32860 KB |
Output is correct |
2 |
Correct |
7 ms |
32860 KB |
Output is correct |
3 |
Correct |
7 ms |
32860 KB |
Output is correct |
4 |
Correct |
7 ms |
32860 KB |
Output is correct |
5 |
Correct |
7 ms |
32856 KB |
Output is correct |
6 |
Correct |
7 ms |
33040 KB |
Output is correct |
7 |
Correct |
7 ms |
32856 KB |
Output is correct |
8 |
Correct |
7 ms |
32860 KB |
Output is correct |
9 |
Correct |
7 ms |
33072 KB |
Output is correct |
10 |
Correct |
7 ms |
32856 KB |
Output is correct |
11 |
Correct |
7 ms |
32856 KB |
Output is correct |
12 |
Correct |
7 ms |
32856 KB |
Output is correct |
13 |
Correct |
7 ms |
32856 KB |
Output is correct |
14 |
Correct |
7 ms |
32860 KB |
Output is correct |
15 |
Correct |
7 ms |
32860 KB |
Output is correct |
16 |
Correct |
7 ms |
32856 KB |
Output is correct |
17 |
Correct |
7 ms |
32860 KB |
Output is correct |
18 |
Correct |
9 ms |
33076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
32860 KB |
Output is correct |
2 |
Correct |
7 ms |
32860 KB |
Output is correct |
3 |
Correct |
7 ms |
32860 KB |
Output is correct |
4 |
Correct |
7 ms |
32860 KB |
Output is correct |
5 |
Correct |
7 ms |
32856 KB |
Output is correct |
6 |
Correct |
7 ms |
33040 KB |
Output is correct |
7 |
Correct |
7 ms |
32856 KB |
Output is correct |
8 |
Correct |
7 ms |
32860 KB |
Output is correct |
9 |
Correct |
7 ms |
33072 KB |
Output is correct |
10 |
Correct |
7 ms |
32856 KB |
Output is correct |
11 |
Correct |
7 ms |
32856 KB |
Output is correct |
12 |
Correct |
7 ms |
32856 KB |
Output is correct |
13 |
Correct |
7 ms |
32856 KB |
Output is correct |
14 |
Correct |
7 ms |
32860 KB |
Output is correct |
15 |
Correct |
7 ms |
32860 KB |
Output is correct |
16 |
Correct |
7 ms |
32856 KB |
Output is correct |
17 |
Correct |
7 ms |
32860 KB |
Output is correct |
18 |
Correct |
9 ms |
33076 KB |
Output is correct |
19 |
Correct |
7 ms |
32860 KB |
Output is correct |
20 |
Correct |
7 ms |
32860 KB |
Output is correct |
21 |
Correct |
7 ms |
33116 KB |
Output is correct |
22 |
Correct |
8 ms |
33076 KB |
Output is correct |
23 |
Correct |
7 ms |
33112 KB |
Output is correct |
24 |
Correct |
8 ms |
33116 KB |
Output is correct |
25 |
Correct |
8 ms |
33112 KB |
Output is correct |
26 |
Correct |
7 ms |
33112 KB |
Output is correct |
27 |
Correct |
8 ms |
33116 KB |
Output is correct |
28 |
Correct |
7 ms |
33116 KB |
Output is correct |
29 |
Correct |
8 ms |
33112 KB |
Output is correct |
30 |
Correct |
9 ms |
33116 KB |
Output is correct |
31 |
Correct |
8 ms |
33112 KB |
Output is correct |
32 |
Correct |
10 ms |
33112 KB |
Output is correct |
33 |
Correct |
8 ms |
33116 KB |
Output is correct |
34 |
Correct |
8 ms |
33116 KB |
Output is correct |
35 |
Correct |
9 ms |
33036 KB |
Output is correct |
36 |
Correct |
8 ms |
33116 KB |
Output is correct |
37 |
Correct |
8 ms |
33116 KB |
Output is correct |
38 |
Correct |
8 ms |
33116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
32860 KB |
Output is correct |
2 |
Correct |
7 ms |
32860 KB |
Output is correct |
3 |
Correct |
7 ms |
32860 KB |
Output is correct |
4 |
Correct |
7 ms |
32860 KB |
Output is correct |
5 |
Correct |
7 ms |
32856 KB |
Output is correct |
6 |
Correct |
7 ms |
33040 KB |
Output is correct |
7 |
Correct |
7 ms |
32856 KB |
Output is correct |
8 |
Correct |
7 ms |
32860 KB |
Output is correct |
9 |
Correct |
7 ms |
33072 KB |
Output is correct |
10 |
Correct |
7 ms |
32856 KB |
Output is correct |
11 |
Correct |
7 ms |
32856 KB |
Output is correct |
12 |
Correct |
7 ms |
32856 KB |
Output is correct |
13 |
Correct |
7 ms |
32856 KB |
Output is correct |
14 |
Correct |
7 ms |
32860 KB |
Output is correct |
15 |
Correct |
7 ms |
32860 KB |
Output is correct |
16 |
Correct |
7 ms |
32856 KB |
Output is correct |
17 |
Correct |
7 ms |
32860 KB |
Output is correct |
18 |
Correct |
9 ms |
33076 KB |
Output is correct |
19 |
Correct |
82 ms |
39004 KB |
Output is correct |
20 |
Correct |
83 ms |
38796 KB |
Output is correct |
21 |
Correct |
96 ms |
39412 KB |
Output is correct |
22 |
Correct |
81 ms |
39628 KB |
Output is correct |
23 |
Correct |
59 ms |
39000 KB |
Output is correct |
24 |
Correct |
45 ms |
39004 KB |
Output is correct |
25 |
Correct |
91 ms |
45396 KB |
Output is correct |
26 |
Correct |
74 ms |
46708 KB |
Output is correct |
27 |
Correct |
113 ms |
44884 KB |
Output is correct |
28 |
Correct |
201 ms |
57936 KB |
Output is correct |
29 |
Correct |
155 ms |
52704 KB |
Output is correct |
30 |
Correct |
109 ms |
44884 KB |
Output is correct |
31 |
Correct |
107 ms |
44772 KB |
Output is correct |
32 |
Correct |
127 ms |
44936 KB |
Output is correct |
33 |
Correct |
130 ms |
43548 KB |
Output is correct |
34 |
Correct |
113 ms |
43604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
32860 KB |
Output is correct |
2 |
Correct |
7 ms |
32860 KB |
Output is correct |
3 |
Correct |
7 ms |
32860 KB |
Output is correct |
4 |
Correct |
7 ms |
32860 KB |
Output is correct |
5 |
Correct |
7 ms |
32856 KB |
Output is correct |
6 |
Correct |
7 ms |
33040 KB |
Output is correct |
7 |
Correct |
7 ms |
32856 KB |
Output is correct |
8 |
Correct |
7 ms |
32860 KB |
Output is correct |
9 |
Correct |
7 ms |
33072 KB |
Output is correct |
10 |
Correct |
7 ms |
32856 KB |
Output is correct |
11 |
Correct |
7 ms |
32856 KB |
Output is correct |
12 |
Correct |
7 ms |
32856 KB |
Output is correct |
13 |
Correct |
7 ms |
32856 KB |
Output is correct |
14 |
Correct |
7 ms |
32860 KB |
Output is correct |
15 |
Correct |
7 ms |
32860 KB |
Output is correct |
16 |
Correct |
7 ms |
32856 KB |
Output is correct |
17 |
Correct |
7 ms |
32860 KB |
Output is correct |
18 |
Correct |
9 ms |
33076 KB |
Output is correct |
19 |
Correct |
7 ms |
32860 KB |
Output is correct |
20 |
Correct |
7 ms |
32860 KB |
Output is correct |
21 |
Correct |
7 ms |
33116 KB |
Output is correct |
22 |
Correct |
8 ms |
33076 KB |
Output is correct |
23 |
Correct |
7 ms |
33112 KB |
Output is correct |
24 |
Correct |
8 ms |
33116 KB |
Output is correct |
25 |
Correct |
8 ms |
33112 KB |
Output is correct |
26 |
Correct |
7 ms |
33112 KB |
Output is correct |
27 |
Correct |
8 ms |
33116 KB |
Output is correct |
28 |
Correct |
7 ms |
33116 KB |
Output is correct |
29 |
Correct |
8 ms |
33112 KB |
Output is correct |
30 |
Correct |
9 ms |
33116 KB |
Output is correct |
31 |
Correct |
8 ms |
33112 KB |
Output is correct |
32 |
Correct |
10 ms |
33112 KB |
Output is correct |
33 |
Correct |
8 ms |
33116 KB |
Output is correct |
34 |
Correct |
8 ms |
33116 KB |
Output is correct |
35 |
Correct |
9 ms |
33036 KB |
Output is correct |
36 |
Correct |
8 ms |
33116 KB |
Output is correct |
37 |
Correct |
8 ms |
33116 KB |
Output is correct |
38 |
Correct |
8 ms |
33116 KB |
Output is correct |
39 |
Correct |
82 ms |
39004 KB |
Output is correct |
40 |
Correct |
83 ms |
38796 KB |
Output is correct |
41 |
Correct |
96 ms |
39412 KB |
Output is correct |
42 |
Correct |
81 ms |
39628 KB |
Output is correct |
43 |
Correct |
59 ms |
39000 KB |
Output is correct |
44 |
Correct |
45 ms |
39004 KB |
Output is correct |
45 |
Correct |
91 ms |
45396 KB |
Output is correct |
46 |
Correct |
74 ms |
46708 KB |
Output is correct |
47 |
Correct |
113 ms |
44884 KB |
Output is correct |
48 |
Correct |
201 ms |
57936 KB |
Output is correct |
49 |
Correct |
155 ms |
52704 KB |
Output is correct |
50 |
Correct |
109 ms |
44884 KB |
Output is correct |
51 |
Correct |
107 ms |
44772 KB |
Output is correct |
52 |
Correct |
127 ms |
44936 KB |
Output is correct |
53 |
Correct |
130 ms |
43548 KB |
Output is correct |
54 |
Correct |
113 ms |
43604 KB |
Output is correct |
55 |
Correct |
13 ms |
33628 KB |
Output is correct |
56 |
Correct |
14 ms |
33368 KB |
Output is correct |
57 |
Correct |
57 ms |
39892 KB |
Output is correct |
58 |
Correct |
34 ms |
41052 KB |
Output is correct |
59 |
Correct |
74 ms |
47576 KB |
Output is correct |
60 |
Correct |
286 ms |
54744 KB |
Output is correct |
61 |
Correct |
123 ms |
46676 KB |
Output is correct |
62 |
Correct |
122 ms |
48060 KB |
Output is correct |
63 |
Correct |
148 ms |
47996 KB |
Output is correct |
64 |
Correct |
264 ms |
46652 KB |
Output is correct |
65 |
Correct |
144 ms |
47956 KB |
Output is correct |
66 |
Correct |
238 ms |
56956 KB |
Output is correct |
67 |
Correct |
80 ms |
50320 KB |
Output is correct |
68 |
Correct |
155 ms |
48080 KB |
Output is correct |
69 |
Correct |
150 ms |
45832 KB |
Output is correct |
70 |
Correct |
141 ms |
47916 KB |
Output is correct |