#include <bits/stdc++.h>
using namespace std;
#define sp " "
#define endl "\n"
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define M 200005
vector<pii> adj[M];
int sz[M], dep[M], dist[M], vis[M], mini[1000006];
int k, n, ans = M;
void dfs(int node, int root){
sz[node] = 1;
//cout<<node<<endl;
for (auto i : adj[node]){
if (i.st == root || vis[i.st]) continue;
dfs(i.st, node);
sz[node] += sz[i.st];
}
}
void dfs2(int node, int root, int d, int dd, vector<pii> &all){
all.pb({dd, d});
for (auto i : adj[node]){
if (i.st == root || vis[i.st]) continue;
dfs2(i.st, node, d + 1, dd + i.nd, all);
}
}
int find_centroid(int node, int root, int S){
for (auto i : adj[node]){
if (i.st == root || vis[i.st]) continue;
if (sz[i.st] >= S / 2) return find_centroid(i.st, node, S);
}
return node;
}
void f(int node){
dfs(node, 0);
node = find_centroid(node, 0, sz[node]);
vector<pii> v;
for (auto i : adj[node]){
if (vis[i.st]) continue;
vector<pii> tmp;
dfs2(i.st, node, 1, i.nd, tmp);
for (auto j : tmp){
if (j.st <= k) ans = min(ans, mini[k - j.st] + j.nd);
}
for (auto j : tmp){
if (j.st <= k) mini[j.st] = min(mini[j.st], j.nd);
}
for (auto j : tmp)
{
v.pb(j);
}
}
for (auto i : v){
if (i.st <= k) mini[i.st] = M;
}
vis[node] = 1;
for (auto i : adj[node]){
if (vis[i.st] == 0) f(i.st);
}
}
int best_path(int N, int K, int H[][2], int L[]){
k = K, n = N;
for (int i = 0; i < N - 1; i++){
int u = H[i][0] + 1, v = H[i][1] + 1;
//cout<<u<<sp<<v<<endl;
adj[u].pb({v, L[i]});
adj[v].pb({u, L[i]});
}
for (int i = 1; i <= K; i++) mini[i] = M;
f(1);
if (ans < M)
return ans;
return -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5000 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5004 KB |
Output is correct |
6 |
Correct |
3 ms |
4924 KB |
Output is correct |
7 |
Correct |
3 ms |
5008 KB |
Output is correct |
8 |
Correct |
3 ms |
5004 KB |
Output is correct |
9 |
Correct |
2 ms |
5000 KB |
Output is correct |
10 |
Correct |
3 ms |
5008 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5004 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5004 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
5008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5000 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5004 KB |
Output is correct |
6 |
Correct |
3 ms |
4924 KB |
Output is correct |
7 |
Correct |
3 ms |
5008 KB |
Output is correct |
8 |
Correct |
3 ms |
5004 KB |
Output is correct |
9 |
Correct |
2 ms |
5000 KB |
Output is correct |
10 |
Correct |
3 ms |
5008 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5004 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5004 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
5008 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
5 ms |
8660 KB |
Output is correct |
23 |
Correct |
4 ms |
7960 KB |
Output is correct |
24 |
Correct |
5 ms |
8404 KB |
Output is correct |
25 |
Correct |
4 ms |
8404 KB |
Output is correct |
26 |
Correct |
3 ms |
6420 KB |
Output is correct |
27 |
Correct |
4 ms |
8148 KB |
Output is correct |
28 |
Correct |
4 ms |
5844 KB |
Output is correct |
29 |
Correct |
4 ms |
6356 KB |
Output is correct |
30 |
Correct |
4 ms |
6484 KB |
Output is correct |
31 |
Correct |
4 ms |
7636 KB |
Output is correct |
32 |
Correct |
5 ms |
7892 KB |
Output is correct |
33 |
Correct |
5 ms |
8148 KB |
Output is correct |
34 |
Correct |
5 ms |
7380 KB |
Output is correct |
35 |
Correct |
6 ms |
8276 KB |
Output is correct |
36 |
Correct |
5 ms |
8720 KB |
Output is correct |
37 |
Correct |
5 ms |
8148 KB |
Output is correct |
38 |
Correct |
4 ms |
6996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5000 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5004 KB |
Output is correct |
6 |
Correct |
3 ms |
4924 KB |
Output is correct |
7 |
Correct |
3 ms |
5008 KB |
Output is correct |
8 |
Correct |
3 ms |
5004 KB |
Output is correct |
9 |
Correct |
2 ms |
5000 KB |
Output is correct |
10 |
Correct |
3 ms |
5008 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5004 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5004 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
5008 KB |
Output is correct |
19 |
Correct |
95 ms |
12840 KB |
Output is correct |
20 |
Correct |
96 ms |
12956 KB |
Output is correct |
21 |
Correct |
106 ms |
12904 KB |
Output is correct |
22 |
Correct |
86 ms |
12864 KB |
Output is correct |
23 |
Correct |
70 ms |
13492 KB |
Output is correct |
24 |
Correct |
55 ms |
12776 KB |
Output is correct |
25 |
Correct |
132 ms |
17484 KB |
Output is correct |
26 |
Correct |
86 ms |
20652 KB |
Output is correct |
27 |
Correct |
149 ms |
18844 KB |
Output is correct |
28 |
Correct |
312 ms |
35484 KB |
Output is correct |
29 |
Correct |
318 ms |
34280 KB |
Output is correct |
30 |
Correct |
150 ms |
18880 KB |
Output is correct |
31 |
Correct |
152 ms |
18912 KB |
Output is correct |
32 |
Correct |
186 ms |
18888 KB |
Output is correct |
33 |
Correct |
204 ms |
20084 KB |
Output is correct |
34 |
Correct |
207 ms |
20020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5000 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
4 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
5004 KB |
Output is correct |
6 |
Correct |
3 ms |
4924 KB |
Output is correct |
7 |
Correct |
3 ms |
5008 KB |
Output is correct |
8 |
Correct |
3 ms |
5004 KB |
Output is correct |
9 |
Correct |
2 ms |
5000 KB |
Output is correct |
10 |
Correct |
3 ms |
5008 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5004 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5004 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
5008 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
5 ms |
8660 KB |
Output is correct |
23 |
Correct |
4 ms |
7960 KB |
Output is correct |
24 |
Correct |
5 ms |
8404 KB |
Output is correct |
25 |
Correct |
4 ms |
8404 KB |
Output is correct |
26 |
Correct |
3 ms |
6420 KB |
Output is correct |
27 |
Correct |
4 ms |
8148 KB |
Output is correct |
28 |
Correct |
4 ms |
5844 KB |
Output is correct |
29 |
Correct |
4 ms |
6356 KB |
Output is correct |
30 |
Correct |
4 ms |
6484 KB |
Output is correct |
31 |
Correct |
4 ms |
7636 KB |
Output is correct |
32 |
Correct |
5 ms |
7892 KB |
Output is correct |
33 |
Correct |
5 ms |
8148 KB |
Output is correct |
34 |
Correct |
5 ms |
7380 KB |
Output is correct |
35 |
Correct |
6 ms |
8276 KB |
Output is correct |
36 |
Correct |
5 ms |
8720 KB |
Output is correct |
37 |
Correct |
5 ms |
8148 KB |
Output is correct |
38 |
Correct |
4 ms |
6996 KB |
Output is correct |
39 |
Correct |
95 ms |
12840 KB |
Output is correct |
40 |
Correct |
96 ms |
12956 KB |
Output is correct |
41 |
Correct |
106 ms |
12904 KB |
Output is correct |
42 |
Correct |
86 ms |
12864 KB |
Output is correct |
43 |
Correct |
70 ms |
13492 KB |
Output is correct |
44 |
Correct |
55 ms |
12776 KB |
Output is correct |
45 |
Correct |
132 ms |
17484 KB |
Output is correct |
46 |
Correct |
86 ms |
20652 KB |
Output is correct |
47 |
Correct |
149 ms |
18844 KB |
Output is correct |
48 |
Correct |
312 ms |
35484 KB |
Output is correct |
49 |
Correct |
318 ms |
34280 KB |
Output is correct |
50 |
Correct |
150 ms |
18880 KB |
Output is correct |
51 |
Correct |
152 ms |
18912 KB |
Output is correct |
52 |
Correct |
186 ms |
18888 KB |
Output is correct |
53 |
Correct |
204 ms |
20084 KB |
Output is correct |
54 |
Correct |
207 ms |
20020 KB |
Output is correct |
55 |
Correct |
9 ms |
5952 KB |
Output is correct |
56 |
Correct |
9 ms |
5916 KB |
Output is correct |
57 |
Correct |
74 ms |
13380 KB |
Output is correct |
58 |
Correct |
31 ms |
12760 KB |
Output is correct |
59 |
Correct |
84 ms |
21352 KB |
Output is correct |
60 |
Correct |
308 ms |
38652 KB |
Output is correct |
61 |
Correct |
140 ms |
18924 KB |
Output is correct |
62 |
Correct |
136 ms |
22708 KB |
Output is correct |
63 |
Correct |
227 ms |
22844 KB |
Output is correct |
64 |
Correct |
273 ms |
22788 KB |
Output is correct |
65 |
Correct |
217 ms |
21004 KB |
Output is correct |
66 |
Correct |
330 ms |
35548 KB |
Output is correct |
67 |
Correct |
115 ms |
23592 KB |
Output is correct |
68 |
Correct |
167 ms |
24456 KB |
Output is correct |
69 |
Correct |
179 ms |
24756 KB |
Output is correct |
70 |
Correct |
198 ms |
23968 KB |
Output is correct |