#include "race.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <iomanip>
#include <deque>
#include <cassert>
#include <ctime>
#include <cstring>
#include <cstdlib>
using namespace std;
typedef long long ll;
typedef long double ld;
#define INF 2001001001
#define MOD 1000000007
int N,K;
int H[200001][2];
int L[200001];
int subsize[200001];
vector<pair<int,int>>adj[200001];
bool dead[200001];
ll dp[1000001]; //min edges to obtain length i
ll temp[1000001];
ll ans=INF;
int dfs(int node, int par){
subsize[node]=1;
for (int i=0;i<(int)adj[node].size();i++){
int v=adj[node][i].first;
if (!dead[v] && v!=par)
subsize[node]+=dfs(v,node);
}
return subsize[node];
}
int dfs(int node, int par, int size){
for (int i=0;i<(int)adj[node].size();i++){
int v=adj[node][i].first;
if (!dead[v] && v!=par && subsize[v]>size/2)
return dfs(v,node,size);
}
return node;
}
vector<int>vec;
void dfs2(int node, int par, int numEdges, ll dis){
if (dis>K)
return;
vec.push_back(dis);
temp[dis]=min(temp[dis],(ll)numEdges);
for (int i=0;i<(int)adj[node].size();i++){
int v=adj[node][i].first;
if (!dead[v] && v!=par)
dfs2(v,node,numEdges+1,dis+adj[node][i].second);
}
}
void solve(int node){
int size=dfs(node,node);
int centroid=dfs(node,node,size);
dead[centroid]=true;
for (int i=1;i<=K;i++)
dp[i]=INF;
for (int i=0;i<(int)adj[centroid].size();i++){
int v=adj[centroid][i].first;
if (!dead[v]){
for (int i=1;i<=K;i++)
temp[i]=INF;
temp[0]=0;
dfs2(v,centroid,1,adj[centroid][i].second);
for (int j=0;j<(int)vec.size();j++)
ans=min(ans,temp[vec[j]]+dp[K-vec[j]]);
for (int j=0;j<(int)vec.size();j++){
dp[vec[j]]=min(dp[vec[j]],temp[vec[j]]);
temp[vec[j]]=INF;
}
vec.clear();
}
}
for (int i=0;i<(int)adj[centroid].size();i++){
int v=adj[centroid][i].first;
if (!dead[v])
solve(v);
}
}
int best_path(int n, int k, int H[][2], int L[]){
N=n;
K=k;
for (int i=0;i+1<N;i++){
adj[H[i][0]].push_back({H[i][1],L[i]});
adj[H[i][1]].push_back({H[i][0],L[i]});
}
solve(0);
if (ans==INF)
return -1;
else
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5120 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
3 |
Correct |
5 ms |
4992 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Correct |
6 ms |
4992 KB |
Output is correct |
6 |
Correct |
6 ms |
5120 KB |
Output is correct |
7 |
Correct |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
6 ms |
4992 KB |
Output is correct |
9 |
Correct |
6 ms |
5120 KB |
Output is correct |
10 |
Correct |
6 ms |
5248 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
12 |
Correct |
6 ms |
5120 KB |
Output is correct |
13 |
Correct |
6 ms |
5120 KB |
Output is correct |
14 |
Correct |
6 ms |
5120 KB |
Output is correct |
15 |
Correct |
6 ms |
4992 KB |
Output is correct |
16 |
Correct |
6 ms |
5120 KB |
Output is correct |
17 |
Correct |
5 ms |
4992 KB |
Output is correct |
18 |
Correct |
5 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5120 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
3 |
Correct |
5 ms |
4992 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Correct |
6 ms |
4992 KB |
Output is correct |
6 |
Correct |
6 ms |
5120 KB |
Output is correct |
7 |
Correct |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
6 ms |
4992 KB |
Output is correct |
9 |
Correct |
6 ms |
5120 KB |
Output is correct |
10 |
Correct |
6 ms |
5248 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
12 |
Correct |
6 ms |
5120 KB |
Output is correct |
13 |
Correct |
6 ms |
5120 KB |
Output is correct |
14 |
Correct |
6 ms |
5120 KB |
Output is correct |
15 |
Correct |
6 ms |
4992 KB |
Output is correct |
16 |
Correct |
6 ms |
5120 KB |
Output is correct |
17 |
Correct |
5 ms |
4992 KB |
Output is correct |
18 |
Correct |
5 ms |
5120 KB |
Output is correct |
19 |
Correct |
6 ms |
5120 KB |
Output is correct |
20 |
Correct |
6 ms |
5120 KB |
Output is correct |
21 |
Correct |
7 ms |
5120 KB |
Output is correct |
22 |
Correct |
1398 ms |
19584 KB |
Output is correct |
23 |
Correct |
1105 ms |
17016 KB |
Output is correct |
24 |
Correct |
1132 ms |
18680 KB |
Output is correct |
25 |
Correct |
1020 ms |
18428 KB |
Output is correct |
26 |
Correct |
335 ms |
10488 KB |
Output is correct |
27 |
Correct |
1018 ms |
17784 KB |
Output is correct |
28 |
Correct |
193 ms |
8192 KB |
Output is correct |
29 |
Correct |
347 ms |
10240 KB |
Output is correct |
30 |
Correct |
395 ms |
10972 KB |
Output is correct |
31 |
Correct |
844 ms |
15480 KB |
Output is correct |
32 |
Correct |
827 ms |
16376 KB |
Output is correct |
33 |
Correct |
1118 ms |
17412 KB |
Output is correct |
34 |
Correct |
565 ms |
14336 KB |
Output is correct |
35 |
Correct |
1016 ms |
17912 KB |
Output is correct |
36 |
Correct |
1302 ms |
19724 KB |
Output is correct |
37 |
Correct |
933 ms |
17656 KB |
Output is correct |
38 |
Correct |
542 ms |
13056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5120 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
3 |
Correct |
5 ms |
4992 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Correct |
6 ms |
4992 KB |
Output is correct |
6 |
Correct |
6 ms |
5120 KB |
Output is correct |
7 |
Correct |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
6 ms |
4992 KB |
Output is correct |
9 |
Correct |
6 ms |
5120 KB |
Output is correct |
10 |
Correct |
6 ms |
5248 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
12 |
Correct |
6 ms |
5120 KB |
Output is correct |
13 |
Correct |
6 ms |
5120 KB |
Output is correct |
14 |
Correct |
6 ms |
5120 KB |
Output is correct |
15 |
Correct |
6 ms |
4992 KB |
Output is correct |
16 |
Correct |
6 ms |
5120 KB |
Output is correct |
17 |
Correct |
5 ms |
4992 KB |
Output is correct |
18 |
Correct |
5 ms |
5120 KB |
Output is correct |
19 |
Correct |
155 ms |
10524 KB |
Output is correct |
20 |
Correct |
157 ms |
10460 KB |
Output is correct |
21 |
Correct |
188 ms |
10616 KB |
Output is correct |
22 |
Correct |
141 ms |
10744 KB |
Output is correct |
23 |
Correct |
160 ms |
10616 KB |
Output is correct |
24 |
Correct |
88 ms |
10488 KB |
Output is correct |
25 |
Correct |
170 ms |
12472 KB |
Output is correct |
26 |
Correct |
105 ms |
14968 KB |
Output is correct |
27 |
Correct |
228 ms |
15940 KB |
Output is correct |
28 |
Correct |
478 ms |
24000 KB |
Output is correct |
29 |
Correct |
444 ms |
23288 KB |
Output is correct |
30 |
Correct |
226 ms |
15864 KB |
Output is correct |
31 |
Correct |
236 ms |
16120 KB |
Output is correct |
32 |
Correct |
280 ms |
15864 KB |
Output is correct |
33 |
Correct |
309 ms |
14712 KB |
Output is correct |
34 |
Correct |
312 ms |
14712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5120 KB |
Output is correct |
2 |
Correct |
6 ms |
5120 KB |
Output is correct |
3 |
Correct |
5 ms |
4992 KB |
Output is correct |
4 |
Correct |
6 ms |
5120 KB |
Output is correct |
5 |
Correct |
6 ms |
4992 KB |
Output is correct |
6 |
Correct |
6 ms |
5120 KB |
Output is correct |
7 |
Correct |
6 ms |
5120 KB |
Output is correct |
8 |
Correct |
6 ms |
4992 KB |
Output is correct |
9 |
Correct |
6 ms |
5120 KB |
Output is correct |
10 |
Correct |
6 ms |
5248 KB |
Output is correct |
11 |
Correct |
6 ms |
5120 KB |
Output is correct |
12 |
Correct |
6 ms |
5120 KB |
Output is correct |
13 |
Correct |
6 ms |
5120 KB |
Output is correct |
14 |
Correct |
6 ms |
5120 KB |
Output is correct |
15 |
Correct |
6 ms |
4992 KB |
Output is correct |
16 |
Correct |
6 ms |
5120 KB |
Output is correct |
17 |
Correct |
5 ms |
4992 KB |
Output is correct |
18 |
Correct |
5 ms |
5120 KB |
Output is correct |
19 |
Correct |
6 ms |
5120 KB |
Output is correct |
20 |
Correct |
6 ms |
5120 KB |
Output is correct |
21 |
Correct |
7 ms |
5120 KB |
Output is correct |
22 |
Correct |
1398 ms |
19584 KB |
Output is correct |
23 |
Correct |
1105 ms |
17016 KB |
Output is correct |
24 |
Correct |
1132 ms |
18680 KB |
Output is correct |
25 |
Correct |
1020 ms |
18428 KB |
Output is correct |
26 |
Correct |
335 ms |
10488 KB |
Output is correct |
27 |
Correct |
1018 ms |
17784 KB |
Output is correct |
28 |
Correct |
193 ms |
8192 KB |
Output is correct |
29 |
Correct |
347 ms |
10240 KB |
Output is correct |
30 |
Correct |
395 ms |
10972 KB |
Output is correct |
31 |
Correct |
844 ms |
15480 KB |
Output is correct |
32 |
Correct |
827 ms |
16376 KB |
Output is correct |
33 |
Correct |
1118 ms |
17412 KB |
Output is correct |
34 |
Correct |
565 ms |
14336 KB |
Output is correct |
35 |
Correct |
1016 ms |
17912 KB |
Output is correct |
36 |
Correct |
1302 ms |
19724 KB |
Output is correct |
37 |
Correct |
933 ms |
17656 KB |
Output is correct |
38 |
Correct |
542 ms |
13056 KB |
Output is correct |
39 |
Correct |
155 ms |
10524 KB |
Output is correct |
40 |
Correct |
157 ms |
10460 KB |
Output is correct |
41 |
Correct |
188 ms |
10616 KB |
Output is correct |
42 |
Correct |
141 ms |
10744 KB |
Output is correct |
43 |
Correct |
160 ms |
10616 KB |
Output is correct |
44 |
Correct |
88 ms |
10488 KB |
Output is correct |
45 |
Correct |
170 ms |
12472 KB |
Output is correct |
46 |
Correct |
105 ms |
14968 KB |
Output is correct |
47 |
Correct |
228 ms |
15940 KB |
Output is correct |
48 |
Correct |
478 ms |
24000 KB |
Output is correct |
49 |
Correct |
444 ms |
23288 KB |
Output is correct |
50 |
Correct |
226 ms |
15864 KB |
Output is correct |
51 |
Correct |
236 ms |
16120 KB |
Output is correct |
52 |
Correct |
280 ms |
15864 KB |
Output is correct |
53 |
Correct |
309 ms |
14712 KB |
Output is correct |
54 |
Correct |
312 ms |
14712 KB |
Output is correct |
55 |
Correct |
57 ms |
5844 KB |
Output is correct |
56 |
Correct |
16 ms |
5760 KB |
Output is correct |
57 |
Correct |
105 ms |
11160 KB |
Output is correct |
58 |
Correct |
53 ms |
10732 KB |
Output is correct |
59 |
Execution timed out |
3033 ms |
18020 KB |
Time limit exceeded |
60 |
Halted |
0 ms |
0 KB |
- |