#include "race.h"
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int MOD = (int)1e9 + 7;
const int NS = (int)2e5 + 4;
int N, k;
vector<pair<int, int>> way[NS];
int chk[NS], sz[NS], rt;
int dis[(int)1e6 + 4];
int ans;
vector<int> did;
vector<pair<int, int>> put;
void getroot(int now, int pr, int alls){
sz[now] = 1;
int f = 1;
for(auto&nxt:way[now]){
if(nxt.first == pr || chk[nxt.first]){
continue;
}
getroot(nxt.first, now, alls);
sz[now] += sz[nxt.first];
if(sz[nxt.first] > alls / 2){
f = 0;
}
}
if(f){
rt = now;
}
}
void getdis(int now, int ndis, int pr, int ndep){
int need = k - ndis;
if(need >= 0){
ans = min(ans, ndep + dis[need]);
}
for(auto&nxt:way[now]){
if(nxt.first == pr || chk[nxt.first]){
continue;
}
getdis(nxt.first, ndis + nxt.second, now, ndep + 1);
}
if(ndis < (int)1e6 + 4){
if(dis[ndis] > ndep){
put.push_back({ndis, ndep});
did.push_back(ndis);
}
}
}
void sol(int now, int alls){
dis[0] = 0;
chk[now] = 1;
did.clear();
for(int i = 0; i < (int)way[now].size(); ++i){
if(chk[way[now][i].first]){
continue;
}
getdis(way[now][i].first, way[now][i].second, now, 1);
while((int)put.size()){
dis[put.back().first] = min(dis[put.back().first], put.back().second);
put.pop_back();
}
}
while((int)did.size()){
dis[did.back()] = MOD;
did.pop_back();
}
for(auto&nxt:way[now]){
if(chk[nxt.first]){
continue;
}
int nsz = (sz[nxt.first] > sz[now] ? alls - sz[now]:sz[nxt.first]);
getroot(nxt.first, now, nsz);
sol(rt, nsz);
}
}
int best_path(int n, int K, int H[][2], int L[])
{
for(int i = 0; i < (int)1e6 + 4; ++i){
dis[i] = MOD;
}
ans = MOD;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
N = n, k = K;
for(int i = 0; i < N - 1; ++i){
int a, b, c;
a = H[i][0], b = H[i][1], c = L[i];
way[a].push_back({b, c}), way[b].push_back({a, c});
}
getroot(1, -1, N);
sol(rt, N);
if(ans == MOD){
return -1;
}
else{
return ans;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8960 KB |
Output is correct |
2 |
Correct |
6 ms |
8960 KB |
Output is correct |
3 |
Correct |
6 ms |
8960 KB |
Output is correct |
4 |
Correct |
7 ms |
8960 KB |
Output is correct |
5 |
Correct |
6 ms |
8960 KB |
Output is correct |
6 |
Correct |
7 ms |
8960 KB |
Output is correct |
7 |
Correct |
8 ms |
8960 KB |
Output is correct |
8 |
Correct |
6 ms |
8960 KB |
Output is correct |
9 |
Correct |
7 ms |
8960 KB |
Output is correct |
10 |
Correct |
7 ms |
8960 KB |
Output is correct |
11 |
Correct |
7 ms |
8960 KB |
Output is correct |
12 |
Correct |
7 ms |
8960 KB |
Output is correct |
13 |
Correct |
6 ms |
8960 KB |
Output is correct |
14 |
Correct |
7 ms |
8960 KB |
Output is correct |
15 |
Correct |
6 ms |
8960 KB |
Output is correct |
16 |
Correct |
6 ms |
8960 KB |
Output is correct |
17 |
Correct |
6 ms |
8960 KB |
Output is correct |
18 |
Correct |
7 ms |
8960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8960 KB |
Output is correct |
2 |
Correct |
6 ms |
8960 KB |
Output is correct |
3 |
Correct |
6 ms |
8960 KB |
Output is correct |
4 |
Correct |
7 ms |
8960 KB |
Output is correct |
5 |
Correct |
6 ms |
8960 KB |
Output is correct |
6 |
Correct |
7 ms |
8960 KB |
Output is correct |
7 |
Correct |
8 ms |
8960 KB |
Output is correct |
8 |
Correct |
6 ms |
8960 KB |
Output is correct |
9 |
Correct |
7 ms |
8960 KB |
Output is correct |
10 |
Correct |
7 ms |
8960 KB |
Output is correct |
11 |
Correct |
7 ms |
8960 KB |
Output is correct |
12 |
Correct |
7 ms |
8960 KB |
Output is correct |
13 |
Correct |
6 ms |
8960 KB |
Output is correct |
14 |
Correct |
7 ms |
8960 KB |
Output is correct |
15 |
Correct |
6 ms |
8960 KB |
Output is correct |
16 |
Correct |
6 ms |
8960 KB |
Output is correct |
17 |
Correct |
6 ms |
8960 KB |
Output is correct |
18 |
Correct |
7 ms |
8960 KB |
Output is correct |
19 |
Correct |
6 ms |
8960 KB |
Output is correct |
20 |
Correct |
7 ms |
8960 KB |
Output is correct |
21 |
Correct |
9 ms |
9088 KB |
Output is correct |
22 |
Correct |
8 ms |
9088 KB |
Output is correct |
23 |
Correct |
8 ms |
9088 KB |
Output is correct |
24 |
Correct |
8 ms |
9088 KB |
Output is correct |
25 |
Correct |
9 ms |
9088 KB |
Output is correct |
26 |
Correct |
9 ms |
9088 KB |
Output is correct |
27 |
Correct |
8 ms |
9088 KB |
Output is correct |
28 |
Correct |
9 ms |
9088 KB |
Output is correct |
29 |
Correct |
8 ms |
9088 KB |
Output is correct |
30 |
Correct |
10 ms |
9088 KB |
Output is correct |
31 |
Correct |
9 ms |
9088 KB |
Output is correct |
32 |
Correct |
9 ms |
9088 KB |
Output is correct |
33 |
Correct |
9 ms |
9088 KB |
Output is correct |
34 |
Correct |
7 ms |
9088 KB |
Output is correct |
35 |
Correct |
7 ms |
9088 KB |
Output is correct |
36 |
Correct |
7 ms |
9088 KB |
Output is correct |
37 |
Correct |
8 ms |
9088 KB |
Output is correct |
38 |
Correct |
8 ms |
9088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8960 KB |
Output is correct |
2 |
Correct |
6 ms |
8960 KB |
Output is correct |
3 |
Correct |
6 ms |
8960 KB |
Output is correct |
4 |
Correct |
7 ms |
8960 KB |
Output is correct |
5 |
Correct |
6 ms |
8960 KB |
Output is correct |
6 |
Correct |
7 ms |
8960 KB |
Output is correct |
7 |
Correct |
8 ms |
8960 KB |
Output is correct |
8 |
Correct |
6 ms |
8960 KB |
Output is correct |
9 |
Correct |
7 ms |
8960 KB |
Output is correct |
10 |
Correct |
7 ms |
8960 KB |
Output is correct |
11 |
Correct |
7 ms |
8960 KB |
Output is correct |
12 |
Correct |
7 ms |
8960 KB |
Output is correct |
13 |
Correct |
6 ms |
8960 KB |
Output is correct |
14 |
Correct |
7 ms |
8960 KB |
Output is correct |
15 |
Correct |
6 ms |
8960 KB |
Output is correct |
16 |
Correct |
6 ms |
8960 KB |
Output is correct |
17 |
Correct |
6 ms |
8960 KB |
Output is correct |
18 |
Correct |
7 ms |
8960 KB |
Output is correct |
19 |
Correct |
408 ms |
17680 KB |
Output is correct |
20 |
Correct |
303 ms |
16504 KB |
Output is correct |
21 |
Correct |
300 ms |
17648 KB |
Output is correct |
22 |
Correct |
2354 ms |
18052 KB |
Output is correct |
23 |
Correct |
333 ms |
18032 KB |
Output is correct |
24 |
Correct |
295 ms |
17900 KB |
Output is correct |
25 |
Correct |
165 ms |
23412 KB |
Output is correct |
26 |
Correct |
101 ms |
24180 KB |
Output is correct |
27 |
Correct |
287 ms |
25948 KB |
Output is correct |
28 |
Correct |
543 ms |
40180 KB |
Output is correct |
29 |
Correct |
516 ms |
32116 KB |
Output is correct |
30 |
Correct |
291 ms |
25888 KB |
Output is correct |
31 |
Correct |
281 ms |
25948 KB |
Output is correct |
32 |
Execution timed out |
3066 ms |
26744 KB |
Time limit exceeded |
33 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
8960 KB |
Output is correct |
2 |
Correct |
6 ms |
8960 KB |
Output is correct |
3 |
Correct |
6 ms |
8960 KB |
Output is correct |
4 |
Correct |
7 ms |
8960 KB |
Output is correct |
5 |
Correct |
6 ms |
8960 KB |
Output is correct |
6 |
Correct |
7 ms |
8960 KB |
Output is correct |
7 |
Correct |
8 ms |
8960 KB |
Output is correct |
8 |
Correct |
6 ms |
8960 KB |
Output is correct |
9 |
Correct |
7 ms |
8960 KB |
Output is correct |
10 |
Correct |
7 ms |
8960 KB |
Output is correct |
11 |
Correct |
7 ms |
8960 KB |
Output is correct |
12 |
Correct |
7 ms |
8960 KB |
Output is correct |
13 |
Correct |
6 ms |
8960 KB |
Output is correct |
14 |
Correct |
7 ms |
8960 KB |
Output is correct |
15 |
Correct |
6 ms |
8960 KB |
Output is correct |
16 |
Correct |
6 ms |
8960 KB |
Output is correct |
17 |
Correct |
6 ms |
8960 KB |
Output is correct |
18 |
Correct |
7 ms |
8960 KB |
Output is correct |
19 |
Correct |
6 ms |
8960 KB |
Output is correct |
20 |
Correct |
7 ms |
8960 KB |
Output is correct |
21 |
Correct |
9 ms |
9088 KB |
Output is correct |
22 |
Correct |
8 ms |
9088 KB |
Output is correct |
23 |
Correct |
8 ms |
9088 KB |
Output is correct |
24 |
Correct |
8 ms |
9088 KB |
Output is correct |
25 |
Correct |
9 ms |
9088 KB |
Output is correct |
26 |
Correct |
9 ms |
9088 KB |
Output is correct |
27 |
Correct |
8 ms |
9088 KB |
Output is correct |
28 |
Correct |
9 ms |
9088 KB |
Output is correct |
29 |
Correct |
8 ms |
9088 KB |
Output is correct |
30 |
Correct |
10 ms |
9088 KB |
Output is correct |
31 |
Correct |
9 ms |
9088 KB |
Output is correct |
32 |
Correct |
9 ms |
9088 KB |
Output is correct |
33 |
Correct |
9 ms |
9088 KB |
Output is correct |
34 |
Correct |
7 ms |
9088 KB |
Output is correct |
35 |
Correct |
7 ms |
9088 KB |
Output is correct |
36 |
Correct |
7 ms |
9088 KB |
Output is correct |
37 |
Correct |
8 ms |
9088 KB |
Output is correct |
38 |
Correct |
8 ms |
9088 KB |
Output is correct |
39 |
Correct |
408 ms |
17680 KB |
Output is correct |
40 |
Correct |
303 ms |
16504 KB |
Output is correct |
41 |
Correct |
300 ms |
17648 KB |
Output is correct |
42 |
Correct |
2354 ms |
18052 KB |
Output is correct |
43 |
Correct |
333 ms |
18032 KB |
Output is correct |
44 |
Correct |
295 ms |
17900 KB |
Output is correct |
45 |
Correct |
165 ms |
23412 KB |
Output is correct |
46 |
Correct |
101 ms |
24180 KB |
Output is correct |
47 |
Correct |
287 ms |
25948 KB |
Output is correct |
48 |
Correct |
543 ms |
40180 KB |
Output is correct |
49 |
Correct |
516 ms |
32116 KB |
Output is correct |
50 |
Correct |
291 ms |
25888 KB |
Output is correct |
51 |
Correct |
281 ms |
25948 KB |
Output is correct |
52 |
Execution timed out |
3066 ms |
26744 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |