#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ii pair<int,int>
#define MAX_N 500000
const int nax=2e5+5;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
vector<pair<int,int>> adj[nax];
int sz[nax];
bool removed[nax];
set<pair<ll,int>> st[nax];
int ans=1e9;
int get_tree_size(int x,int p){
sz[x]=1;
for(auto u:adj[x]){
if(u.fi==p||removed[u.fi]) continue;
sz[x]+=get_tree_size(u.fi,x);
}
return sz[x];
}
int get_centroid(int x,int t_sz,int p){
for(auto u:adj[x]){
if(u.fi==p||removed[u.fi]) continue;
if(sz[u.fi]*2>t_sz){
return get_centroid(u.fi,t_sz,x);
}
}
return x;
}
void dfs(int x,int centroid,int p,pair<ll,int> cur){
for(auto u:adj[x]){
if(u.fi==p||removed[u.fi]) continue;
cur.fi+=u.se;
cur.se++;
dfs(u.fi,centroid,x,cur);
cur.fi-=u.se;
cur.se--;
}
//cout <<x<<" "<<centroid<<" "<<cur.fi<<" "<<cur.se<<endl;
st[centroid].insert(cur);
}
int n,k;
void build(int x){
int centroid=get_centroid(x,get_tree_size(x,-1),-1);
for(auto u:adj[centroid]){
if(removed[u.fi]) continue;
dfs(u.fi,u.fi,centroid,make_pair(u.se,1));
}
multiset<pair<ll,int>> stt;
stt.insert({0,0});
for(auto u:adj[centroid]){
if(removed[u.fi]) continue;
for(auto i:st[u.fi]){
stt.insert(i);
}
}
for(auto u:adj[centroid]){
if(removed[u.fi]) continue;
for(auto i:st[u.fi]){
stt.erase(stt.find(i));
}
for(auto i:st[u.fi]){
if(stt.lower_bound(make_pair(k-i.fi,0))==stt.end()) continue;
auto cur=*stt.lower_bound(make_pair(k-i.fi,0));
if(cur.fi+i.fi==k) ans=min(ans,cur.se+i.se);
}
for(auto i:st[u.fi]){
stt.insert(i);
}
}
for (auto u:adj[centroid])
{
if(removed[u.fi]) continue;
st[u.fi].clear();
}
removed[centroid]=true;
for(auto u:adj[centroid]){
if(removed[u.fi]) continue;
build(u.fi);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n=N;
k=K;
for (int i = 0; i < n-1; ++i)
{
adj[H[i][0]].pb({H[i][1],L[i]});
adj[H[i][1]].pb({H[i][0],L[i]});
}
build(0);
if(ans==1e9) return -1;
else return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16988 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
19036 KB |
Output is correct |
4 |
Correct |
2 ms |
19176 KB |
Output is correct |
5 |
Correct |
3 ms |
16988 KB |
Output is correct |
6 |
Correct |
3 ms |
16988 KB |
Output is correct |
7 |
Correct |
3 ms |
19036 KB |
Output is correct |
8 |
Correct |
2 ms |
16988 KB |
Output is correct |
9 |
Correct |
3 ms |
19036 KB |
Output is correct |
10 |
Correct |
3 ms |
16988 KB |
Output is correct |
11 |
Correct |
3 ms |
16988 KB |
Output is correct |
12 |
Correct |
3 ms |
19036 KB |
Output is correct |
13 |
Correct |
3 ms |
19132 KB |
Output is correct |
14 |
Correct |
3 ms |
19036 KB |
Output is correct |
15 |
Correct |
3 ms |
19036 KB |
Output is correct |
16 |
Correct |
3 ms |
19036 KB |
Output is correct |
17 |
Correct |
3 ms |
19036 KB |
Output is correct |
18 |
Correct |
3 ms |
19188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16988 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
19036 KB |
Output is correct |
4 |
Correct |
2 ms |
19176 KB |
Output is correct |
5 |
Correct |
3 ms |
16988 KB |
Output is correct |
6 |
Correct |
3 ms |
16988 KB |
Output is correct |
7 |
Correct |
3 ms |
19036 KB |
Output is correct |
8 |
Correct |
2 ms |
16988 KB |
Output is correct |
9 |
Correct |
3 ms |
19036 KB |
Output is correct |
10 |
Correct |
3 ms |
16988 KB |
Output is correct |
11 |
Correct |
3 ms |
16988 KB |
Output is correct |
12 |
Correct |
3 ms |
19036 KB |
Output is correct |
13 |
Correct |
3 ms |
19132 KB |
Output is correct |
14 |
Correct |
3 ms |
19036 KB |
Output is correct |
15 |
Correct |
3 ms |
19036 KB |
Output is correct |
16 |
Correct |
3 ms |
19036 KB |
Output is correct |
17 |
Correct |
3 ms |
19036 KB |
Output is correct |
18 |
Correct |
3 ms |
19188 KB |
Output is correct |
19 |
Correct |
3 ms |
19036 KB |
Output is correct |
20 |
Correct |
3 ms |
19036 KB |
Output is correct |
21 |
Correct |
4 ms |
19292 KB |
Output is correct |
22 |
Correct |
5 ms |
19292 KB |
Output is correct |
23 |
Correct |
5 ms |
19544 KB |
Output is correct |
24 |
Correct |
5 ms |
19292 KB |
Output is correct |
25 |
Correct |
6 ms |
19292 KB |
Output is correct |
26 |
Correct |
5 ms |
19332 KB |
Output is correct |
27 |
Correct |
4 ms |
19032 KB |
Output is correct |
28 |
Correct |
5 ms |
19288 KB |
Output is correct |
29 |
Correct |
5 ms |
19288 KB |
Output is correct |
30 |
Correct |
5 ms |
19292 KB |
Output is correct |
31 |
Correct |
5 ms |
19292 KB |
Output is correct |
32 |
Correct |
5 ms |
19292 KB |
Output is correct |
33 |
Correct |
5 ms |
19292 KB |
Output is correct |
34 |
Correct |
5 ms |
19292 KB |
Output is correct |
35 |
Correct |
4 ms |
19292 KB |
Output is correct |
36 |
Correct |
4 ms |
19292 KB |
Output is correct |
37 |
Correct |
4 ms |
19292 KB |
Output is correct |
38 |
Correct |
5 ms |
19172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16988 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
19036 KB |
Output is correct |
4 |
Correct |
2 ms |
19176 KB |
Output is correct |
5 |
Correct |
3 ms |
16988 KB |
Output is correct |
6 |
Correct |
3 ms |
16988 KB |
Output is correct |
7 |
Correct |
3 ms |
19036 KB |
Output is correct |
8 |
Correct |
2 ms |
16988 KB |
Output is correct |
9 |
Correct |
3 ms |
19036 KB |
Output is correct |
10 |
Correct |
3 ms |
16988 KB |
Output is correct |
11 |
Correct |
3 ms |
16988 KB |
Output is correct |
12 |
Correct |
3 ms |
19036 KB |
Output is correct |
13 |
Correct |
3 ms |
19132 KB |
Output is correct |
14 |
Correct |
3 ms |
19036 KB |
Output is correct |
15 |
Correct |
3 ms |
19036 KB |
Output is correct |
16 |
Correct |
3 ms |
19036 KB |
Output is correct |
17 |
Correct |
3 ms |
19036 KB |
Output is correct |
18 |
Correct |
3 ms |
19188 KB |
Output is correct |
19 |
Correct |
309 ms |
26160 KB |
Output is correct |
20 |
Correct |
285 ms |
25968 KB |
Output is correct |
21 |
Correct |
269 ms |
25872 KB |
Output is correct |
22 |
Correct |
233 ms |
26068 KB |
Output is correct |
23 |
Correct |
393 ms |
27240 KB |
Output is correct |
24 |
Correct |
161 ms |
25692 KB |
Output is correct |
25 |
Correct |
522 ms |
41556 KB |
Output is correct |
26 |
Correct |
409 ms |
41476 KB |
Output is correct |
27 |
Correct |
404 ms |
41596 KB |
Output is correct |
28 |
Correct |
1219 ms |
62032 KB |
Output is correct |
29 |
Correct |
1196 ms |
62048 KB |
Output is correct |
30 |
Correct |
409 ms |
41552 KB |
Output is correct |
31 |
Correct |
412 ms |
41560 KB |
Output is correct |
32 |
Correct |
413 ms |
41700 KB |
Output is correct |
33 |
Correct |
546 ms |
31796 KB |
Output is correct |
34 |
Correct |
1164 ms |
55420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
16988 KB |
Output is correct |
2 |
Correct |
3 ms |
16988 KB |
Output is correct |
3 |
Correct |
3 ms |
19036 KB |
Output is correct |
4 |
Correct |
2 ms |
19176 KB |
Output is correct |
5 |
Correct |
3 ms |
16988 KB |
Output is correct |
6 |
Correct |
3 ms |
16988 KB |
Output is correct |
7 |
Correct |
3 ms |
19036 KB |
Output is correct |
8 |
Correct |
2 ms |
16988 KB |
Output is correct |
9 |
Correct |
3 ms |
19036 KB |
Output is correct |
10 |
Correct |
3 ms |
16988 KB |
Output is correct |
11 |
Correct |
3 ms |
16988 KB |
Output is correct |
12 |
Correct |
3 ms |
19036 KB |
Output is correct |
13 |
Correct |
3 ms |
19132 KB |
Output is correct |
14 |
Correct |
3 ms |
19036 KB |
Output is correct |
15 |
Correct |
3 ms |
19036 KB |
Output is correct |
16 |
Correct |
3 ms |
19036 KB |
Output is correct |
17 |
Correct |
3 ms |
19036 KB |
Output is correct |
18 |
Correct |
3 ms |
19188 KB |
Output is correct |
19 |
Correct |
3 ms |
19036 KB |
Output is correct |
20 |
Correct |
3 ms |
19036 KB |
Output is correct |
21 |
Correct |
4 ms |
19292 KB |
Output is correct |
22 |
Correct |
5 ms |
19292 KB |
Output is correct |
23 |
Correct |
5 ms |
19544 KB |
Output is correct |
24 |
Correct |
5 ms |
19292 KB |
Output is correct |
25 |
Correct |
6 ms |
19292 KB |
Output is correct |
26 |
Correct |
5 ms |
19332 KB |
Output is correct |
27 |
Correct |
4 ms |
19032 KB |
Output is correct |
28 |
Correct |
5 ms |
19288 KB |
Output is correct |
29 |
Correct |
5 ms |
19288 KB |
Output is correct |
30 |
Correct |
5 ms |
19292 KB |
Output is correct |
31 |
Correct |
5 ms |
19292 KB |
Output is correct |
32 |
Correct |
5 ms |
19292 KB |
Output is correct |
33 |
Correct |
5 ms |
19292 KB |
Output is correct |
34 |
Correct |
5 ms |
19292 KB |
Output is correct |
35 |
Correct |
4 ms |
19292 KB |
Output is correct |
36 |
Correct |
4 ms |
19292 KB |
Output is correct |
37 |
Correct |
4 ms |
19292 KB |
Output is correct |
38 |
Correct |
5 ms |
19172 KB |
Output is correct |
39 |
Correct |
309 ms |
26160 KB |
Output is correct |
40 |
Correct |
285 ms |
25968 KB |
Output is correct |
41 |
Correct |
269 ms |
25872 KB |
Output is correct |
42 |
Correct |
233 ms |
26068 KB |
Output is correct |
43 |
Correct |
393 ms |
27240 KB |
Output is correct |
44 |
Correct |
161 ms |
25692 KB |
Output is correct |
45 |
Correct |
522 ms |
41556 KB |
Output is correct |
46 |
Correct |
409 ms |
41476 KB |
Output is correct |
47 |
Correct |
404 ms |
41596 KB |
Output is correct |
48 |
Correct |
1219 ms |
62032 KB |
Output is correct |
49 |
Correct |
1196 ms |
62048 KB |
Output is correct |
50 |
Correct |
409 ms |
41552 KB |
Output is correct |
51 |
Correct |
412 ms |
41560 KB |
Output is correct |
52 |
Correct |
413 ms |
41700 KB |
Output is correct |
53 |
Correct |
546 ms |
31796 KB |
Output is correct |
54 |
Correct |
1164 ms |
55420 KB |
Output is correct |
55 |
Correct |
32 ms |
20824 KB |
Output is correct |
56 |
Correct |
20 ms |
19548 KB |
Output is correct |
57 |
Correct |
143 ms |
25588 KB |
Output is correct |
58 |
Correct |
86 ms |
38852 KB |
Output is correct |
59 |
Correct |
440 ms |
42324 KB |
Output is correct |
60 |
Correct |
1138 ms |
64080 KB |
Output is correct |
61 |
Correct |
404 ms |
46416 KB |
Output is correct |
62 |
Correct |
375 ms |
43860 KB |
Output is correct |
63 |
Correct |
396 ms |
43604 KB |
Output is correct |
64 |
Correct |
1060 ms |
53516 KB |
Output is correct |
65 |
Correct |
897 ms |
56912 KB |
Output is correct |
66 |
Correct |
1252 ms |
62036 KB |
Output is correct |
67 |
Correct |
256 ms |
57508 KB |
Output is correct |
68 |
Correct |
592 ms |
55780 KB |
Output is correct |
69 |
Correct |
640 ms |
54108 KB |
Output is correct |
70 |
Correct |
545 ms |
54612 KB |
Output is correct |