#include <bits/stdc++.h>
#include "race.h"
using namespace std;
#define ll int
#define endl '\n'
const int N = 2e5 + 5;
vector<pair<ll, ll>> node[N];
ll sz[N];
bool in[N];
ll res = 1e6;
ll K;
map<ll, ll> mem, tmp;
ll initsz(ll u, ll p){
sz[u] = 1;
for (auto j : node[u]){
if (!in[j.first] || j.first == p) continue;
initsz(j.first, u);
sz[u] += sz[j.first];
}
return sz[u];
}
ll CT(ll u, ll p, ll S){
for (auto j : node[u]){
if (!in[j.first] || j.first == p) continue;
if ((sz[j.first] * 2) > S) return CT(j.first, u, S);
}
return u;
}
void addtmp(ll d, ll e){
if (tmp[d] == 0) tmp[d] = e;
else tmp[d] = min(tmp[d], e);
}
void upd(ll d){
if (mem[K - d] != 0) res = min(res, mem[K - d] + tmp[d]);
}
void dfs(ll u, ll p, ll d, ll e){
if (d > K) return;
addtmp(d, e);
upd(d);
for (auto j : node[u]){
if (!in[j.first] || j.first == p) continue;
dfs(j.first, u, d + j.second, e + 1);
}
}
void addmem(ll d, ll e){
if (mem[d] == 0) mem[d] = e;
else mem[d] = min(mem[d], e);
}
void findCT(ll u){
ll S = initsz(u, u);
u = CT(u, u, S);
in[u] = 0;
mem.clear();
for (auto j : node[u]){
if (!in[j.first]) continue;
tmp.clear();
dfs(j.first, j.first, j.second, 1);
if (tmp[K] != 0) res = min(res, tmp[K]);
for (auto j : tmp){
addmem(j.first, j.second);
}
}
for (auto j : node[u]){
if (!in[j.first]) continue;
findCT(j.first);
}
}
ll best_path(int n, int k, int h[][2], int l[]){
for (int i = 1; i < n; i++){
node[h[i - 1][0] + 1].push_back({h[i - 1][1] + 1, l[i - 1]});
node[h[i - 1][1] + 1].push_back({h[i - 1][0] + 1, l[i - 1]});
}
K = k;
memset(in, 1, sizeof(in));
findCT(1);
if (res == 1e6) return -1;
else return res;
}
/*
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
if (fopen("VietCT.INP", "r")){
freopen("VietCT.INP", "r", stdin);
freopen("VietCT.OUT", "w", stdout);
}
}
/*
*/
Compilation message
race.cpp:90:1: warning: "/*" within comment [-Wcomment]
90 | /*
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5248 KB |
Output is correct |
2 |
Correct |
4 ms |
5248 KB |
Output is correct |
3 |
Correct |
4 ms |
5248 KB |
Output is correct |
4 |
Correct |
4 ms |
5248 KB |
Output is correct |
5 |
Correct |
4 ms |
5248 KB |
Output is correct |
6 |
Correct |
4 ms |
5248 KB |
Output is correct |
7 |
Correct |
4 ms |
5248 KB |
Output is correct |
8 |
Correct |
4 ms |
5248 KB |
Output is correct |
9 |
Correct |
4 ms |
5248 KB |
Output is correct |
10 |
Correct |
4 ms |
5248 KB |
Output is correct |
11 |
Correct |
4 ms |
5248 KB |
Output is correct |
12 |
Correct |
4 ms |
5248 KB |
Output is correct |
13 |
Correct |
4 ms |
5248 KB |
Output is correct |
14 |
Correct |
4 ms |
5248 KB |
Output is correct |
15 |
Correct |
4 ms |
5248 KB |
Output is correct |
16 |
Correct |
4 ms |
5248 KB |
Output is correct |
17 |
Correct |
4 ms |
5248 KB |
Output is correct |
18 |
Correct |
4 ms |
5248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5248 KB |
Output is correct |
2 |
Correct |
4 ms |
5248 KB |
Output is correct |
3 |
Correct |
4 ms |
5248 KB |
Output is correct |
4 |
Correct |
4 ms |
5248 KB |
Output is correct |
5 |
Correct |
4 ms |
5248 KB |
Output is correct |
6 |
Correct |
4 ms |
5248 KB |
Output is correct |
7 |
Correct |
4 ms |
5248 KB |
Output is correct |
8 |
Correct |
4 ms |
5248 KB |
Output is correct |
9 |
Correct |
4 ms |
5248 KB |
Output is correct |
10 |
Correct |
4 ms |
5248 KB |
Output is correct |
11 |
Correct |
4 ms |
5248 KB |
Output is correct |
12 |
Correct |
4 ms |
5248 KB |
Output is correct |
13 |
Correct |
4 ms |
5248 KB |
Output is correct |
14 |
Correct |
4 ms |
5248 KB |
Output is correct |
15 |
Correct |
4 ms |
5248 KB |
Output is correct |
16 |
Correct |
4 ms |
5248 KB |
Output is correct |
17 |
Correct |
4 ms |
5248 KB |
Output is correct |
18 |
Correct |
4 ms |
5248 KB |
Output is correct |
19 |
Correct |
4 ms |
5248 KB |
Output is correct |
20 |
Correct |
4 ms |
5248 KB |
Output is correct |
21 |
Correct |
7 ms |
5376 KB |
Output is correct |
22 |
Correct |
6 ms |
5248 KB |
Output is correct |
23 |
Correct |
5 ms |
5248 KB |
Output is correct |
24 |
Correct |
6 ms |
5248 KB |
Output is correct |
25 |
Correct |
7 ms |
5376 KB |
Output is correct |
26 |
Correct |
5 ms |
5248 KB |
Output is correct |
27 |
Correct |
5 ms |
5248 KB |
Output is correct |
28 |
Correct |
7 ms |
5376 KB |
Output is correct |
29 |
Correct |
7 ms |
5376 KB |
Output is correct |
30 |
Correct |
7 ms |
5376 KB |
Output is correct |
31 |
Correct |
8 ms |
5376 KB |
Output is correct |
32 |
Correct |
7 ms |
5376 KB |
Output is correct |
33 |
Correct |
8 ms |
5376 KB |
Output is correct |
34 |
Correct |
6 ms |
5248 KB |
Output is correct |
35 |
Correct |
6 ms |
5248 KB |
Output is correct |
36 |
Correct |
7 ms |
5248 KB |
Output is correct |
37 |
Correct |
7 ms |
5376 KB |
Output is correct |
38 |
Correct |
7 ms |
5376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5248 KB |
Output is correct |
2 |
Correct |
4 ms |
5248 KB |
Output is correct |
3 |
Correct |
4 ms |
5248 KB |
Output is correct |
4 |
Correct |
4 ms |
5248 KB |
Output is correct |
5 |
Correct |
4 ms |
5248 KB |
Output is correct |
6 |
Correct |
4 ms |
5248 KB |
Output is correct |
7 |
Correct |
4 ms |
5248 KB |
Output is correct |
8 |
Correct |
4 ms |
5248 KB |
Output is correct |
9 |
Correct |
4 ms |
5248 KB |
Output is correct |
10 |
Correct |
4 ms |
5248 KB |
Output is correct |
11 |
Correct |
4 ms |
5248 KB |
Output is correct |
12 |
Correct |
4 ms |
5248 KB |
Output is correct |
13 |
Correct |
4 ms |
5248 KB |
Output is correct |
14 |
Correct |
4 ms |
5248 KB |
Output is correct |
15 |
Correct |
4 ms |
5248 KB |
Output is correct |
16 |
Correct |
4 ms |
5248 KB |
Output is correct |
17 |
Correct |
4 ms |
5248 KB |
Output is correct |
18 |
Correct |
4 ms |
5248 KB |
Output is correct |
19 |
Correct |
367 ms |
11844 KB |
Output is correct |
20 |
Correct |
361 ms |
11768 KB |
Output is correct |
21 |
Correct |
389 ms |
11768 KB |
Output is correct |
22 |
Correct |
360 ms |
12024 KB |
Output is correct |
23 |
Correct |
161 ms |
12152 KB |
Output is correct |
24 |
Correct |
144 ms |
12024 KB |
Output is correct |
25 |
Correct |
215 ms |
14712 KB |
Output is correct |
26 |
Correct |
147 ms |
17528 KB |
Output is correct |
27 |
Correct |
393 ms |
18904 KB |
Output is correct |
28 |
Correct |
450 ms |
30328 KB |
Output is correct |
29 |
Correct |
438 ms |
29304 KB |
Output is correct |
30 |
Correct |
386 ms |
18936 KB |
Output is correct |
31 |
Correct |
377 ms |
18936 KB |
Output is correct |
32 |
Correct |
402 ms |
19192 KB |
Output is correct |
33 |
Correct |
647 ms |
17656 KB |
Output is correct |
34 |
Correct |
257 ms |
18552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5248 KB |
Output is correct |
2 |
Correct |
4 ms |
5248 KB |
Output is correct |
3 |
Correct |
4 ms |
5248 KB |
Output is correct |
4 |
Correct |
4 ms |
5248 KB |
Output is correct |
5 |
Correct |
4 ms |
5248 KB |
Output is correct |
6 |
Correct |
4 ms |
5248 KB |
Output is correct |
7 |
Correct |
4 ms |
5248 KB |
Output is correct |
8 |
Correct |
4 ms |
5248 KB |
Output is correct |
9 |
Correct |
4 ms |
5248 KB |
Output is correct |
10 |
Correct |
4 ms |
5248 KB |
Output is correct |
11 |
Correct |
4 ms |
5248 KB |
Output is correct |
12 |
Correct |
4 ms |
5248 KB |
Output is correct |
13 |
Correct |
4 ms |
5248 KB |
Output is correct |
14 |
Correct |
4 ms |
5248 KB |
Output is correct |
15 |
Correct |
4 ms |
5248 KB |
Output is correct |
16 |
Correct |
4 ms |
5248 KB |
Output is correct |
17 |
Correct |
4 ms |
5248 KB |
Output is correct |
18 |
Correct |
4 ms |
5248 KB |
Output is correct |
19 |
Correct |
4 ms |
5248 KB |
Output is correct |
20 |
Correct |
4 ms |
5248 KB |
Output is correct |
21 |
Correct |
7 ms |
5376 KB |
Output is correct |
22 |
Correct |
6 ms |
5248 KB |
Output is correct |
23 |
Correct |
5 ms |
5248 KB |
Output is correct |
24 |
Correct |
6 ms |
5248 KB |
Output is correct |
25 |
Correct |
7 ms |
5376 KB |
Output is correct |
26 |
Correct |
5 ms |
5248 KB |
Output is correct |
27 |
Correct |
5 ms |
5248 KB |
Output is correct |
28 |
Correct |
7 ms |
5376 KB |
Output is correct |
29 |
Correct |
7 ms |
5376 KB |
Output is correct |
30 |
Correct |
7 ms |
5376 KB |
Output is correct |
31 |
Correct |
8 ms |
5376 KB |
Output is correct |
32 |
Correct |
7 ms |
5376 KB |
Output is correct |
33 |
Correct |
8 ms |
5376 KB |
Output is correct |
34 |
Correct |
6 ms |
5248 KB |
Output is correct |
35 |
Correct |
6 ms |
5248 KB |
Output is correct |
36 |
Correct |
7 ms |
5248 KB |
Output is correct |
37 |
Correct |
7 ms |
5376 KB |
Output is correct |
38 |
Correct |
7 ms |
5376 KB |
Output is correct |
39 |
Correct |
367 ms |
11844 KB |
Output is correct |
40 |
Correct |
361 ms |
11768 KB |
Output is correct |
41 |
Correct |
389 ms |
11768 KB |
Output is correct |
42 |
Correct |
360 ms |
12024 KB |
Output is correct |
43 |
Correct |
161 ms |
12152 KB |
Output is correct |
44 |
Correct |
144 ms |
12024 KB |
Output is correct |
45 |
Correct |
215 ms |
14712 KB |
Output is correct |
46 |
Correct |
147 ms |
17528 KB |
Output is correct |
47 |
Correct |
393 ms |
18904 KB |
Output is correct |
48 |
Correct |
450 ms |
30328 KB |
Output is correct |
49 |
Correct |
438 ms |
29304 KB |
Output is correct |
50 |
Correct |
386 ms |
18936 KB |
Output is correct |
51 |
Correct |
377 ms |
18936 KB |
Output is correct |
52 |
Correct |
402 ms |
19192 KB |
Output is correct |
53 |
Correct |
647 ms |
17656 KB |
Output is correct |
54 |
Correct |
257 ms |
18552 KB |
Output is correct |
55 |
Correct |
41 ms |
6144 KB |
Output is correct |
56 |
Correct |
28 ms |
6008 KB |
Output is correct |
57 |
Correct |
252 ms |
12152 KB |
Output is correct |
58 |
Correct |
77 ms |
11756 KB |
Output is correct |
59 |
Correct |
853 ms |
26616 KB |
Output is correct |
60 |
Correct |
2666 ms |
50516 KB |
Output is correct |
61 |
Correct |
560 ms |
19392 KB |
Output is correct |
62 |
Correct |
616 ms |
19320 KB |
Output is correct |
63 |
Correct |
711 ms |
19320 KB |
Output is correct |
64 |
Correct |
2446 ms |
30216 KB |
Output is correct |
65 |
Correct |
316 ms |
19704 KB |
Output is correct |
66 |
Correct |
1375 ms |
28024 KB |
Output is correct |
67 |
Correct |
240 ms |
19684 KB |
Output is correct |
68 |
Correct |
915 ms |
26860 KB |
Output is correct |
69 |
Correct |
902 ms |
26780 KB |
Output is correct |
70 |
Correct |
860 ms |
26104 KB |
Output is correct |