#include "race.h"
#include <bits/stdc++.h>
#define FOR(i, begin, end) for(int i=(begin); i<(end); i++)
#define pb push_back
#define s second
#define f first
using namespace std;
typedef pair<int, int> pii;
const int MxN=2e5+10;
vector<pii> ad[MxN];
bool v[MxN];
int sz[MxN];
void get_sizes(int u, int pst=-1)
{
sz[u]=1;
for(auto it : ad[u]){
int x=it.f;
if(v[x] || x==pst) continue;
get_sizes(x, u);
sz[u]+=sz[x];
}
}
int get_centroid(int u, int max_size, int pst=-1)
{
for(auto it : ad[u]){
int x=it.f;
if(v[x] || x==pst) continue;
if(sz[x]>max_size/2){
return get_centroid(x, max_size, u);
}
}
return u;
}
void trav(int u, multiset<pii> &st, int w_sum, int cn, int pst=-1)
{
st.insert({w_sum,cn});
for(auto it : ad[u]){
int x=it.f, w=it.s;
if(v[x] || x==pst) continue;
trav(x, st, w_sum+w, cn+1, u);
}
}
int ans=1e9;
void calc_paths(int u, int k)
{
int nb=ad[u].size();
multiset<pii> st[nb], full;
FOR(i, 0, nb)
{
int x=ad[u][i].f, w=ad[u][i].s;
if(v[x]) continue;
trav(x, st[i], w, 1, u);
for(auto it : st[i]){
full.insert(it);
}
}
full.insert({0,0});
// cout << u << ":\n";
// for(auto it : full){
// cout << it.f << " " << it.s << '\n';
// }
FOR(i, 0, nb)
{
for(auto it : st[i]){
full.erase(full.find(it));
}
for(auto it : st[i]){
auto fnd=full.lower_bound({k-it.f,0});
if(fnd!=full.end() && (*fnd).f==k-it.f){
ans=min(ans, it.s+(*fnd).s);
}
}
for(auto it : st[i]){
full.insert(it);
}
}
}
void dfs(int u, int k)
{
get_sizes(u);
int ct=get_centroid(u, sz[u]);
// cout << u << " " << ct << endl;
calc_paths(ct, k);
v[ct]=true;
for(auto it : ad[ct]){
int x=it.f;
if(v[x]) continue;
dfs(x, k);
}
}
int best_path(int n, int k, int h[][2], int l[])
{
FOR(i, 0, n-1)
{
ad[h[i][0]].pb({h[i][1],l[i]});
ad[h[i][1]].pb({h[i][0],l[i]});
}
dfs(0, k);
return (ans==int(1e9)?-1:ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9652 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
1 ms |
9564 KB |
Output is correct |
13 |
Correct |
2 ms |
9772 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9560 KB |
Output is correct |
16 |
Correct |
2 ms |
9636 KB |
Output is correct |
17 |
Correct |
2 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
9564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9652 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
1 ms |
9564 KB |
Output is correct |
13 |
Correct |
2 ms |
9772 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9560 KB |
Output is correct |
16 |
Correct |
2 ms |
9636 KB |
Output is correct |
17 |
Correct |
2 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
9564 KB |
Output is correct |
19 |
Correct |
2 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9564 KB |
Output is correct |
21 |
Correct |
4 ms |
9820 KB |
Output is correct |
22 |
Correct |
4 ms |
9820 KB |
Output is correct |
23 |
Correct |
4 ms |
9820 KB |
Output is correct |
24 |
Correct |
4 ms |
9820 KB |
Output is correct |
25 |
Correct |
3 ms |
9768 KB |
Output is correct |
26 |
Correct |
3 ms |
9772 KB |
Output is correct |
27 |
Correct |
3 ms |
9820 KB |
Output is correct |
28 |
Correct |
4 ms |
9820 KB |
Output is correct |
29 |
Correct |
5 ms |
9820 KB |
Output is correct |
30 |
Correct |
4 ms |
9820 KB |
Output is correct |
31 |
Correct |
3 ms |
9896 KB |
Output is correct |
32 |
Correct |
3 ms |
9820 KB |
Output is correct |
33 |
Correct |
4 ms |
9752 KB |
Output is correct |
34 |
Correct |
4 ms |
9820 KB |
Output is correct |
35 |
Correct |
3 ms |
9820 KB |
Output is correct |
36 |
Correct |
3 ms |
9820 KB |
Output is correct |
37 |
Correct |
3 ms |
9820 KB |
Output is correct |
38 |
Correct |
4 ms |
9892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9652 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
1 ms |
9564 KB |
Output is correct |
13 |
Correct |
2 ms |
9772 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9560 KB |
Output is correct |
16 |
Correct |
2 ms |
9636 KB |
Output is correct |
17 |
Correct |
2 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
9564 KB |
Output is correct |
19 |
Correct |
507 ms |
24916 KB |
Output is correct |
20 |
Correct |
467 ms |
26140 KB |
Output is correct |
21 |
Correct |
448 ms |
25728 KB |
Output is correct |
22 |
Correct |
403 ms |
26436 KB |
Output is correct |
23 |
Correct |
582 ms |
26452 KB |
Output is correct |
24 |
Correct |
266 ms |
26452 KB |
Output is correct |
25 |
Correct |
504 ms |
30292 KB |
Output is correct |
26 |
Correct |
381 ms |
32852 KB |
Output is correct |
27 |
Correct |
443 ms |
39860 KB |
Output is correct |
28 |
Correct |
1082 ms |
54296 KB |
Output is correct |
29 |
Correct |
1097 ms |
53244 KB |
Output is correct |
30 |
Correct |
452 ms |
40276 KB |
Output is correct |
31 |
Correct |
451 ms |
40420 KB |
Output is correct |
32 |
Correct |
487 ms |
40020 KB |
Output is correct |
33 |
Correct |
905 ms |
39360 KB |
Output is correct |
34 |
Correct |
1040 ms |
39516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9564 KB |
Output is correct |
2 |
Correct |
2 ms |
9564 KB |
Output is correct |
3 |
Correct |
2 ms |
9564 KB |
Output is correct |
4 |
Correct |
2 ms |
9564 KB |
Output is correct |
5 |
Correct |
2 ms |
9564 KB |
Output is correct |
6 |
Correct |
2 ms |
9652 KB |
Output is correct |
7 |
Correct |
2 ms |
9564 KB |
Output is correct |
8 |
Correct |
2 ms |
9564 KB |
Output is correct |
9 |
Correct |
2 ms |
9564 KB |
Output is correct |
10 |
Correct |
2 ms |
9564 KB |
Output is correct |
11 |
Correct |
2 ms |
9564 KB |
Output is correct |
12 |
Correct |
1 ms |
9564 KB |
Output is correct |
13 |
Correct |
2 ms |
9772 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9560 KB |
Output is correct |
16 |
Correct |
2 ms |
9636 KB |
Output is correct |
17 |
Correct |
2 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
9564 KB |
Output is correct |
19 |
Correct |
2 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9564 KB |
Output is correct |
21 |
Correct |
4 ms |
9820 KB |
Output is correct |
22 |
Correct |
4 ms |
9820 KB |
Output is correct |
23 |
Correct |
4 ms |
9820 KB |
Output is correct |
24 |
Correct |
4 ms |
9820 KB |
Output is correct |
25 |
Correct |
3 ms |
9768 KB |
Output is correct |
26 |
Correct |
3 ms |
9772 KB |
Output is correct |
27 |
Correct |
3 ms |
9820 KB |
Output is correct |
28 |
Correct |
4 ms |
9820 KB |
Output is correct |
29 |
Correct |
5 ms |
9820 KB |
Output is correct |
30 |
Correct |
4 ms |
9820 KB |
Output is correct |
31 |
Correct |
3 ms |
9896 KB |
Output is correct |
32 |
Correct |
3 ms |
9820 KB |
Output is correct |
33 |
Correct |
4 ms |
9752 KB |
Output is correct |
34 |
Correct |
4 ms |
9820 KB |
Output is correct |
35 |
Correct |
3 ms |
9820 KB |
Output is correct |
36 |
Correct |
3 ms |
9820 KB |
Output is correct |
37 |
Correct |
3 ms |
9820 KB |
Output is correct |
38 |
Correct |
4 ms |
9892 KB |
Output is correct |
39 |
Correct |
507 ms |
24916 KB |
Output is correct |
40 |
Correct |
467 ms |
26140 KB |
Output is correct |
41 |
Correct |
448 ms |
25728 KB |
Output is correct |
42 |
Correct |
403 ms |
26436 KB |
Output is correct |
43 |
Correct |
582 ms |
26452 KB |
Output is correct |
44 |
Correct |
266 ms |
26452 KB |
Output is correct |
45 |
Correct |
504 ms |
30292 KB |
Output is correct |
46 |
Correct |
381 ms |
32852 KB |
Output is correct |
47 |
Correct |
443 ms |
39860 KB |
Output is correct |
48 |
Correct |
1082 ms |
54296 KB |
Output is correct |
49 |
Correct |
1097 ms |
53244 KB |
Output is correct |
50 |
Correct |
452 ms |
40276 KB |
Output is correct |
51 |
Correct |
451 ms |
40420 KB |
Output is correct |
52 |
Correct |
487 ms |
40020 KB |
Output is correct |
53 |
Correct |
905 ms |
39360 KB |
Output is correct |
54 |
Correct |
1040 ms |
39516 KB |
Output is correct |
55 |
Correct |
29 ms |
11096 KB |
Output is correct |
56 |
Correct |
37 ms |
11096 KB |
Output is correct |
57 |
Correct |
516 ms |
25428 KB |
Output is correct |
58 |
Correct |
91 ms |
30916 KB |
Output is correct |
59 |
Correct |
448 ms |
33104 KB |
Output is correct |
60 |
Correct |
1095 ms |
53584 KB |
Output is correct |
61 |
Correct |
475 ms |
39704 KB |
Output is correct |
62 |
Correct |
438 ms |
40276 KB |
Output is correct |
63 |
Correct |
453 ms |
40020 KB |
Output is correct |
64 |
Correct |
1084 ms |
38992 KB |
Output is correct |
65 |
Correct |
931 ms |
41556 KB |
Output is correct |
66 |
Correct |
1175 ms |
48960 KB |
Output is correct |
67 |
Correct |
267 ms |
51132 KB |
Output is correct |
68 |
Correct |
518 ms |
40272 KB |
Output is correct |
69 |
Correct |
530 ms |
38484 KB |
Output is correct |
70 |
Correct |
566 ms |
39596 KB |
Output is correct |