#include <bits/stdc++.h>
using namespace std;
int const MN=200005;
map<int,int> cnt;
vector<pair<int,int>> adj[MN];
int sz[MN];
int n,k;
int ans=MN;
void dfs_sz(int node,int par=-1){
sz[node]=1;
for(auto i:adj[node])
if(i.first!=par){
dfs_sz(i.first,node);
sz[node]+=sz[i.first];
}
}
int total=0;
int find_centroid(int node){
for(auto i:adj[node]){
if(sz[i.first]*2>total){
sz[node]-=sz[i.first];
sz[i.first]+=sz[node];
return find_centroid(i.first);
}
}
return node;
}
void dfs(int node,int par,int w,int dep){
if(w==k)
ans=min(ans,dep);
if(w>=k)
return;
if(cnt.find(k-w)!=cnt.end())
ans=min(ans,cnt[k-w]+dep);
for(auto i:adj[node])
if(sz[i.first]!=-1 && i.first!=par)
dfs(i.first,node,w+i.second,dep+1);
}
void update(int node,int par,int w,int dep){
if(w>=k)
return;
if(cnt[w]==0 || cnt[w]>dep)
cnt[w]=dep;
for(auto i:adj[node])
if(sz[i.first]!=-1 && i.first!=par)
update(i.first,node,w+i.second,dep+1);
}
void solve(int node){
total=sz[node];
node=find_centroid(node);
sz[node]=-1;
// cout<<node<<endl;
for(auto i:adj[node]){
if(sz[i.first]==-1)
continue;
dfs(i.first,-1,i.second,1);
update(i.first,-1,i.second,1);
}
cnt.clear();
// cout<<ans<<endl;
for(auto i:adj[node])
if(sz[i.first]!=-1)
solve(i.first);
}
int best_path(int N, int K, int H[][2], int L[]){
n=N;
k=K;
total=n;
for (int i = 0; i < n-1; ++i)
{
int u=H[i][0];
int v=H[i][1];
adj[u].push_back({v,L[i]});
adj[v].push_back({u,L[i]});
}
dfs_sz(0);
solve(0);
if(ans==MN)
ans=-1;
return ans;
}
# |
Verdict |
Execution time |
Memory |
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 |
9560 KB |
Output is correct |
5 |
Correct |
2 ms |
9568 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9548 KB |
Output is correct |
8 |
Correct |
2 ms |
9560 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 |
9560 KB |
Output is correct |
12 |
Correct |
2 ms |
9564 KB |
Output is correct |
13 |
Correct |
3 ms |
9568 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9568 KB |
Output is correct |
16 |
Correct |
3 ms |
9564 KB |
Output is correct |
17 |
Correct |
3 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
10072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
9560 KB |
Output is correct |
5 |
Correct |
2 ms |
9568 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9548 KB |
Output is correct |
8 |
Correct |
2 ms |
9560 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 |
9560 KB |
Output is correct |
12 |
Correct |
2 ms |
9564 KB |
Output is correct |
13 |
Correct |
3 ms |
9568 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9568 KB |
Output is correct |
16 |
Correct |
3 ms |
9564 KB |
Output is correct |
17 |
Correct |
3 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
10072 KB |
Output is correct |
19 |
Correct |
2 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9560 KB |
Output is correct |
21 |
Correct |
3 ms |
9560 KB |
Output is correct |
22 |
Correct |
3 ms |
9564 KB |
Output is correct |
23 |
Correct |
2 ms |
9560 KB |
Output is correct |
24 |
Correct |
4 ms |
9564 KB |
Output is correct |
25 |
Correct |
3 ms |
9564 KB |
Output is correct |
26 |
Correct |
4 ms |
9576 KB |
Output is correct |
27 |
Correct |
2 ms |
9564 KB |
Output is correct |
28 |
Correct |
3 ms |
9564 KB |
Output is correct |
29 |
Correct |
3 ms |
9564 KB |
Output is correct |
30 |
Correct |
3 ms |
9564 KB |
Output is correct |
31 |
Correct |
3 ms |
9564 KB |
Output is correct |
32 |
Correct |
3 ms |
9564 KB |
Output is correct |
33 |
Correct |
3 ms |
9652 KB |
Output is correct |
34 |
Correct |
3 ms |
9564 KB |
Output is correct |
35 |
Correct |
3 ms |
9564 KB |
Output is correct |
36 |
Correct |
3 ms |
9560 KB |
Output is correct |
37 |
Correct |
3 ms |
9560 KB |
Output is correct |
38 |
Correct |
3 ms |
9564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
9560 KB |
Output is correct |
5 |
Correct |
2 ms |
9568 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9548 KB |
Output is correct |
8 |
Correct |
2 ms |
9560 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 |
9560 KB |
Output is correct |
12 |
Correct |
2 ms |
9564 KB |
Output is correct |
13 |
Correct |
3 ms |
9568 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9568 KB |
Output is correct |
16 |
Correct |
3 ms |
9564 KB |
Output is correct |
17 |
Correct |
3 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
10072 KB |
Output is correct |
19 |
Correct |
131 ms |
15548 KB |
Output is correct |
20 |
Correct |
118 ms |
16308 KB |
Output is correct |
21 |
Correct |
137 ms |
16180 KB |
Output is correct |
22 |
Correct |
143 ms |
16720 KB |
Output is correct |
23 |
Correct |
52 ms |
17112 KB |
Output is correct |
24 |
Correct |
50 ms |
16880 KB |
Output is correct |
25 |
Correct |
84 ms |
20220 KB |
Output is correct |
26 |
Correct |
73 ms |
23420 KB |
Output is correct |
27 |
Correct |
153 ms |
20916 KB |
Output is correct |
28 |
Correct |
142 ms |
35312 KB |
Output is correct |
29 |
Correct |
119 ms |
34376 KB |
Output is correct |
30 |
Correct |
133 ms |
21300 KB |
Output is correct |
31 |
Correct |
125 ms |
21532 KB |
Output is correct |
32 |
Correct |
160 ms |
21224 KB |
Output is correct |
33 |
Correct |
160 ms |
20480 KB |
Output is correct |
34 |
Correct |
80 ms |
20560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
9560 KB |
Output is correct |
5 |
Correct |
2 ms |
9568 KB |
Output is correct |
6 |
Correct |
2 ms |
9564 KB |
Output is correct |
7 |
Correct |
2 ms |
9548 KB |
Output is correct |
8 |
Correct |
2 ms |
9560 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 |
9560 KB |
Output is correct |
12 |
Correct |
2 ms |
9564 KB |
Output is correct |
13 |
Correct |
3 ms |
9568 KB |
Output is correct |
14 |
Correct |
2 ms |
9564 KB |
Output is correct |
15 |
Correct |
2 ms |
9568 KB |
Output is correct |
16 |
Correct |
3 ms |
9564 KB |
Output is correct |
17 |
Correct |
3 ms |
9564 KB |
Output is correct |
18 |
Correct |
2 ms |
10072 KB |
Output is correct |
19 |
Correct |
2 ms |
9564 KB |
Output is correct |
20 |
Correct |
2 ms |
9560 KB |
Output is correct |
21 |
Correct |
3 ms |
9560 KB |
Output is correct |
22 |
Correct |
3 ms |
9564 KB |
Output is correct |
23 |
Correct |
2 ms |
9560 KB |
Output is correct |
24 |
Correct |
4 ms |
9564 KB |
Output is correct |
25 |
Correct |
3 ms |
9564 KB |
Output is correct |
26 |
Correct |
4 ms |
9576 KB |
Output is correct |
27 |
Correct |
2 ms |
9564 KB |
Output is correct |
28 |
Correct |
3 ms |
9564 KB |
Output is correct |
29 |
Correct |
3 ms |
9564 KB |
Output is correct |
30 |
Correct |
3 ms |
9564 KB |
Output is correct |
31 |
Correct |
3 ms |
9564 KB |
Output is correct |
32 |
Correct |
3 ms |
9564 KB |
Output is correct |
33 |
Correct |
3 ms |
9652 KB |
Output is correct |
34 |
Correct |
3 ms |
9564 KB |
Output is correct |
35 |
Correct |
3 ms |
9564 KB |
Output is correct |
36 |
Correct |
3 ms |
9560 KB |
Output is correct |
37 |
Correct |
3 ms |
9560 KB |
Output is correct |
38 |
Correct |
3 ms |
9564 KB |
Output is correct |
39 |
Correct |
131 ms |
15548 KB |
Output is correct |
40 |
Correct |
118 ms |
16308 KB |
Output is correct |
41 |
Correct |
137 ms |
16180 KB |
Output is correct |
42 |
Correct |
143 ms |
16720 KB |
Output is correct |
43 |
Correct |
52 ms |
17112 KB |
Output is correct |
44 |
Correct |
50 ms |
16880 KB |
Output is correct |
45 |
Correct |
84 ms |
20220 KB |
Output is correct |
46 |
Correct |
73 ms |
23420 KB |
Output is correct |
47 |
Correct |
153 ms |
20916 KB |
Output is correct |
48 |
Correct |
142 ms |
35312 KB |
Output is correct |
49 |
Correct |
119 ms |
34376 KB |
Output is correct |
50 |
Correct |
133 ms |
21300 KB |
Output is correct |
51 |
Correct |
125 ms |
21532 KB |
Output is correct |
52 |
Correct |
160 ms |
21224 KB |
Output is correct |
53 |
Correct |
160 ms |
20480 KB |
Output is correct |
54 |
Correct |
80 ms |
20560 KB |
Output is correct |
55 |
Correct |
17 ms |
10076 KB |
Output is correct |
56 |
Correct |
14 ms |
9816 KB |
Output is correct |
57 |
Correct |
104 ms |
15868 KB |
Output is correct |
58 |
Correct |
38 ms |
17104 KB |
Output is correct |
59 |
Correct |
212 ms |
27108 KB |
Output is correct |
60 |
Correct |
723 ms |
43380 KB |
Output is correct |
61 |
Correct |
200 ms |
20972 KB |
Output is correct |
62 |
Correct |
206 ms |
21568 KB |
Output is correct |
63 |
Correct |
238 ms |
21752 KB |
Output is correct |
64 |
Correct |
619 ms |
24656 KB |
Output is correct |
65 |
Correct |
89 ms |
22352 KB |
Output is correct |
66 |
Correct |
357 ms |
30036 KB |
Output is correct |
67 |
Correct |
89 ms |
22956 KB |
Output is correct |
68 |
Correct |
301 ms |
25172 KB |
Output is correct |
69 |
Correct |
301 ms |
23048 KB |
Output is correct |
70 |
Correct |
253 ms |
25400 KB |
Output is correct |