#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 |
14428 KB |
Output is correct |
2 |
Correct |
6 ms |
14384 KB |
Output is correct |
3 |
Correct |
6 ms |
14424 KB |
Output is correct |
4 |
Correct |
6 ms |
14372 KB |
Output is correct |
5 |
Correct |
6 ms |
14428 KB |
Output is correct |
6 |
Correct |
7 ms |
14384 KB |
Output is correct |
7 |
Correct |
8 ms |
14428 KB |
Output is correct |
8 |
Correct |
8 ms |
14428 KB |
Output is correct |
9 |
Correct |
6 ms |
14428 KB |
Output is correct |
10 |
Correct |
9 ms |
14428 KB |
Output is correct |
11 |
Correct |
6 ms |
14428 KB |
Output is correct |
12 |
Correct |
9 ms |
14424 KB |
Output is correct |
13 |
Correct |
6 ms |
14428 KB |
Output is correct |
14 |
Correct |
6 ms |
14428 KB |
Output is correct |
15 |
Correct |
6 ms |
14568 KB |
Output is correct |
16 |
Correct |
6 ms |
14428 KB |
Output is correct |
17 |
Correct |
7 ms |
14428 KB |
Output is correct |
18 |
Correct |
6 ms |
14428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14428 KB |
Output is correct |
2 |
Correct |
6 ms |
14384 KB |
Output is correct |
3 |
Correct |
6 ms |
14424 KB |
Output is correct |
4 |
Correct |
6 ms |
14372 KB |
Output is correct |
5 |
Correct |
6 ms |
14428 KB |
Output is correct |
6 |
Correct |
7 ms |
14384 KB |
Output is correct |
7 |
Correct |
8 ms |
14428 KB |
Output is correct |
8 |
Correct |
8 ms |
14428 KB |
Output is correct |
9 |
Correct |
6 ms |
14428 KB |
Output is correct |
10 |
Correct |
9 ms |
14428 KB |
Output is correct |
11 |
Correct |
6 ms |
14428 KB |
Output is correct |
12 |
Correct |
9 ms |
14424 KB |
Output is correct |
13 |
Correct |
6 ms |
14428 KB |
Output is correct |
14 |
Correct |
6 ms |
14428 KB |
Output is correct |
15 |
Correct |
6 ms |
14568 KB |
Output is correct |
16 |
Correct |
6 ms |
14428 KB |
Output is correct |
17 |
Correct |
7 ms |
14428 KB |
Output is correct |
18 |
Correct |
6 ms |
14428 KB |
Output is correct |
19 |
Correct |
6 ms |
14424 KB |
Output is correct |
20 |
Correct |
6 ms |
14424 KB |
Output is correct |
21 |
Correct |
7 ms |
14684 KB |
Output is correct |
22 |
Correct |
7 ms |
14704 KB |
Output is correct |
23 |
Correct |
7 ms |
14684 KB |
Output is correct |
24 |
Correct |
7 ms |
14684 KB |
Output is correct |
25 |
Correct |
8 ms |
14684 KB |
Output is correct |
26 |
Correct |
7 ms |
14648 KB |
Output is correct |
27 |
Correct |
6 ms |
14684 KB |
Output is correct |
28 |
Correct |
7 ms |
14648 KB |
Output is correct |
29 |
Correct |
8 ms |
14684 KB |
Output is correct |
30 |
Correct |
7 ms |
14684 KB |
Output is correct |
31 |
Correct |
7 ms |
14684 KB |
Output is correct |
32 |
Correct |
7 ms |
14684 KB |
Output is correct |
33 |
Correct |
7 ms |
14828 KB |
Output is correct |
34 |
Correct |
9 ms |
14684 KB |
Output is correct |
35 |
Correct |
9 ms |
14684 KB |
Output is correct |
36 |
Correct |
6 ms |
14696 KB |
Output is correct |
37 |
Correct |
6 ms |
14684 KB |
Output is correct |
38 |
Correct |
7 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14428 KB |
Output is correct |
2 |
Correct |
6 ms |
14384 KB |
Output is correct |
3 |
Correct |
6 ms |
14424 KB |
Output is correct |
4 |
Correct |
6 ms |
14372 KB |
Output is correct |
5 |
Correct |
6 ms |
14428 KB |
Output is correct |
6 |
Correct |
7 ms |
14384 KB |
Output is correct |
7 |
Correct |
8 ms |
14428 KB |
Output is correct |
8 |
Correct |
8 ms |
14428 KB |
Output is correct |
9 |
Correct |
6 ms |
14428 KB |
Output is correct |
10 |
Correct |
9 ms |
14428 KB |
Output is correct |
11 |
Correct |
6 ms |
14428 KB |
Output is correct |
12 |
Correct |
9 ms |
14424 KB |
Output is correct |
13 |
Correct |
6 ms |
14428 KB |
Output is correct |
14 |
Correct |
6 ms |
14428 KB |
Output is correct |
15 |
Correct |
6 ms |
14568 KB |
Output is correct |
16 |
Correct |
6 ms |
14428 KB |
Output is correct |
17 |
Correct |
7 ms |
14428 KB |
Output is correct |
18 |
Correct |
6 ms |
14428 KB |
Output is correct |
19 |
Correct |
104 ms |
36432 KB |
Output is correct |
20 |
Correct |
128 ms |
36412 KB |
Output is correct |
21 |
Correct |
92 ms |
36152 KB |
Output is correct |
22 |
Correct |
94 ms |
35664 KB |
Output is correct |
23 |
Correct |
141 ms |
48472 KB |
Output is correct |
24 |
Correct |
99 ms |
39248 KB |
Output is correct |
25 |
Correct |
82 ms |
34640 KB |
Output is correct |
26 |
Correct |
48 ms |
40532 KB |
Output is correct |
27 |
Correct |
149 ms |
46416 KB |
Output is correct |
28 |
Correct |
245 ms |
79188 KB |
Output is correct |
29 |
Correct |
230 ms |
78004 KB |
Output is correct |
30 |
Correct |
166 ms |
46416 KB |
Output is correct |
31 |
Correct |
167 ms |
46420 KB |
Output is correct |
32 |
Correct |
219 ms |
46672 KB |
Output is correct |
33 |
Correct |
220 ms |
51284 KB |
Output is correct |
34 |
Correct |
323 ms |
83284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14428 KB |
Output is correct |
2 |
Correct |
6 ms |
14384 KB |
Output is correct |
3 |
Correct |
6 ms |
14424 KB |
Output is correct |
4 |
Correct |
6 ms |
14372 KB |
Output is correct |
5 |
Correct |
6 ms |
14428 KB |
Output is correct |
6 |
Correct |
7 ms |
14384 KB |
Output is correct |
7 |
Correct |
8 ms |
14428 KB |
Output is correct |
8 |
Correct |
8 ms |
14428 KB |
Output is correct |
9 |
Correct |
6 ms |
14428 KB |
Output is correct |
10 |
Correct |
9 ms |
14428 KB |
Output is correct |
11 |
Correct |
6 ms |
14428 KB |
Output is correct |
12 |
Correct |
9 ms |
14424 KB |
Output is correct |
13 |
Correct |
6 ms |
14428 KB |
Output is correct |
14 |
Correct |
6 ms |
14428 KB |
Output is correct |
15 |
Correct |
6 ms |
14568 KB |
Output is correct |
16 |
Correct |
6 ms |
14428 KB |
Output is correct |
17 |
Correct |
7 ms |
14428 KB |
Output is correct |
18 |
Correct |
6 ms |
14428 KB |
Output is correct |
19 |
Correct |
6 ms |
14424 KB |
Output is correct |
20 |
Correct |
6 ms |
14424 KB |
Output is correct |
21 |
Correct |
7 ms |
14684 KB |
Output is correct |
22 |
Correct |
7 ms |
14704 KB |
Output is correct |
23 |
Correct |
7 ms |
14684 KB |
Output is correct |
24 |
Correct |
7 ms |
14684 KB |
Output is correct |
25 |
Correct |
8 ms |
14684 KB |
Output is correct |
26 |
Correct |
7 ms |
14648 KB |
Output is correct |
27 |
Correct |
6 ms |
14684 KB |
Output is correct |
28 |
Correct |
7 ms |
14648 KB |
Output is correct |
29 |
Correct |
8 ms |
14684 KB |
Output is correct |
30 |
Correct |
7 ms |
14684 KB |
Output is correct |
31 |
Correct |
7 ms |
14684 KB |
Output is correct |
32 |
Correct |
7 ms |
14684 KB |
Output is correct |
33 |
Correct |
7 ms |
14828 KB |
Output is correct |
34 |
Correct |
9 ms |
14684 KB |
Output is correct |
35 |
Correct |
9 ms |
14684 KB |
Output is correct |
36 |
Correct |
6 ms |
14696 KB |
Output is correct |
37 |
Correct |
6 ms |
14684 KB |
Output is correct |
38 |
Correct |
7 ms |
14684 KB |
Output is correct |
39 |
Correct |
104 ms |
36432 KB |
Output is correct |
40 |
Correct |
128 ms |
36412 KB |
Output is correct |
41 |
Correct |
92 ms |
36152 KB |
Output is correct |
42 |
Correct |
94 ms |
35664 KB |
Output is correct |
43 |
Correct |
141 ms |
48472 KB |
Output is correct |
44 |
Correct |
99 ms |
39248 KB |
Output is correct |
45 |
Correct |
82 ms |
34640 KB |
Output is correct |
46 |
Correct |
48 ms |
40532 KB |
Output is correct |
47 |
Correct |
149 ms |
46416 KB |
Output is correct |
48 |
Correct |
245 ms |
79188 KB |
Output is correct |
49 |
Correct |
230 ms |
78004 KB |
Output is correct |
50 |
Correct |
166 ms |
46416 KB |
Output is correct |
51 |
Correct |
167 ms |
46420 KB |
Output is correct |
52 |
Correct |
219 ms |
46672 KB |
Output is correct |
53 |
Correct |
220 ms |
51284 KB |
Output is correct |
54 |
Correct |
323 ms |
83284 KB |
Output is correct |
55 |
Correct |
16 ms |
17500 KB |
Output is correct |
56 |
Correct |
12 ms |
16212 KB |
Output is correct |
57 |
Correct |
49 ms |
33716 KB |
Output is correct |
58 |
Correct |
40 ms |
27332 KB |
Output is correct |
59 |
Correct |
108 ms |
46800 KB |
Output is correct |
60 |
Correct |
250 ms |
78532 KB |
Output is correct |
61 |
Correct |
181 ms |
49948 KB |
Output is correct |
62 |
Correct |
145 ms |
46420 KB |
Output is correct |
63 |
Correct |
208 ms |
46548 KB |
Output is correct |
64 |
Correct |
376 ms |
101200 KB |
Output is correct |
65 |
Correct |
380 ms |
98896 KB |
Output is correct |
66 |
Correct |
247 ms |
75600 KB |
Output is correct |
67 |
Correct |
170 ms |
40516 KB |
Output is correct |
68 |
Correct |
288 ms |
62804 KB |
Output is correct |
69 |
Correct |
372 ms |
67908 KB |
Output is correct |
70 |
Correct |
275 ms |
60964 KB |
Output is correct |