#include <bits/stdc++.h>
#define NMAX 500001
#define inf (INT_MAX>>2)
#define ll long long
#define add push_back
#define mp make_pair
#include "race.h"
using namespace std;
#define min(a,b) (a > b ? b : a)
vector<pair<int,int>> g[NMAX];
int tot, k, ans;
int sz[NMAX], cen[NMAX];
unordered_map<int,int> all,sub;
void dfs(int node,int par){
sz[node] = 1;
for(auto e:g[node]){
int x = e.first;
if(x == par || cen[x] )continue;
dfs(x,node);
sz[node] += sz[x];
}
tot = max(tot,sz[node]);
}
int centroid(int node,int par){
for(auto e:g[node]){
int x = e.first;
if(x!=par && sz[x]> tot/2 && !cen[x]){
return centroid(x,node);
}
}
cen[node] = 1;
return node;
}
void dfs2(int node, int par, int cc, int len){
if(len == k) ans = min(ans, cc);
if(len >= k) return;
if(all[k-len]) ans = min(ans,all[k-len]+cc);
if(!sub[len]) sub[len] = cc;
sub[len] = min(sub[len],cc);
for(auto e:g[node]){
int x = e.first;
if(!cen[x] && x != par)
dfs2(x,node,cc+1,len + e.second);
}
}
void dcmp(int node,int par){
tot = 0;
dfs(node,par);
int c = centroid(node,par);
all.clear();
for(auto e :g[c]){
int x = e.first;
if(cen[x])continue;
sub.clear();
dfs2(x,c,1,e.second);
for(auto u:sub)
all[u.first] = all[u.first] == 0 ? u.second : min(all[u.first],u.second);
}
for(auto e:g[c]){
int x = e.first;
if(!cen[x]) dcmp(x,c);
}
}
int best_path(int n,int K,int h[][2] ,int L[]) {
k = K;
for(int i = 0 ;i<n-1;i++){
int u = h[i][0], v = h[i][1];
g[u].add(mp(v,L[i]));g[v].add(mp(u,L[i]));
}
memset(cen,0,sizeof(cen));
ans = inf;
dcmp(0,-1);
ans = ans == inf ? -1: ans;
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
14080 KB |
Output is correct |
2 |
Correct |
13 ms |
14080 KB |
Output is correct |
3 |
Correct |
13 ms |
14080 KB |
Output is correct |
4 |
Correct |
13 ms |
14080 KB |
Output is correct |
5 |
Correct |
13 ms |
14048 KB |
Output is correct |
6 |
Correct |
15 ms |
14080 KB |
Output is correct |
7 |
Correct |
13 ms |
14080 KB |
Output is correct |
8 |
Correct |
13 ms |
14080 KB |
Output is correct |
9 |
Correct |
15 ms |
14080 KB |
Output is correct |
10 |
Correct |
14 ms |
14080 KB |
Output is correct |
11 |
Correct |
13 ms |
14080 KB |
Output is correct |
12 |
Correct |
14 ms |
14208 KB |
Output is correct |
13 |
Correct |
13 ms |
14080 KB |
Output is correct |
14 |
Correct |
13 ms |
14080 KB |
Output is correct |
15 |
Correct |
14 ms |
14080 KB |
Output is correct |
16 |
Correct |
14 ms |
14080 KB |
Output is correct |
17 |
Correct |
14 ms |
14080 KB |
Output is correct |
18 |
Correct |
14 ms |
14080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
14080 KB |
Output is correct |
2 |
Correct |
13 ms |
14080 KB |
Output is correct |
3 |
Correct |
13 ms |
14080 KB |
Output is correct |
4 |
Correct |
13 ms |
14080 KB |
Output is correct |
5 |
Correct |
13 ms |
14048 KB |
Output is correct |
6 |
Correct |
15 ms |
14080 KB |
Output is correct |
7 |
Correct |
13 ms |
14080 KB |
Output is correct |
8 |
Correct |
13 ms |
14080 KB |
Output is correct |
9 |
Correct |
15 ms |
14080 KB |
Output is correct |
10 |
Correct |
14 ms |
14080 KB |
Output is correct |
11 |
Correct |
13 ms |
14080 KB |
Output is correct |
12 |
Correct |
14 ms |
14208 KB |
Output is correct |
13 |
Correct |
13 ms |
14080 KB |
Output is correct |
14 |
Correct |
13 ms |
14080 KB |
Output is correct |
15 |
Correct |
14 ms |
14080 KB |
Output is correct |
16 |
Correct |
14 ms |
14080 KB |
Output is correct |
17 |
Correct |
14 ms |
14080 KB |
Output is correct |
18 |
Correct |
14 ms |
14080 KB |
Output is correct |
19 |
Correct |
13 ms |
14080 KB |
Output is correct |
20 |
Correct |
14 ms |
13996 KB |
Output is correct |
21 |
Correct |
17 ms |
14080 KB |
Output is correct |
22 |
Correct |
16 ms |
14208 KB |
Output is correct |
23 |
Correct |
14 ms |
14052 KB |
Output is correct |
24 |
Correct |
14 ms |
14080 KB |
Output is correct |
25 |
Correct |
16 ms |
14208 KB |
Output is correct |
26 |
Correct |
17 ms |
14080 KB |
Output is correct |
27 |
Correct |
14 ms |
14080 KB |
Output is correct |
28 |
Correct |
15 ms |
14180 KB |
Output is correct |
29 |
Correct |
16 ms |
14208 KB |
Output is correct |
30 |
Correct |
16 ms |
14208 KB |
Output is correct |
31 |
Correct |
17 ms |
14208 KB |
Output is correct |
32 |
Correct |
16 ms |
14208 KB |
Output is correct |
33 |
Correct |
16 ms |
14208 KB |
Output is correct |
34 |
Correct |
15 ms |
14080 KB |
Output is correct |
35 |
Correct |
16 ms |
14080 KB |
Output is correct |
36 |
Correct |
17 ms |
14080 KB |
Output is correct |
37 |
Correct |
16 ms |
14220 KB |
Output is correct |
38 |
Correct |
15 ms |
14208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
14080 KB |
Output is correct |
2 |
Correct |
13 ms |
14080 KB |
Output is correct |
3 |
Correct |
13 ms |
14080 KB |
Output is correct |
4 |
Correct |
13 ms |
14080 KB |
Output is correct |
5 |
Correct |
13 ms |
14048 KB |
Output is correct |
6 |
Correct |
15 ms |
14080 KB |
Output is correct |
7 |
Correct |
13 ms |
14080 KB |
Output is correct |
8 |
Correct |
13 ms |
14080 KB |
Output is correct |
9 |
Correct |
15 ms |
14080 KB |
Output is correct |
10 |
Correct |
14 ms |
14080 KB |
Output is correct |
11 |
Correct |
13 ms |
14080 KB |
Output is correct |
12 |
Correct |
14 ms |
14208 KB |
Output is correct |
13 |
Correct |
13 ms |
14080 KB |
Output is correct |
14 |
Correct |
13 ms |
14080 KB |
Output is correct |
15 |
Correct |
14 ms |
14080 KB |
Output is correct |
16 |
Correct |
14 ms |
14080 KB |
Output is correct |
17 |
Correct |
14 ms |
14080 KB |
Output is correct |
18 |
Correct |
14 ms |
14080 KB |
Output is correct |
19 |
Correct |
311 ms |
19316 KB |
Output is correct |
20 |
Correct |
272 ms |
19344 KB |
Output is correct |
21 |
Correct |
298 ms |
19320 KB |
Output is correct |
22 |
Correct |
275 ms |
19448 KB |
Output is correct |
23 |
Correct |
152 ms |
19576 KB |
Output is correct |
24 |
Correct |
147 ms |
19656 KB |
Output is correct |
25 |
Correct |
286 ms |
22076 KB |
Output is correct |
26 |
Correct |
211 ms |
25132 KB |
Output is correct |
27 |
Correct |
328 ms |
24824 KB |
Output is correct |
28 |
Correct |
534 ms |
36088 KB |
Output is correct |
29 |
Correct |
539 ms |
35316 KB |
Output is correct |
30 |
Correct |
327 ms |
24824 KB |
Output is correct |
31 |
Correct |
328 ms |
24696 KB |
Output is correct |
32 |
Correct |
331 ms |
24696 KB |
Output is correct |
33 |
Correct |
593 ms |
23800 KB |
Output is correct |
34 |
Correct |
341 ms |
23544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
14080 KB |
Output is correct |
2 |
Correct |
13 ms |
14080 KB |
Output is correct |
3 |
Correct |
13 ms |
14080 KB |
Output is correct |
4 |
Correct |
13 ms |
14080 KB |
Output is correct |
5 |
Correct |
13 ms |
14048 KB |
Output is correct |
6 |
Correct |
15 ms |
14080 KB |
Output is correct |
7 |
Correct |
13 ms |
14080 KB |
Output is correct |
8 |
Correct |
13 ms |
14080 KB |
Output is correct |
9 |
Correct |
15 ms |
14080 KB |
Output is correct |
10 |
Correct |
14 ms |
14080 KB |
Output is correct |
11 |
Correct |
13 ms |
14080 KB |
Output is correct |
12 |
Correct |
14 ms |
14208 KB |
Output is correct |
13 |
Correct |
13 ms |
14080 KB |
Output is correct |
14 |
Correct |
13 ms |
14080 KB |
Output is correct |
15 |
Correct |
14 ms |
14080 KB |
Output is correct |
16 |
Correct |
14 ms |
14080 KB |
Output is correct |
17 |
Correct |
14 ms |
14080 KB |
Output is correct |
18 |
Correct |
14 ms |
14080 KB |
Output is correct |
19 |
Correct |
13 ms |
14080 KB |
Output is correct |
20 |
Correct |
14 ms |
13996 KB |
Output is correct |
21 |
Correct |
17 ms |
14080 KB |
Output is correct |
22 |
Correct |
16 ms |
14208 KB |
Output is correct |
23 |
Correct |
14 ms |
14052 KB |
Output is correct |
24 |
Correct |
14 ms |
14080 KB |
Output is correct |
25 |
Correct |
16 ms |
14208 KB |
Output is correct |
26 |
Correct |
17 ms |
14080 KB |
Output is correct |
27 |
Correct |
14 ms |
14080 KB |
Output is correct |
28 |
Correct |
15 ms |
14180 KB |
Output is correct |
29 |
Correct |
16 ms |
14208 KB |
Output is correct |
30 |
Correct |
16 ms |
14208 KB |
Output is correct |
31 |
Correct |
17 ms |
14208 KB |
Output is correct |
32 |
Correct |
16 ms |
14208 KB |
Output is correct |
33 |
Correct |
16 ms |
14208 KB |
Output is correct |
34 |
Correct |
15 ms |
14080 KB |
Output is correct |
35 |
Correct |
16 ms |
14080 KB |
Output is correct |
36 |
Correct |
17 ms |
14080 KB |
Output is correct |
37 |
Correct |
16 ms |
14220 KB |
Output is correct |
38 |
Correct |
15 ms |
14208 KB |
Output is correct |
39 |
Correct |
311 ms |
19316 KB |
Output is correct |
40 |
Correct |
272 ms |
19344 KB |
Output is correct |
41 |
Correct |
298 ms |
19320 KB |
Output is correct |
42 |
Correct |
275 ms |
19448 KB |
Output is correct |
43 |
Correct |
152 ms |
19576 KB |
Output is correct |
44 |
Correct |
147 ms |
19656 KB |
Output is correct |
45 |
Correct |
286 ms |
22076 KB |
Output is correct |
46 |
Correct |
211 ms |
25132 KB |
Output is correct |
47 |
Correct |
328 ms |
24824 KB |
Output is correct |
48 |
Correct |
534 ms |
36088 KB |
Output is correct |
49 |
Correct |
539 ms |
35316 KB |
Output is correct |
50 |
Correct |
327 ms |
24824 KB |
Output is correct |
51 |
Correct |
328 ms |
24696 KB |
Output is correct |
52 |
Correct |
331 ms |
24696 KB |
Output is correct |
53 |
Correct |
593 ms |
23800 KB |
Output is correct |
54 |
Correct |
341 ms |
23544 KB |
Output is correct |
55 |
Correct |
71 ms |
14848 KB |
Output is correct |
56 |
Correct |
36 ms |
14564 KB |
Output is correct |
57 |
Correct |
214 ms |
19704 KB |
Output is correct |
58 |
Correct |
67 ms |
19512 KB |
Output is correct |
59 |
Execution timed out |
3015 ms |
33132 KB |
Time limit exceeded |
60 |
Halted |
0 ms |
0 KB |
- |