#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
typedef double D;
typedef long long ll;
const ll mod=1e9+7;
const ll inf=(1ll<<62);
const int SQ=400;
const int MX=2e5+9;
int n,a,b,subtree[MX],blocked[MX],par[MX],depth[MX],k,dis[MX];
vector<pair<int,int> >v[MX];
vector<int>f;
ll ans=0;
unordered_map<int,int>cnt;
void dfs_size(int x,int p){
subtree[x]=1;par[x]=p;
for(auto pp:v[x]){
if(pp.first==p||blocked[pp.first])continue;
dfs_size(pp.first,x);
subtree[x]+=subtree[pp.first];
}
}
vector<int>nodes;
void DFS(int x,int p,int sum){
if(sum>k)return;
f.push_back(x);
nodes.push_back(x);
dis[x]=sum;
for(auto pp:v[x]){
if(pp.first==p||blocked[pp.first])continue;
depth[pp.first]=depth[x]+1;
DFS(pp.first,x,sum+pp.second);
}
}
void create(int x){
dfs_size(x,-1);
int S=subtree[x],idx;
queue<int>q;
q.push(x);
while(!q.empty()){
int nxt=q.front();q.pop();
int s=subtree[x]-subtree[nxt];
for(auto pp:v[nxt]){
if(pp.first==par[nxt]||blocked[pp.first])continue;
s=max(s,subtree[pp.first]);
q.push(pp.first);
}
if(S>s){
S=s;
idx=nxt;
}
}
for(auto pp:f){
cnt[dis[pp]]=0;
dis[pp]=depth[pp]=0;
}
f.clear();
blocked[idx]=1;
cnt[0]=0;
dis[idx]=depth[idx]=0;
for(auto pp:v[idx]){
if(blocked[pp.first])continue;
depth[pp.first]=1;
DFS(pp.first,idx,pp.second);
for(auto nxt:nodes){
if(k-dis[nxt]<0)continue;
if(dis[nxt]==k)ans=min(ans,(ll)depth[nxt]);
if(cnt[k-dis[nxt]]==0)continue;
ans=min(ans,(ll)depth[nxt]+cnt[k-dis[nxt]]);
}
for(auto i:nodes){
if(cnt[dis[i]]==0){cnt[dis[i]]=depth[i];}
else cnt[dis[i]]=min(cnt[dis[i]],depth[i]);
}
nodes.clear();
}
// cout<<"answer: "<<ans<<endl;
for(auto pp:v[idx]){
if(blocked[pp.first])continue;
create(pp.first);
}
}
int c;
ll best_path(int N,int K,int H[][2],int L[]) {
n=N,k=K;
for(int i=0;i<n-1;i++) {
int x=H[i][0]+1,y=H[i][1]+1,w=L[i];
v[x].push_back({y,w});
v[y].push_back({x,w});
}
ans=inf;
create(1);
if(ans==inf)return -1;
return ans;
}
Compilation message
race.cpp: In function 'void create(int)':
race.cpp:37:22: warning: 'idx' may be used uninitialized in this function [-Wmaybe-uninitialized]
int S=subtree[x],idx;
^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5292 KB |
Output is correct |
4 |
Correct |
6 ms |
5292 KB |
Output is correct |
5 |
Correct |
6 ms |
5292 KB |
Output is correct |
6 |
Correct |
6 ms |
5292 KB |
Output is correct |
7 |
Correct |
7 ms |
5296 KB |
Output is correct |
8 |
Correct |
6 ms |
5348 KB |
Output is correct |
9 |
Correct |
6 ms |
5352 KB |
Output is correct |
10 |
Correct |
7 ms |
5352 KB |
Output is correct |
11 |
Correct |
6 ms |
5480 KB |
Output is correct |
12 |
Correct |
8 ms |
5480 KB |
Output is correct |
13 |
Correct |
7 ms |
5484 KB |
Output is correct |
14 |
Correct |
6 ms |
5484 KB |
Output is correct |
15 |
Correct |
7 ms |
5484 KB |
Output is correct |
16 |
Correct |
7 ms |
5484 KB |
Output is correct |
17 |
Correct |
6 ms |
5484 KB |
Output is correct |
18 |
Correct |
6 ms |
5484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5292 KB |
Output is correct |
4 |
Correct |
6 ms |
5292 KB |
Output is correct |
5 |
Correct |
6 ms |
5292 KB |
Output is correct |
6 |
Correct |
6 ms |
5292 KB |
Output is correct |
7 |
Correct |
7 ms |
5296 KB |
Output is correct |
8 |
Correct |
6 ms |
5348 KB |
Output is correct |
9 |
Correct |
6 ms |
5352 KB |
Output is correct |
10 |
Correct |
7 ms |
5352 KB |
Output is correct |
11 |
Correct |
6 ms |
5480 KB |
Output is correct |
12 |
Correct |
8 ms |
5480 KB |
Output is correct |
13 |
Correct |
7 ms |
5484 KB |
Output is correct |
14 |
Correct |
6 ms |
5484 KB |
Output is correct |
15 |
Correct |
7 ms |
5484 KB |
Output is correct |
16 |
Correct |
7 ms |
5484 KB |
Output is correct |
17 |
Correct |
6 ms |
5484 KB |
Output is correct |
18 |
Correct |
6 ms |
5484 KB |
Output is correct |
19 |
Correct |
6 ms |
5484 KB |
Output is correct |
20 |
Correct |
6 ms |
5484 KB |
Output is correct |
21 |
Correct |
7 ms |
5484 KB |
Output is correct |
22 |
Correct |
7 ms |
5500 KB |
Output is correct |
23 |
Correct |
7 ms |
5500 KB |
Output is correct |
24 |
Correct |
7 ms |
5500 KB |
Output is correct |
25 |
Correct |
10 ms |
5756 KB |
Output is correct |
26 |
Correct |
7 ms |
5756 KB |
Output is correct |
27 |
Correct |
6 ms |
5756 KB |
Output is correct |
28 |
Correct |
8 ms |
5756 KB |
Output is correct |
29 |
Correct |
10 ms |
5848 KB |
Output is correct |
30 |
Correct |
9 ms |
5848 KB |
Output is correct |
31 |
Correct |
11 ms |
5848 KB |
Output is correct |
32 |
Correct |
10 ms |
5884 KB |
Output is correct |
33 |
Correct |
10 ms |
5884 KB |
Output is correct |
34 |
Correct |
8 ms |
5884 KB |
Output is correct |
35 |
Correct |
9 ms |
5884 KB |
Output is correct |
36 |
Correct |
9 ms |
5884 KB |
Output is correct |
37 |
Correct |
9 ms |
5884 KB |
Output is correct |
38 |
Correct |
9 ms |
5884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5292 KB |
Output is correct |
4 |
Correct |
6 ms |
5292 KB |
Output is correct |
5 |
Correct |
6 ms |
5292 KB |
Output is correct |
6 |
Correct |
6 ms |
5292 KB |
Output is correct |
7 |
Correct |
7 ms |
5296 KB |
Output is correct |
8 |
Correct |
6 ms |
5348 KB |
Output is correct |
9 |
Correct |
6 ms |
5352 KB |
Output is correct |
10 |
Correct |
7 ms |
5352 KB |
Output is correct |
11 |
Correct |
6 ms |
5480 KB |
Output is correct |
12 |
Correct |
8 ms |
5480 KB |
Output is correct |
13 |
Correct |
7 ms |
5484 KB |
Output is correct |
14 |
Correct |
6 ms |
5484 KB |
Output is correct |
15 |
Correct |
7 ms |
5484 KB |
Output is correct |
16 |
Correct |
7 ms |
5484 KB |
Output is correct |
17 |
Correct |
6 ms |
5484 KB |
Output is correct |
18 |
Correct |
6 ms |
5484 KB |
Output is correct |
19 |
Correct |
310 ms |
12308 KB |
Output is correct |
20 |
Correct |
323 ms |
12308 KB |
Output is correct |
21 |
Correct |
358 ms |
12616 KB |
Output is correct |
22 |
Correct |
321 ms |
12864 KB |
Output is correct |
23 |
Correct |
182 ms |
12864 KB |
Output is correct |
24 |
Correct |
159 ms |
12864 KB |
Output is correct |
25 |
Correct |
326 ms |
14844 KB |
Output is correct |
26 |
Correct |
247 ms |
18712 KB |
Output is correct |
27 |
Correct |
301 ms |
19416 KB |
Output is correct |
28 |
Correct |
627 ms |
30332 KB |
Output is correct |
29 |
Correct |
662 ms |
30332 KB |
Output is correct |
30 |
Correct |
299 ms |
30332 KB |
Output is correct |
31 |
Correct |
304 ms |
30332 KB |
Output is correct |
32 |
Correct |
413 ms |
30332 KB |
Output is correct |
33 |
Correct |
576 ms |
30332 KB |
Output is correct |
34 |
Correct |
489 ms |
30332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
6 ms |
5292 KB |
Output is correct |
4 |
Correct |
6 ms |
5292 KB |
Output is correct |
5 |
Correct |
6 ms |
5292 KB |
Output is correct |
6 |
Correct |
6 ms |
5292 KB |
Output is correct |
7 |
Correct |
7 ms |
5296 KB |
Output is correct |
8 |
Correct |
6 ms |
5348 KB |
Output is correct |
9 |
Correct |
6 ms |
5352 KB |
Output is correct |
10 |
Correct |
7 ms |
5352 KB |
Output is correct |
11 |
Correct |
6 ms |
5480 KB |
Output is correct |
12 |
Correct |
8 ms |
5480 KB |
Output is correct |
13 |
Correct |
7 ms |
5484 KB |
Output is correct |
14 |
Correct |
6 ms |
5484 KB |
Output is correct |
15 |
Correct |
7 ms |
5484 KB |
Output is correct |
16 |
Correct |
7 ms |
5484 KB |
Output is correct |
17 |
Correct |
6 ms |
5484 KB |
Output is correct |
18 |
Correct |
6 ms |
5484 KB |
Output is correct |
19 |
Correct |
6 ms |
5484 KB |
Output is correct |
20 |
Correct |
6 ms |
5484 KB |
Output is correct |
21 |
Correct |
7 ms |
5484 KB |
Output is correct |
22 |
Correct |
7 ms |
5500 KB |
Output is correct |
23 |
Correct |
7 ms |
5500 KB |
Output is correct |
24 |
Correct |
7 ms |
5500 KB |
Output is correct |
25 |
Correct |
10 ms |
5756 KB |
Output is correct |
26 |
Correct |
7 ms |
5756 KB |
Output is correct |
27 |
Correct |
6 ms |
5756 KB |
Output is correct |
28 |
Correct |
8 ms |
5756 KB |
Output is correct |
29 |
Correct |
10 ms |
5848 KB |
Output is correct |
30 |
Correct |
9 ms |
5848 KB |
Output is correct |
31 |
Correct |
11 ms |
5848 KB |
Output is correct |
32 |
Correct |
10 ms |
5884 KB |
Output is correct |
33 |
Correct |
10 ms |
5884 KB |
Output is correct |
34 |
Correct |
8 ms |
5884 KB |
Output is correct |
35 |
Correct |
9 ms |
5884 KB |
Output is correct |
36 |
Correct |
9 ms |
5884 KB |
Output is correct |
37 |
Correct |
9 ms |
5884 KB |
Output is correct |
38 |
Correct |
9 ms |
5884 KB |
Output is correct |
39 |
Correct |
310 ms |
12308 KB |
Output is correct |
40 |
Correct |
323 ms |
12308 KB |
Output is correct |
41 |
Correct |
358 ms |
12616 KB |
Output is correct |
42 |
Correct |
321 ms |
12864 KB |
Output is correct |
43 |
Correct |
182 ms |
12864 KB |
Output is correct |
44 |
Correct |
159 ms |
12864 KB |
Output is correct |
45 |
Correct |
326 ms |
14844 KB |
Output is correct |
46 |
Correct |
247 ms |
18712 KB |
Output is correct |
47 |
Correct |
301 ms |
19416 KB |
Output is correct |
48 |
Correct |
627 ms |
30332 KB |
Output is correct |
49 |
Correct |
662 ms |
30332 KB |
Output is correct |
50 |
Correct |
299 ms |
30332 KB |
Output is correct |
51 |
Correct |
304 ms |
30332 KB |
Output is correct |
52 |
Correct |
413 ms |
30332 KB |
Output is correct |
53 |
Correct |
576 ms |
30332 KB |
Output is correct |
54 |
Correct |
489 ms |
30332 KB |
Output is correct |
55 |
Correct |
25 ms |
30332 KB |
Output is correct |
56 |
Correct |
28 ms |
30332 KB |
Output is correct |
57 |
Correct |
221 ms |
30332 KB |
Output is correct |
58 |
Correct |
83 ms |
30332 KB |
Output is correct |
59 |
Correct |
273 ms |
30332 KB |
Output is correct |
60 |
Correct |
2125 ms |
58484 KB |
Output is correct |
61 |
Correct |
354 ms |
58484 KB |
Output is correct |
62 |
Correct |
491 ms |
58484 KB |
Output is correct |
63 |
Correct |
696 ms |
58484 KB |
Output is correct |
64 |
Correct |
2078 ms |
58484 KB |
Output is correct |
65 |
Correct |
495 ms |
58484 KB |
Output is correct |
66 |
Correct |
2101 ms |
58484 KB |
Output is correct |
67 |
Correct |
345 ms |
58484 KB |
Output is correct |
68 |
Correct |
1010 ms |
58484 KB |
Output is correct |
69 |
Correct |
1059 ms |
58484 KB |
Output is correct |
70 |
Correct |
1055 ms |
58484 KB |
Output is correct |