#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int mxN = 2e5+10;
const int mxK = 1e6+10;
const int INF = 1e9+10;
int ans = INF, k, bk = 0;
int d[mxK], last[mxN];
vector<array<int, 2>> adj[mxN];
bool vis[mxN];
int sz[mxN];
void get_sz(int node, int p) {
sz[node] = 1;
for(auto [it, w] : adj[node]) {
if(it == p || vis[it]) continue;
get_sz(it, node);
sz[node] += sz[it];
}
}
int get_c(int node, int p, int n) {
for(auto [it, w] : adj[node]) {
if(vis[it] || it == p) continue;
if(sz[it] * 2 > n) return get_c(it, node, p);
}
return node;
}
void get_min(int node, int p, bool filling, int dist, int s) {
if(s > k) return ;
if(filling) {
if(last[s] != bk) {
last[s] = bk;
d[s] = dist;
}
else {
d[s] = min(d[s], dist);
}
}
else {
if(last[k-s] == bk) {
ans = min(ans, dist+d[k-s]);
}
}
for(auto [it, w] : adj[node]) {
if(it == p || vis[it]) continue;
get_min(it, node, filling, dist+1, s+w);
}
}
void build(int node) {
get_sz(node, -1);
int c = get_c(node, -1, sz[node]);
vis[c] = true;
bk++;
last[0] = bk;
for(auto [it, w] : adj[c]) {
if(vis[it]) continue;
get_min(it, c, 0, 1, w);
get_min(it, c, 1, 1, w);
}
for(auto [it, w] : adj[c]) {
if(vis[it]) continue;
build(it);
}
}
int best_path(int n, int K, int h[][2], int l[]) {
for(int i = 0; i < n-1; i++) {
adj[h[i][0]].push_back({h[i][1], l[i]});
adj[h[i][1]].push_back({h[i][0], l[i]});
}
k = K;
build(0);
return (ans == INF ? -1 : ans);
}
/*
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout << best_path(11, 12, {{0, 1}, {0, 2}, {2, 3}, {3, 4}, {4, 5}, {0, 6},
{6, 7}, {6, 8}, {8, 9}, {8, 10}},
{3, 4, 5, 4, 6, 3, 2, 5, 6, 7});
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10684 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10840 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10684 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10840 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
1 ms |
10588 KB |
Output is correct |
20 |
Correct |
1 ms |
10840 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
13916 KB |
Output is correct |
23 |
Correct |
3 ms |
13148 KB |
Output is correct |
24 |
Correct |
3 ms |
13660 KB |
Output is correct |
25 |
Correct |
2 ms |
12380 KB |
Output is correct |
26 |
Correct |
2 ms |
12636 KB |
Output is correct |
27 |
Correct |
2 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
12732 KB |
Output is correct |
29 |
Correct |
2 ms |
11612 KB |
Output is correct |
30 |
Incorrect |
2 ms |
11868 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10684 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10840 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
472 ms |
16576 KB |
Output is correct |
20 |
Correct |
475 ms |
16720 KB |
Output is correct |
21 |
Correct |
804 ms |
16580 KB |
Output is correct |
22 |
Correct |
74 ms |
16452 KB |
Output is correct |
23 |
Correct |
40 ms |
16728 KB |
Output is correct |
24 |
Correct |
31 ms |
16732 KB |
Output is correct |
25 |
Execution timed out |
3070 ms |
20060 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10584 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10584 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10588 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10684 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
2 ms |
10840 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
1 ms |
10588 KB |
Output is correct |
20 |
Correct |
1 ms |
10840 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
13916 KB |
Output is correct |
23 |
Correct |
3 ms |
13148 KB |
Output is correct |
24 |
Correct |
3 ms |
13660 KB |
Output is correct |
25 |
Correct |
2 ms |
12380 KB |
Output is correct |
26 |
Correct |
2 ms |
12636 KB |
Output is correct |
27 |
Correct |
2 ms |
10588 KB |
Output is correct |
28 |
Correct |
2 ms |
12732 KB |
Output is correct |
29 |
Correct |
2 ms |
11612 KB |
Output is correct |
30 |
Incorrect |
2 ms |
11868 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |