#include <bits/stdc++.h>
using namespace std;
#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define LSB(i) ((i) & (-i))
#define ll long long
const int dx[]{-1,1,0,0,-1,-1,1,1};
const int dy[]{0,0,1,-1,-1,1,-1,1};
const int MOD = 1e9+7;
const int N = 2e5+5;
std::vector<array<int,2>> G[N];
map<ll, ll> vec[N];
ll sum[N], dist[N], n, k, ans;
void dfs0(int u, int p, int s = 0, int d = 0){
sum[u] = s;
dist[u] = d;
vec[u][s] = d;
for(auto& nei : G[u]){
int v = nei[0], w = nei[1];
if(v==p) continue;
dfs0(v, u, s+w, d+1);
}
}
void dfs(int u, int p){
ll target = k + 2*sum[u];
for(auto& nei : G[u]){
int v = nei[0];
if(v==p) continue;
dfs(v, u);
if(vec[v].size() > vec[u].size()) swap(vec[u], vec[v]);
for(auto& p : vec[v]){
if(vec[u].count(target-p.first)){
ans = min(ans, vec[u][target-p.first]+p.second-2*dist[u]);
}
}
for(auto& p : vec[v]){
if(!vec[u].count(p.first)) vec[u].insert(p);
else vec[u][p.first] = min(vec[u][p.first], p.second);
}
}
}
int best_path(int _n, int _k, int edges[][2], int weights[]) {
if (k == 1) { return 0; }
n = _n;
k = _k;
ans = INT_MAX;
for (int i = 0; i < n - 1; i++) {
int u = edges[i][0];
int v = edges[i][1];
G[u].push_back({v, weights[i]});
G[v].push_back({u, weights[i]});
}
dfs0(0, -1);
dfs(0, -1);
return ans == INT_MAX ? -1 : ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14680 KB |
Output is correct |
2 |
Correct |
6 ms |
14428 KB |
Output is correct |
3 |
Correct |
5 ms |
14404 KB |
Output is correct |
4 |
Correct |
6 ms |
14428 KB |
Output is correct |
5 |
Correct |
6 ms |
14428 KB |
Output is correct |
6 |
Correct |
5 ms |
14428 KB |
Output is correct |
7 |
Correct |
5 ms |
14592 KB |
Output is correct |
8 |
Correct |
5 ms |
14428 KB |
Output is correct |
9 |
Correct |
6 ms |
14428 KB |
Output is correct |
10 |
Correct |
7 ms |
14428 KB |
Output is correct |
11 |
Correct |
8 ms |
14428 KB |
Output is correct |
12 |
Correct |
6 ms |
14428 KB |
Output is correct |
13 |
Correct |
6 ms |
14428 KB |
Output is correct |
14 |
Correct |
6 ms |
14428 KB |
Output is correct |
15 |
Correct |
5 ms |
14532 KB |
Output is correct |
16 |
Correct |
6 ms |
14428 KB |
Output is correct |
17 |
Correct |
6 ms |
14428 KB |
Output is correct |
18 |
Correct |
6 ms |
14424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14680 KB |
Output is correct |
2 |
Correct |
6 ms |
14428 KB |
Output is correct |
3 |
Correct |
5 ms |
14404 KB |
Output is correct |
4 |
Correct |
6 ms |
14428 KB |
Output is correct |
5 |
Correct |
6 ms |
14428 KB |
Output is correct |
6 |
Correct |
5 ms |
14428 KB |
Output is correct |
7 |
Correct |
5 ms |
14592 KB |
Output is correct |
8 |
Correct |
5 ms |
14428 KB |
Output is correct |
9 |
Correct |
6 ms |
14428 KB |
Output is correct |
10 |
Correct |
7 ms |
14428 KB |
Output is correct |
11 |
Correct |
8 ms |
14428 KB |
Output is correct |
12 |
Correct |
6 ms |
14428 KB |
Output is correct |
13 |
Correct |
6 ms |
14428 KB |
Output is correct |
14 |
Correct |
6 ms |
14428 KB |
Output is correct |
15 |
Correct |
5 ms |
14532 KB |
Output is correct |
16 |
Correct |
6 ms |
14428 KB |
Output is correct |
17 |
Correct |
6 ms |
14428 KB |
Output is correct |
18 |
Correct |
6 ms |
14424 KB |
Output is correct |
19 |
Correct |
7 ms |
14424 KB |
Output is correct |
20 |
Correct |
6 ms |
14428 KB |
Output is correct |
21 |
Correct |
6 ms |
14684 KB |
Output is correct |
22 |
Correct |
7 ms |
14684 KB |
Output is correct |
23 |
Correct |
7 ms |
14684 KB |
Output is correct |
24 |
Correct |
7 ms |
14780 KB |
Output is correct |
25 |
Correct |
7 ms |
14680 KB |
Output is correct |
26 |
Correct |
7 ms |
14684 KB |
Output is correct |
27 |
Correct |
6 ms |
14684 KB |
Output is correct |
28 |
Correct |
7 ms |
14684 KB |
Output is correct |
29 |
Correct |
7 ms |
14684 KB |
Output is correct |
30 |
Correct |
6 ms |
14684 KB |
Output is correct |
31 |
Correct |
6 ms |
14684 KB |
Output is correct |
32 |
Correct |
6 ms |
14696 KB |
Output is correct |
33 |
Correct |
7 ms |
14684 KB |
Output is correct |
34 |
Correct |
6 ms |
14684 KB |
Output is correct |
35 |
Correct |
6 ms |
14684 KB |
Output is correct |
36 |
Correct |
6 ms |
14684 KB |
Output is correct |
37 |
Correct |
7 ms |
14684 KB |
Output is correct |
38 |
Correct |
6 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14680 KB |
Output is correct |
2 |
Correct |
6 ms |
14428 KB |
Output is correct |
3 |
Correct |
5 ms |
14404 KB |
Output is correct |
4 |
Correct |
6 ms |
14428 KB |
Output is correct |
5 |
Correct |
6 ms |
14428 KB |
Output is correct |
6 |
Correct |
5 ms |
14428 KB |
Output is correct |
7 |
Correct |
5 ms |
14592 KB |
Output is correct |
8 |
Correct |
5 ms |
14428 KB |
Output is correct |
9 |
Correct |
6 ms |
14428 KB |
Output is correct |
10 |
Correct |
7 ms |
14428 KB |
Output is correct |
11 |
Correct |
8 ms |
14428 KB |
Output is correct |
12 |
Correct |
6 ms |
14428 KB |
Output is correct |
13 |
Correct |
6 ms |
14428 KB |
Output is correct |
14 |
Correct |
6 ms |
14428 KB |
Output is correct |
15 |
Correct |
5 ms |
14532 KB |
Output is correct |
16 |
Correct |
6 ms |
14428 KB |
Output is correct |
17 |
Correct |
6 ms |
14428 KB |
Output is correct |
18 |
Correct |
6 ms |
14424 KB |
Output is correct |
19 |
Correct |
89 ms |
37716 KB |
Output is correct |
20 |
Correct |
87 ms |
37752 KB |
Output is correct |
21 |
Correct |
84 ms |
37460 KB |
Output is correct |
22 |
Correct |
82 ms |
37260 KB |
Output is correct |
23 |
Correct |
115 ms |
49788 KB |
Output is correct |
24 |
Correct |
93 ms |
40528 KB |
Output is correct |
25 |
Correct |
75 ms |
35924 KB |
Output is correct |
26 |
Correct |
43 ms |
41956 KB |
Output is correct |
27 |
Correct |
136 ms |
49492 KB |
Output is correct |
28 |
Correct |
209 ms |
82260 KB |
Output is correct |
29 |
Correct |
217 ms |
81232 KB |
Output is correct |
30 |
Correct |
133 ms |
49436 KB |
Output is correct |
31 |
Correct |
136 ms |
49472 KB |
Output is correct |
32 |
Correct |
191 ms |
49544 KB |
Output is correct |
33 |
Correct |
188 ms |
54328 KB |
Output is correct |
34 |
Correct |
257 ms |
87244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14680 KB |
Output is correct |
2 |
Correct |
6 ms |
14428 KB |
Output is correct |
3 |
Correct |
5 ms |
14404 KB |
Output is correct |
4 |
Correct |
6 ms |
14428 KB |
Output is correct |
5 |
Correct |
6 ms |
14428 KB |
Output is correct |
6 |
Correct |
5 ms |
14428 KB |
Output is correct |
7 |
Correct |
5 ms |
14592 KB |
Output is correct |
8 |
Correct |
5 ms |
14428 KB |
Output is correct |
9 |
Correct |
6 ms |
14428 KB |
Output is correct |
10 |
Correct |
7 ms |
14428 KB |
Output is correct |
11 |
Correct |
8 ms |
14428 KB |
Output is correct |
12 |
Correct |
6 ms |
14428 KB |
Output is correct |
13 |
Correct |
6 ms |
14428 KB |
Output is correct |
14 |
Correct |
6 ms |
14428 KB |
Output is correct |
15 |
Correct |
5 ms |
14532 KB |
Output is correct |
16 |
Correct |
6 ms |
14428 KB |
Output is correct |
17 |
Correct |
6 ms |
14428 KB |
Output is correct |
18 |
Correct |
6 ms |
14424 KB |
Output is correct |
19 |
Correct |
7 ms |
14424 KB |
Output is correct |
20 |
Correct |
6 ms |
14428 KB |
Output is correct |
21 |
Correct |
6 ms |
14684 KB |
Output is correct |
22 |
Correct |
7 ms |
14684 KB |
Output is correct |
23 |
Correct |
7 ms |
14684 KB |
Output is correct |
24 |
Correct |
7 ms |
14780 KB |
Output is correct |
25 |
Correct |
7 ms |
14680 KB |
Output is correct |
26 |
Correct |
7 ms |
14684 KB |
Output is correct |
27 |
Correct |
6 ms |
14684 KB |
Output is correct |
28 |
Correct |
7 ms |
14684 KB |
Output is correct |
29 |
Correct |
7 ms |
14684 KB |
Output is correct |
30 |
Correct |
6 ms |
14684 KB |
Output is correct |
31 |
Correct |
6 ms |
14684 KB |
Output is correct |
32 |
Correct |
6 ms |
14696 KB |
Output is correct |
33 |
Correct |
7 ms |
14684 KB |
Output is correct |
34 |
Correct |
6 ms |
14684 KB |
Output is correct |
35 |
Correct |
6 ms |
14684 KB |
Output is correct |
36 |
Correct |
6 ms |
14684 KB |
Output is correct |
37 |
Correct |
7 ms |
14684 KB |
Output is correct |
38 |
Correct |
6 ms |
14684 KB |
Output is correct |
39 |
Correct |
89 ms |
37716 KB |
Output is correct |
40 |
Correct |
87 ms |
37752 KB |
Output is correct |
41 |
Correct |
84 ms |
37460 KB |
Output is correct |
42 |
Correct |
82 ms |
37260 KB |
Output is correct |
43 |
Correct |
115 ms |
49788 KB |
Output is correct |
44 |
Correct |
93 ms |
40528 KB |
Output is correct |
45 |
Correct |
75 ms |
35924 KB |
Output is correct |
46 |
Correct |
43 ms |
41956 KB |
Output is correct |
47 |
Correct |
136 ms |
49492 KB |
Output is correct |
48 |
Correct |
209 ms |
82260 KB |
Output is correct |
49 |
Correct |
217 ms |
81232 KB |
Output is correct |
50 |
Correct |
133 ms |
49436 KB |
Output is correct |
51 |
Correct |
136 ms |
49472 KB |
Output is correct |
52 |
Correct |
191 ms |
49544 KB |
Output is correct |
53 |
Correct |
188 ms |
54328 KB |
Output is correct |
54 |
Correct |
257 ms |
87244 KB |
Output is correct |
55 |
Correct |
16 ms |
17756 KB |
Output is correct |
56 |
Correct |
12 ms |
16476 KB |
Output is correct |
57 |
Correct |
49 ms |
35056 KB |
Output is correct |
58 |
Correct |
38 ms |
28496 KB |
Output is correct |
59 |
Correct |
72 ms |
48212 KB |
Output is correct |
60 |
Correct |
217 ms |
81488 KB |
Output is correct |
61 |
Correct |
177 ms |
52892 KB |
Output is correct |
62 |
Correct |
163 ms |
49488 KB |
Output is correct |
63 |
Correct |
190 ms |
49544 KB |
Output is correct |
64 |
Correct |
377 ms |
104568 KB |
Output is correct |
65 |
Correct |
359 ms |
102892 KB |
Output is correct |
66 |
Correct |
252 ms |
78676 KB |
Output is correct |
67 |
Correct |
138 ms |
43968 KB |
Output is correct |
68 |
Correct |
277 ms |
66572 KB |
Output is correct |
69 |
Correct |
311 ms |
71504 KB |
Output is correct |
70 |
Correct |
300 ms |
64340 KB |
Output is correct |