#include "race.h"
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define deb(x) cout<<#x<<": "<<x<<endl;
vector<int> dis;
vector<int> sz;
vector<int> par;
vector<vector<pair<int,int>>> adj;
vector<bool> vis;
int ans=1e6;
int need;
void szdfs(int node, int parent){
par[node]=parent;
sz[node]=1;
for(int i=0; i<(int) adj[node].size(); ++i){
int x= adj[node][i].first;
if(vis[x] || x==parent) continue;
szdfs(x, node);
sz[node]+=sz[x];
}
}
int Centroid (int node, int siz){
for(int i=0; i<(int) adj[node].size(); ++i){
int x= adj[node][i].first;
if(par[x]!=node || vis[x]) continue;
if(sz[x] > siz/2){
return Centroid ( x, siz);
}
}
return node;
}
queue<pair<int,int>>q;
void distances (int node,int parent, int d, int c){
if(d>need) return;
q.push({d, c});
if(d<=need){
if(dis[need-d]!=-1){
ans=min(ans, dis[need-d]+c);
}
}
for(int i=0; i<(int) adj[node].size(); ++i){
int x=adj[node][i].first;
int y=adj[node][i].second;
if(parent==x || vis[x]) continue;
distances(x, node, d+y, c+1);
}
}
void Centroid_desc (int node){
szdfs(node, -1);
int root= Centroid ( node, sz[node]);
queue<int> elim;
for(int i=0; i<(int) adj[root].size(); ++i){
if(vis[adj[root][i].first] ) continue;
distances(adj[root][i].first, root, adj[root][i].second, 1);
while(!q.empty()){
if(dis[q.front().first]!=-1){
dis[q.front().first]=min(dis[q.front().first], q.front().second);
}
else{
dis[q.front().first]=q.front().second;
}
elim.push(q.front().first);
q.pop();
}
}
if(dis[need]!=-1){
ans=min(ans, dis[need]);
}
vis[root]=1;
while(!elim.empty()){
dis[elim.front()]=-1;
elim.pop();
}
for(int i=0; i<(int) adj[root].size(); ++i){
if(vis[adj[root][i].first]) continue;
Centroid_desc(adj[root][i].first);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
dis.resize(1e6+5, -1);
sz.resize(N);
par.resize(N);
adj.resize(N);
vis.resize(N,false);
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]});
}
need=K;
Centroid_desc(0);
if(ans==1e6){
ans=-1;
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6232 KB |
Output is correct |
2 |
Correct |
2 ms |
6236 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
6236 KB |
Output is correct |
5 |
Correct |
2 ms |
6232 KB |
Output is correct |
6 |
Correct |
2 ms |
6236 KB |
Output is correct |
7 |
Correct |
2 ms |
6248 KB |
Output is correct |
8 |
Correct |
2 ms |
6236 KB |
Output is correct |
9 |
Correct |
2 ms |
6400 KB |
Output is correct |
10 |
Correct |
3 ms |
6236 KB |
Output is correct |
11 |
Correct |
3 ms |
6236 KB |
Output is correct |
12 |
Correct |
2 ms |
6236 KB |
Output is correct |
13 |
Correct |
2 ms |
6236 KB |
Output is correct |
14 |
Correct |
3 ms |
6236 KB |
Output is correct |
15 |
Correct |
2 ms |
6236 KB |
Output is correct |
16 |
Correct |
2 ms |
6236 KB |
Output is correct |
17 |
Correct |
2 ms |
6236 KB |
Output is correct |
18 |
Correct |
2 ms |
6300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6232 KB |
Output is correct |
2 |
Correct |
2 ms |
6236 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
6236 KB |
Output is correct |
5 |
Correct |
2 ms |
6232 KB |
Output is correct |
6 |
Correct |
2 ms |
6236 KB |
Output is correct |
7 |
Correct |
2 ms |
6248 KB |
Output is correct |
8 |
Correct |
2 ms |
6236 KB |
Output is correct |
9 |
Correct |
2 ms |
6400 KB |
Output is correct |
10 |
Correct |
3 ms |
6236 KB |
Output is correct |
11 |
Correct |
3 ms |
6236 KB |
Output is correct |
12 |
Correct |
2 ms |
6236 KB |
Output is correct |
13 |
Correct |
2 ms |
6236 KB |
Output is correct |
14 |
Correct |
3 ms |
6236 KB |
Output is correct |
15 |
Correct |
2 ms |
6236 KB |
Output is correct |
16 |
Correct |
2 ms |
6236 KB |
Output is correct |
17 |
Correct |
2 ms |
6236 KB |
Output is correct |
18 |
Correct |
2 ms |
6300 KB |
Output is correct |
19 |
Correct |
2 ms |
6236 KB |
Output is correct |
20 |
Correct |
2 ms |
6236 KB |
Output is correct |
21 |
Correct |
3 ms |
6488 KB |
Output is correct |
22 |
Correct |
2 ms |
6488 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
3 ms |
6492 KB |
Output is correct |
25 |
Correct |
2 ms |
6492 KB |
Output is correct |
26 |
Correct |
2 ms |
6492 KB |
Output is correct |
27 |
Correct |
2 ms |
6492 KB |
Output is correct |
28 |
Correct |
2 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
6492 KB |
Output is correct |
30 |
Correct |
2 ms |
6492 KB |
Output is correct |
31 |
Correct |
3 ms |
6312 KB |
Output is correct |
32 |
Correct |
3 ms |
6492 KB |
Output is correct |
33 |
Correct |
2 ms |
6492 KB |
Output is correct |
34 |
Correct |
2 ms |
6492 KB |
Output is correct |
35 |
Correct |
3 ms |
6492 KB |
Output is correct |
36 |
Correct |
3 ms |
6492 KB |
Output is correct |
37 |
Correct |
3 ms |
6492 KB |
Output is correct |
38 |
Correct |
2 ms |
6264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6232 KB |
Output is correct |
2 |
Correct |
2 ms |
6236 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
6236 KB |
Output is correct |
5 |
Correct |
2 ms |
6232 KB |
Output is correct |
6 |
Correct |
2 ms |
6236 KB |
Output is correct |
7 |
Correct |
2 ms |
6248 KB |
Output is correct |
8 |
Correct |
2 ms |
6236 KB |
Output is correct |
9 |
Correct |
2 ms |
6400 KB |
Output is correct |
10 |
Correct |
3 ms |
6236 KB |
Output is correct |
11 |
Correct |
3 ms |
6236 KB |
Output is correct |
12 |
Correct |
2 ms |
6236 KB |
Output is correct |
13 |
Correct |
2 ms |
6236 KB |
Output is correct |
14 |
Correct |
3 ms |
6236 KB |
Output is correct |
15 |
Correct |
2 ms |
6236 KB |
Output is correct |
16 |
Correct |
2 ms |
6236 KB |
Output is correct |
17 |
Correct |
2 ms |
6236 KB |
Output is correct |
18 |
Correct |
2 ms |
6300 KB |
Output is correct |
19 |
Correct |
89 ms |
15500 KB |
Output is correct |
20 |
Correct |
82 ms |
15440 KB |
Output is correct |
21 |
Correct |
96 ms |
15452 KB |
Output is correct |
22 |
Correct |
91 ms |
15708 KB |
Output is correct |
23 |
Correct |
57 ms |
15708 KB |
Output is correct |
24 |
Correct |
50 ms |
15448 KB |
Output is correct |
25 |
Correct |
88 ms |
19044 KB |
Output is correct |
26 |
Correct |
74 ms |
23132 KB |
Output is correct |
27 |
Correct |
111 ms |
22348 KB |
Output is correct |
28 |
Correct |
188 ms |
36888 KB |
Output is correct |
29 |
Correct |
190 ms |
35632 KB |
Output is correct |
30 |
Correct |
110 ms |
22352 KB |
Output is correct |
31 |
Correct |
122 ms |
22604 KB |
Output is correct |
32 |
Correct |
150 ms |
22224 KB |
Output is correct |
33 |
Correct |
151 ms |
21324 KB |
Output is correct |
34 |
Correct |
136 ms |
21072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6232 KB |
Output is correct |
2 |
Correct |
2 ms |
6236 KB |
Output is correct |
3 |
Correct |
2 ms |
6236 KB |
Output is correct |
4 |
Correct |
2 ms |
6236 KB |
Output is correct |
5 |
Correct |
2 ms |
6232 KB |
Output is correct |
6 |
Correct |
2 ms |
6236 KB |
Output is correct |
7 |
Correct |
2 ms |
6248 KB |
Output is correct |
8 |
Correct |
2 ms |
6236 KB |
Output is correct |
9 |
Correct |
2 ms |
6400 KB |
Output is correct |
10 |
Correct |
3 ms |
6236 KB |
Output is correct |
11 |
Correct |
3 ms |
6236 KB |
Output is correct |
12 |
Correct |
2 ms |
6236 KB |
Output is correct |
13 |
Correct |
2 ms |
6236 KB |
Output is correct |
14 |
Correct |
3 ms |
6236 KB |
Output is correct |
15 |
Correct |
2 ms |
6236 KB |
Output is correct |
16 |
Correct |
2 ms |
6236 KB |
Output is correct |
17 |
Correct |
2 ms |
6236 KB |
Output is correct |
18 |
Correct |
2 ms |
6300 KB |
Output is correct |
19 |
Correct |
2 ms |
6236 KB |
Output is correct |
20 |
Correct |
2 ms |
6236 KB |
Output is correct |
21 |
Correct |
3 ms |
6488 KB |
Output is correct |
22 |
Correct |
2 ms |
6488 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
3 ms |
6492 KB |
Output is correct |
25 |
Correct |
2 ms |
6492 KB |
Output is correct |
26 |
Correct |
2 ms |
6492 KB |
Output is correct |
27 |
Correct |
2 ms |
6492 KB |
Output is correct |
28 |
Correct |
2 ms |
6492 KB |
Output is correct |
29 |
Correct |
2 ms |
6492 KB |
Output is correct |
30 |
Correct |
2 ms |
6492 KB |
Output is correct |
31 |
Correct |
3 ms |
6312 KB |
Output is correct |
32 |
Correct |
3 ms |
6492 KB |
Output is correct |
33 |
Correct |
2 ms |
6492 KB |
Output is correct |
34 |
Correct |
2 ms |
6492 KB |
Output is correct |
35 |
Correct |
3 ms |
6492 KB |
Output is correct |
36 |
Correct |
3 ms |
6492 KB |
Output is correct |
37 |
Correct |
3 ms |
6492 KB |
Output is correct |
38 |
Correct |
2 ms |
6264 KB |
Output is correct |
39 |
Correct |
89 ms |
15500 KB |
Output is correct |
40 |
Correct |
82 ms |
15440 KB |
Output is correct |
41 |
Correct |
96 ms |
15452 KB |
Output is correct |
42 |
Correct |
91 ms |
15708 KB |
Output is correct |
43 |
Correct |
57 ms |
15708 KB |
Output is correct |
44 |
Correct |
50 ms |
15448 KB |
Output is correct |
45 |
Correct |
88 ms |
19044 KB |
Output is correct |
46 |
Correct |
74 ms |
23132 KB |
Output is correct |
47 |
Correct |
111 ms |
22348 KB |
Output is correct |
48 |
Correct |
188 ms |
36888 KB |
Output is correct |
49 |
Correct |
190 ms |
35632 KB |
Output is correct |
50 |
Correct |
110 ms |
22352 KB |
Output is correct |
51 |
Correct |
122 ms |
22604 KB |
Output is correct |
52 |
Correct |
150 ms |
22224 KB |
Output is correct |
53 |
Correct |
151 ms |
21324 KB |
Output is correct |
54 |
Correct |
136 ms |
21072 KB |
Output is correct |
55 |
Correct |
8 ms |
7004 KB |
Output is correct |
56 |
Correct |
8 ms |
7004 KB |
Output is correct |
57 |
Correct |
61 ms |
16140 KB |
Output is correct |
58 |
Correct |
31 ms |
16080 KB |
Output is correct |
59 |
Correct |
78 ms |
23120 KB |
Output is correct |
60 |
Correct |
269 ms |
37324 KB |
Output is correct |
61 |
Correct |
128 ms |
22620 KB |
Output is correct |
62 |
Correct |
148 ms |
23120 KB |
Output is correct |
63 |
Correct |
193 ms |
23136 KB |
Output is correct |
64 |
Correct |
287 ms |
23184 KB |
Output is correct |
65 |
Correct |
149 ms |
22352 KB |
Output is correct |
66 |
Correct |
280 ms |
33276 KB |
Output is correct |
67 |
Correct |
87 ms |
23504 KB |
Output is correct |
68 |
Correct |
163 ms |
22864 KB |
Output is correct |
69 |
Correct |
191 ms |
23072 KB |
Output is correct |
70 |
Correct |
199 ms |
22280 KB |
Output is correct |