#include "race.h"
#include<bits/stdc++.h>
using namespace std;
#define maxn 200005
#define ll long long
#define pii pair<ll,ll>
ll n,k,son[maxn],lans,nans,d[maxn],dist[maxn];
vector<pii> edge[maxn];
map<ll,pii> mp;
vector<ll> p;
ll pre_dfs(ll u,ll par){
ll Max=0,cu=1;
d[u]=d[par]+1;
for(pii v:edge[u]){
if(v.first==par) continue;
dist[v.first]=dist[u]+v.second;
ll cv=pre_dfs(v.first,u);
if(cv>Max){Max=cv;son[u]=v.first;}
cu+=cv;
}
return cu;
}
void add(ll u){
if(mp.find(dist[u])==mp.end() || mp[dist[u]].first>d[u]) mp[dist[u]]={d[u],1};
else if(mp[dist[u]].first==d[u]) mp[dist[u]].second++;
}
void update(ll l,ll u,ll anc){
if(mp.find(l)!=mp.end()){
if(lans>mp[l].first+d[u]-2*d[anc]){
lans=mp[l].first+d[u]-2*d[anc];
nans=mp[l].second;
}
else if(lans==mp[l].first+d[u]-2*d[anc]) nans+=mp[l].second;
}
}
void cal(ll u,ll par,ll anc){
p.push_back(u);
update(k+2*dist[anc]-dist[u],u,anc);
for(pii v:edge[u]){
if(v.first==par) continue;
cal(v.first,u,anc);
}
}
void dfs(ll u,ll par,ll t){
for(pii v:edge[u]){
if(v.first!=son[u] && v.first!=par) dfs(v.first,u,1);
}
if(son[u]) dfs(son[u],u,0);
//cout << u << " " << dist[u] << ' ' << par << ' ' << t << '\n';
add(u);update(dist[u]+k,u,u);
for(pii v:edge[u]){
if(v.first==par || v.first==son[u]) continue;
cal(v.first,u,u);
for(ll x:p) add(x);
p.clear();
}
//for(auto it:mp) cout << it.first << " " << it.second.first << " " << it.second.second << '\n';
if(t) mp.clear();
}
int best_path(int N,int K,int H[][2],int L[]){
n=N;k=K;
for(ll i=0;i<n-1;i++){
ll u=H[i][0],v=H[i][1],w=L[i];
u++;v++;
edge[u].push_back({v,w});
edge[v].push_back({u,w});
}
lans=LLONG_MAX;
pre_dfs(1,0);dfs(1,0,0);
if(lans==LLONG_MAX) return -1;
else return (int)lans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5020 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5012 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5016 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5020 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5012 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5016 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5148 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5204 KB |
Output is correct |
24 |
Correct |
4 ms |
5120 KB |
Output is correct |
25 |
Correct |
4 ms |
5204 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5204 KB |
Output is correct |
29 |
Correct |
4 ms |
5076 KB |
Output is correct |
30 |
Correct |
3 ms |
5204 KB |
Output is correct |
31 |
Correct |
4 ms |
5204 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5204 KB |
Output is correct |
34 |
Correct |
3 ms |
5152 KB |
Output is correct |
35 |
Correct |
3 ms |
5156 KB |
Output is correct |
36 |
Correct |
3 ms |
5204 KB |
Output is correct |
37 |
Correct |
3 ms |
5152 KB |
Output is correct |
38 |
Correct |
4 ms |
5152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5020 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5012 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5016 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
158 ms |
15644 KB |
Output is correct |
20 |
Correct |
156 ms |
15308 KB |
Output is correct |
21 |
Correct |
159 ms |
15640 KB |
Output is correct |
22 |
Correct |
153 ms |
15436 KB |
Output is correct |
23 |
Correct |
170 ms |
15896 KB |
Output is correct |
24 |
Correct |
123 ms |
15100 KB |
Output is correct |
25 |
Correct |
93 ms |
19608 KB |
Output is correct |
26 |
Correct |
47 ms |
22348 KB |
Output is correct |
27 |
Correct |
186 ms |
25792 KB |
Output is correct |
28 |
Correct |
213 ms |
52532 KB |
Output is correct |
29 |
Correct |
235 ms |
51464 KB |
Output is correct |
30 |
Correct |
186 ms |
25888 KB |
Output is correct |
31 |
Correct |
193 ms |
25868 KB |
Output is correct |
32 |
Correct |
271 ms |
26012 KB |
Output is correct |
33 |
Correct |
278 ms |
25620 KB |
Output is correct |
34 |
Correct |
365 ms |
38120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
5020 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5012 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5016 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5148 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5204 KB |
Output is correct |
24 |
Correct |
4 ms |
5120 KB |
Output is correct |
25 |
Correct |
4 ms |
5204 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5204 KB |
Output is correct |
29 |
Correct |
4 ms |
5076 KB |
Output is correct |
30 |
Correct |
3 ms |
5204 KB |
Output is correct |
31 |
Correct |
4 ms |
5204 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5204 KB |
Output is correct |
34 |
Correct |
3 ms |
5152 KB |
Output is correct |
35 |
Correct |
3 ms |
5156 KB |
Output is correct |
36 |
Correct |
3 ms |
5204 KB |
Output is correct |
37 |
Correct |
3 ms |
5152 KB |
Output is correct |
38 |
Correct |
4 ms |
5152 KB |
Output is correct |
39 |
Correct |
158 ms |
15644 KB |
Output is correct |
40 |
Correct |
156 ms |
15308 KB |
Output is correct |
41 |
Correct |
159 ms |
15640 KB |
Output is correct |
42 |
Correct |
153 ms |
15436 KB |
Output is correct |
43 |
Correct |
170 ms |
15896 KB |
Output is correct |
44 |
Correct |
123 ms |
15100 KB |
Output is correct |
45 |
Correct |
93 ms |
19608 KB |
Output is correct |
46 |
Correct |
47 ms |
22348 KB |
Output is correct |
47 |
Correct |
186 ms |
25792 KB |
Output is correct |
48 |
Correct |
213 ms |
52532 KB |
Output is correct |
49 |
Correct |
235 ms |
51464 KB |
Output is correct |
50 |
Correct |
186 ms |
25888 KB |
Output is correct |
51 |
Correct |
193 ms |
25868 KB |
Output is correct |
52 |
Correct |
271 ms |
26012 KB |
Output is correct |
53 |
Correct |
278 ms |
25620 KB |
Output is correct |
54 |
Correct |
365 ms |
38120 KB |
Output is correct |
55 |
Correct |
15 ms |
6356 KB |
Output is correct |
56 |
Correct |
10 ms |
6044 KB |
Output is correct |
57 |
Correct |
90 ms |
15620 KB |
Output is correct |
58 |
Correct |
51 ms |
13440 KB |
Output is correct |
59 |
Correct |
75 ms |
28540 KB |
Output is correct |
60 |
Correct |
207 ms |
51736 KB |
Output is correct |
61 |
Correct |
224 ms |
27724 KB |
Output is correct |
62 |
Correct |
198 ms |
25888 KB |
Output is correct |
63 |
Correct |
242 ms |
26012 KB |
Output is correct |
64 |
Correct |
535 ms |
32280 KB |
Output is correct |
65 |
Correct |
483 ms |
38760 KB |
Output is correct |
66 |
Correct |
227 ms |
49332 KB |
Output is correct |
67 |
Correct |
159 ms |
24928 KB |
Output is correct |
68 |
Correct |
322 ms |
36492 KB |
Output is correct |
69 |
Correct |
327 ms |
36460 KB |
Output is correct |
70 |
Correct |
327 ms |
34916 KB |
Output is correct |