#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define F first
#define S second
#define INF 1e18
#define all(v) (v).begin(),(v).end()
#define rall(v) (v).rbegin(),(v).rend()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define OK cout<<"Ok"<<endl;
#define MOD (ll)(1e9+7)
const int mxn=2e5+5;
int n,k,par[mxn],sub[mxn],color[mxn],sz,first,ans=1e9;
map<int,int>global,local;
vector<pii>vt[mxn];
void calc(int u,int p){
sz++;
sub[u]=1;
for(pii pp:vt[u]){
int v=pp.F;
if(v!=p&&!color[v]){
calc(v,u);
sub[u]+=sub[v];
}
}
}
int find_centroid(int u,int p){
for(pii pp:vt[u]){
int v=pp.F;
if(v==p) continue;
if(sub[v]*2>sz&&!color[v]){
return find_centroid(v,u);
}
}
return u;
}
void dfs(int u,int p,ll dis,int dep){
if(dis==k) ans=min(ans,dep);
if(dep>0&&dis<=k){
if(global[k-dis]!=0){
ans=min(ans,dep+global[k-dis]);
}
if(local[dis]==0){
local[dis]=dep;
}
else{
local[dis]=min(local[dis],dep);
}
}
for(pii pp:vt[u]){
int v=pp.F,len=pp.S;
if(v!=p&&!color[v]){
if(dep==0) local.clear();
dfs(v,u,dis+len,dep+1);
if(dep==0){
for(auto it=local.begin();it!=local.end();it++){
int x=it->F;
int y=it->S;
if(global[x]==0){
global[x]=y;
}
else{
global[x]=min(global[x],y);
}
}
}
}
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n=N,k=K;
for(int i=0;i<n-1;i++){
int u=H[i][0]+1,v=H[i][1]+1;
vt[u].pb({v,L[i]});
vt[v].pb({u,L[i]});
if(L[i]==k) return 1;
}
for(int id=1;id<=20;id++){
for(int i=1;i<=n;i++){
if(!color[i]){
sz=0;
calc(i,-1);
int node=find_centroid(i,-1);
color[node]=1;
global.clear();
local.clear();
dfs(node,-1,0,0);
}
}
}
if(ans==1e9) return -1;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4936 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4936 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
4 ms |
5076 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5076 KB |
Output is correct |
30 |
Correct |
5 ms |
5076 KB |
Output is correct |
31 |
Correct |
4 ms |
5076 KB |
Output is correct |
32 |
Correct |
4 ms |
5188 KB |
Output is correct |
33 |
Correct |
5 ms |
5076 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
4 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5076 KB |
Output is correct |
37 |
Correct |
4 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4936 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
199 ms |
10632 KB |
Output is correct |
20 |
Correct |
207 ms |
10624 KB |
Output is correct |
21 |
Correct |
195 ms |
10616 KB |
Output is correct |
22 |
Correct |
193 ms |
10716 KB |
Output is correct |
23 |
Correct |
20 ms |
6100 KB |
Output is correct |
24 |
Correct |
76 ms |
10724 KB |
Output is correct |
25 |
Correct |
158 ms |
17060 KB |
Output is correct |
26 |
Correct |
89 ms |
17928 KB |
Output is correct |
27 |
Correct |
194 ms |
16388 KB |
Output is correct |
28 |
Correct |
296 ms |
30680 KB |
Output is correct |
29 |
Correct |
315 ms |
29620 KB |
Output is correct |
30 |
Correct |
198 ms |
16480 KB |
Output is correct |
31 |
Correct |
189 ms |
16496 KB |
Output is correct |
32 |
Correct |
281 ms |
16484 KB |
Output is correct |
33 |
Correct |
346 ms |
15416 KB |
Output is correct |
34 |
Correct |
202 ms |
15468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
2 ms |
4948 KB |
Output is correct |
5 |
Correct |
2 ms |
4948 KB |
Output is correct |
6 |
Correct |
2 ms |
4948 KB |
Output is correct |
7 |
Correct |
2 ms |
4948 KB |
Output is correct |
8 |
Correct |
2 ms |
4948 KB |
Output is correct |
9 |
Correct |
2 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
2 ms |
4948 KB |
Output is correct |
13 |
Correct |
2 ms |
4948 KB |
Output is correct |
14 |
Correct |
2 ms |
4948 KB |
Output is correct |
15 |
Correct |
2 ms |
4948 KB |
Output is correct |
16 |
Correct |
2 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4936 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
2 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
4 ms |
5076 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
4 ms |
5076 KB |
Output is correct |
30 |
Correct |
5 ms |
5076 KB |
Output is correct |
31 |
Correct |
4 ms |
5076 KB |
Output is correct |
32 |
Correct |
4 ms |
5188 KB |
Output is correct |
33 |
Correct |
5 ms |
5076 KB |
Output is correct |
34 |
Correct |
4 ms |
5076 KB |
Output is correct |
35 |
Correct |
4 ms |
5076 KB |
Output is correct |
36 |
Correct |
4 ms |
5076 KB |
Output is correct |
37 |
Correct |
4 ms |
5076 KB |
Output is correct |
38 |
Correct |
4 ms |
5076 KB |
Output is correct |
39 |
Correct |
199 ms |
10632 KB |
Output is correct |
40 |
Correct |
207 ms |
10624 KB |
Output is correct |
41 |
Correct |
195 ms |
10616 KB |
Output is correct |
42 |
Correct |
193 ms |
10716 KB |
Output is correct |
43 |
Correct |
20 ms |
6100 KB |
Output is correct |
44 |
Correct |
76 ms |
10724 KB |
Output is correct |
45 |
Correct |
158 ms |
17060 KB |
Output is correct |
46 |
Correct |
89 ms |
17928 KB |
Output is correct |
47 |
Correct |
194 ms |
16388 KB |
Output is correct |
48 |
Correct |
296 ms |
30680 KB |
Output is correct |
49 |
Correct |
315 ms |
29620 KB |
Output is correct |
50 |
Correct |
198 ms |
16480 KB |
Output is correct |
51 |
Correct |
189 ms |
16496 KB |
Output is correct |
52 |
Correct |
281 ms |
16484 KB |
Output is correct |
53 |
Correct |
346 ms |
15416 KB |
Output is correct |
54 |
Correct |
202 ms |
15468 KB |
Output is correct |
55 |
Correct |
25 ms |
5844 KB |
Output is correct |
56 |
Correct |
16 ms |
5588 KB |
Output is correct |
57 |
Correct |
141 ms |
10876 KB |
Output is correct |
58 |
Correct |
41 ms |
10896 KB |
Output is correct |
59 |
Correct |
499 ms |
26848 KB |
Output is correct |
60 |
Correct |
1578 ms |
51204 KB |
Output is correct |
61 |
Correct |
320 ms |
16896 KB |
Output is correct |
62 |
Correct |
349 ms |
16816 KB |
Output is correct |
63 |
Correct |
420 ms |
16844 KB |
Output is correct |
64 |
Correct |
1513 ms |
27316 KB |
Output is correct |
65 |
Correct |
206 ms |
16400 KB |
Output is correct |
66 |
Correct |
863 ms |
29732 KB |
Output is correct |
67 |
Correct |
110 ms |
16824 KB |
Output is correct |
68 |
Correct |
479 ms |
23812 KB |
Output is correct |
69 |
Correct |
579 ms |
23568 KB |
Output is correct |
70 |
Correct |
515 ms |
23124 KB |
Output is correct |