#include <bits/stdc++.h>
using namespace std;
const int N = 200001;
vector<pair<int, int> > adj[N];
map<long long, long long> mp[N];
long long distToRoot[N];
long long weightPathToRoot[N];
int n, k;
long long ans;
void dfs(int u, int p, long long currentsum, int height) {
mp[u][currentsum] = height;
weightPathToRoot[u] = currentsum;
distToRoot[u] = height;
for (pair<int, int> x : adj[u]) {
int v = x.first;
if(v != p){
dfs(v, u, currentsum + x.second, height + 1);
}
}
}
void dfs2(int u, int p) {
long long target = k + 2 * weightPathToRoot[u];
for (pair<int, int> x : adj[u]) {
int v = x.first;
if(v != p){
dfs2(v, u);
if (mp[v].size() > mp[u].size()){
swap(mp[v], mp[u]);
}
for (auto i : mp[v]) {
if (mp[u].find(target - i.first) != mp[u].end()) {
ans = min(ans, mp[u][target - i.first] + i.second - 2 * distToRoot[u]);
}
}
for (auto i : mp[v]) {
if (mp[u].find(i.first) == mp[u].end()) {
mp[u].insert(i);
} else {
mp[u][i.first] = min(mp[u][i.first], i.second);
}
}
}
}
}
int best_path(int n_, int k_, int edges[][2], int weights[]) {
if (k_ == 1){
return 0;
}
n = n_;
k = k_;
ans = 1e18;
for (int i = 0; i < n - 1; i++) {
int u = edges[i][0];
int v = edges[i][1];
adj[u].push_back(make_pair(v, weights[i]));
adj[v].push_back(make_pair(u, weights[i]));
}
dfs(0, -1, 0, 0);
dfs2(0, -1);
return ans == 1e18 ? -1 : ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
9 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14396 KB |
Output is correct |
5 |
Correct |
10 ms |
14400 KB |
Output is correct |
6 |
Correct |
8 ms |
14448 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14404 KB |
Output is correct |
9 |
Correct |
8 ms |
14448 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14400 KB |
Output is correct |
12 |
Correct |
9 ms |
14420 KB |
Output is correct |
13 |
Correct |
8 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14396 KB |
Output is correct |
15 |
Correct |
8 ms |
14396 KB |
Output is correct |
16 |
Correct |
8 ms |
14396 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
9 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14396 KB |
Output is correct |
5 |
Correct |
10 ms |
14400 KB |
Output is correct |
6 |
Correct |
8 ms |
14448 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14404 KB |
Output is correct |
9 |
Correct |
8 ms |
14448 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14400 KB |
Output is correct |
12 |
Correct |
9 ms |
14420 KB |
Output is correct |
13 |
Correct |
8 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14396 KB |
Output is correct |
15 |
Correct |
8 ms |
14396 KB |
Output is correct |
16 |
Correct |
8 ms |
14396 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
7 ms |
14420 KB |
Output is correct |
20 |
Correct |
7 ms |
14396 KB |
Output is correct |
21 |
Correct |
8 ms |
14660 KB |
Output is correct |
22 |
Correct |
9 ms |
14676 KB |
Output is correct |
23 |
Correct |
9 ms |
14676 KB |
Output is correct |
24 |
Correct |
9 ms |
14676 KB |
Output is correct |
25 |
Correct |
9 ms |
14664 KB |
Output is correct |
26 |
Correct |
9 ms |
14668 KB |
Output is correct |
27 |
Correct |
9 ms |
14540 KB |
Output is correct |
28 |
Correct |
9 ms |
14676 KB |
Output is correct |
29 |
Correct |
9 ms |
14676 KB |
Output is correct |
30 |
Correct |
9 ms |
14676 KB |
Output is correct |
31 |
Correct |
11 ms |
14676 KB |
Output is correct |
32 |
Correct |
9 ms |
14664 KB |
Output is correct |
33 |
Correct |
10 ms |
14668 KB |
Output is correct |
34 |
Correct |
8 ms |
14668 KB |
Output is correct |
35 |
Correct |
8 ms |
14664 KB |
Output is correct |
36 |
Correct |
9 ms |
14676 KB |
Output is correct |
37 |
Correct |
8 ms |
14548 KB |
Output is correct |
38 |
Correct |
9 ms |
14676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
9 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14396 KB |
Output is correct |
5 |
Correct |
10 ms |
14400 KB |
Output is correct |
6 |
Correct |
8 ms |
14448 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14404 KB |
Output is correct |
9 |
Correct |
8 ms |
14448 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14400 KB |
Output is correct |
12 |
Correct |
9 ms |
14420 KB |
Output is correct |
13 |
Correct |
8 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14396 KB |
Output is correct |
15 |
Correct |
8 ms |
14396 KB |
Output is correct |
16 |
Correct |
8 ms |
14396 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
146 ms |
37752 KB |
Output is correct |
20 |
Correct |
146 ms |
37732 KB |
Output is correct |
21 |
Correct |
157 ms |
37500 KB |
Output is correct |
22 |
Correct |
164 ms |
37104 KB |
Output is correct |
23 |
Correct |
176 ms |
49868 KB |
Output is correct |
24 |
Correct |
137 ms |
40564 KB |
Output is correct |
25 |
Correct |
111 ms |
38384 KB |
Output is correct |
26 |
Correct |
61 ms |
46588 KB |
Output is correct |
27 |
Correct |
218 ms |
49408 KB |
Output is correct |
28 |
Correct |
357 ms |
91752 KB |
Output is correct |
29 |
Correct |
308 ms |
89868 KB |
Output is correct |
30 |
Correct |
215 ms |
49424 KB |
Output is correct |
31 |
Correct |
213 ms |
49344 KB |
Output is correct |
32 |
Correct |
292 ms |
49536 KB |
Output is correct |
33 |
Correct |
293 ms |
54420 KB |
Output is correct |
34 |
Correct |
376 ms |
87300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
9 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14396 KB |
Output is correct |
5 |
Correct |
10 ms |
14400 KB |
Output is correct |
6 |
Correct |
8 ms |
14448 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14404 KB |
Output is correct |
9 |
Correct |
8 ms |
14448 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14400 KB |
Output is correct |
12 |
Correct |
9 ms |
14420 KB |
Output is correct |
13 |
Correct |
8 ms |
14420 KB |
Output is correct |
14 |
Correct |
8 ms |
14396 KB |
Output is correct |
15 |
Correct |
8 ms |
14396 KB |
Output is correct |
16 |
Correct |
8 ms |
14396 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
7 ms |
14420 KB |
Output is correct |
19 |
Correct |
7 ms |
14420 KB |
Output is correct |
20 |
Correct |
7 ms |
14396 KB |
Output is correct |
21 |
Correct |
8 ms |
14660 KB |
Output is correct |
22 |
Correct |
9 ms |
14676 KB |
Output is correct |
23 |
Correct |
9 ms |
14676 KB |
Output is correct |
24 |
Correct |
9 ms |
14676 KB |
Output is correct |
25 |
Correct |
9 ms |
14664 KB |
Output is correct |
26 |
Correct |
9 ms |
14668 KB |
Output is correct |
27 |
Correct |
9 ms |
14540 KB |
Output is correct |
28 |
Correct |
9 ms |
14676 KB |
Output is correct |
29 |
Correct |
9 ms |
14676 KB |
Output is correct |
30 |
Correct |
9 ms |
14676 KB |
Output is correct |
31 |
Correct |
11 ms |
14676 KB |
Output is correct |
32 |
Correct |
9 ms |
14664 KB |
Output is correct |
33 |
Correct |
10 ms |
14668 KB |
Output is correct |
34 |
Correct |
8 ms |
14668 KB |
Output is correct |
35 |
Correct |
8 ms |
14664 KB |
Output is correct |
36 |
Correct |
9 ms |
14676 KB |
Output is correct |
37 |
Correct |
8 ms |
14548 KB |
Output is correct |
38 |
Correct |
9 ms |
14676 KB |
Output is correct |
39 |
Correct |
146 ms |
37752 KB |
Output is correct |
40 |
Correct |
146 ms |
37732 KB |
Output is correct |
41 |
Correct |
157 ms |
37500 KB |
Output is correct |
42 |
Correct |
164 ms |
37104 KB |
Output is correct |
43 |
Correct |
176 ms |
49868 KB |
Output is correct |
44 |
Correct |
137 ms |
40564 KB |
Output is correct |
45 |
Correct |
111 ms |
38384 KB |
Output is correct |
46 |
Correct |
61 ms |
46588 KB |
Output is correct |
47 |
Correct |
218 ms |
49408 KB |
Output is correct |
48 |
Correct |
357 ms |
91752 KB |
Output is correct |
49 |
Correct |
308 ms |
89868 KB |
Output is correct |
50 |
Correct |
215 ms |
49424 KB |
Output is correct |
51 |
Correct |
213 ms |
49344 KB |
Output is correct |
52 |
Correct |
292 ms |
49536 KB |
Output is correct |
53 |
Correct |
293 ms |
54420 KB |
Output is correct |
54 |
Correct |
376 ms |
87300 KB |
Output is correct |
55 |
Correct |
24 ms |
17612 KB |
Output is correct |
56 |
Correct |
15 ms |
16364 KB |
Output is correct |
57 |
Correct |
77 ms |
35096 KB |
Output is correct |
58 |
Correct |
54 ms |
28396 KB |
Output is correct |
59 |
Correct |
99 ms |
52812 KB |
Output is correct |
60 |
Correct |
301 ms |
90364 KB |
Output is correct |
61 |
Correct |
238 ms |
52964 KB |
Output is correct |
62 |
Correct |
201 ms |
49468 KB |
Output is correct |
63 |
Correct |
302 ms |
49628 KB |
Output is correct |
64 |
Correct |
465 ms |
104812 KB |
Output is correct |
65 |
Correct |
459 ms |
102888 KB |
Output is correct |
66 |
Correct |
344 ms |
85748 KB |
Output is correct |
67 |
Correct |
195 ms |
44008 KB |
Output is correct |
68 |
Correct |
399 ms |
66488 KB |
Output is correct |
69 |
Correct |
412 ms |
71652 KB |
Output is correct |
70 |
Correct |
375 ms |
64288 KB |
Output is correct |