#include<bits/stdc++.h>
using namespace std;
const int maxN=200005;
const long long big=100000000000000;
long long k;
long long minans=INT_MAX;
vector<int>a[maxN];
int sus[2*maxN],dist[2*maxN];
long long path[1000001];
bool used[maxN];
int sz[maxN];
long long min(long long a,long long b) {
return (a<b?a:b);
}
int upd_sz(int n,int parent=-1) {
sz[n]=1;
for(int ii:a[n]) {
int i=sus[ii];
if(i!=parent && used[i]==0)sz[n]+=upd_sz(i,n);
}
return sz[n];
}
int find_cen(int n,int siz,int parent=-1) {
for(int ii:a[n]) {
int i=sus[ii];
if(used[i]==0 && sz[i]>siz/2 && i!=parent)return find_cen(i,siz,n);
}
return n;
}
void upd_ans(int n,int depth,long long distance,int parent=-1) {
if(k-distance>=0)minans=min(minans,depth+path[k-distance]+big);
for(int ii:a[n]) {
int i=sus[ii];
if(used[i]==0 && i!=parent) {
upd_ans(i,depth+1,distance+dist[ii],n);
}
}
}
vector<int>vec;
void upd_score(int n,int depth,long long distance,int parent=-1) {
if(distance<=k){path[distance]=min(path[distance],depth-big);vec.push_back(distance);}
for(int ii:a[n]) {
int i=sus[ii];
if(used[i]==0 && i!=parent) {
upd_score(i,depth+1,distance+dist[ii],n);
}
}
}
void dfs(int n) {
int c=find_cen(n,upd_sz(n));
used[c]=1;
path[0]=-big;
vec.clear();
for(int ii:a[c]) {
int i=sus[ii];
if(used[i]==0) {
upd_ans(i,1,dist[ii]);
upd_score(i,1,dist[ii]);
}
}
for(int i:vec)path[i]=0;
for(int ii:a[c]) {
int i=sus[ii];
if(used[i]==0)dfs(i);
}
}
int best_path(int N,int K,int H[][2],int L[]) {
k=K;
for(int i=0;i<N-1;i++) {
a[H[i][0]].push_back(i*2);
a[H[i][1]].push_back(i*2+1);
sus[i*2]=H[i][1];
sus[i*2+1]=H[i][0];
dist[i*2]=dist[i*2+1]=L[i];
}
dfs(0);
if(minans==INT_MAX)return -1;
else return minans;
}
/**
int main() {
int n;
cin>>n>>k;
for(int i=0;i<n-1;i++) {
int z;
int x,y;cin>>x>>y>>z;
a[x].push_back(y);
a[y].push_back(x);
dist[{x,y}]=dist[{y,x}]=z;
}
dfs(0);
if(minans==INT_MAX)cout<<-1<<endl;
else cout<<minans<<endl;
return 0;
}*/
/**
4 3
0 1 1
1 2 2
1 3 4
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4984 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4972 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
4 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5052 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4984 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4972 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
4 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5052 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
7 ms |
8356 KB |
Output is correct |
23 |
Correct |
5 ms |
7892 KB |
Output is correct |
24 |
Correct |
6 ms |
9172 KB |
Output is correct |
25 |
Correct |
5 ms |
8276 KB |
Output is correct |
26 |
Correct |
5 ms |
7380 KB |
Output is correct |
27 |
Correct |
5 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
6612 KB |
Output is correct |
29 |
Correct |
4 ms |
7508 KB |
Output is correct |
30 |
Correct |
5 ms |
7764 KB |
Output is correct |
31 |
Correct |
5 ms |
8404 KB |
Output is correct |
32 |
Correct |
5 ms |
8404 KB |
Output is correct |
33 |
Correct |
7 ms |
9812 KB |
Output is correct |
34 |
Correct |
6 ms |
9044 KB |
Output is correct |
35 |
Correct |
8 ms |
9940 KB |
Output is correct |
36 |
Correct |
8 ms |
10388 KB |
Output is correct |
37 |
Correct |
5 ms |
8276 KB |
Output is correct |
38 |
Correct |
6 ms |
7252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4984 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4972 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
4 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5052 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
172 ms |
11540 KB |
Output is correct |
20 |
Correct |
179 ms |
11504 KB |
Output is correct |
21 |
Correct |
226 ms |
11720 KB |
Output is correct |
22 |
Correct |
191 ms |
12036 KB |
Output is correct |
23 |
Correct |
116 ms |
11376 KB |
Output is correct |
24 |
Correct |
66 ms |
11604 KB |
Output is correct |
25 |
Correct |
157 ms |
16076 KB |
Output is correct |
26 |
Correct |
98 ms |
18128 KB |
Output is correct |
27 |
Correct |
253 ms |
18312 KB |
Output is correct |
28 |
Correct |
462 ms |
30180 KB |
Output is correct |
29 |
Correct |
412 ms |
29388 KB |
Output is correct |
30 |
Correct |
321 ms |
18392 KB |
Output is correct |
31 |
Correct |
214 ms |
18376 KB |
Output is correct |
32 |
Correct |
282 ms |
18320 KB |
Output is correct |
33 |
Correct |
320 ms |
17884 KB |
Output is correct |
34 |
Correct |
267 ms |
17800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
2 ms |
4984 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
3 ms |
4972 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
2 ms |
4948 KB |
Output is correct |
11 |
Correct |
2 ms |
4948 KB |
Output is correct |
12 |
Correct |
4 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
5052 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
2 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
2 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
7 ms |
8356 KB |
Output is correct |
23 |
Correct |
5 ms |
7892 KB |
Output is correct |
24 |
Correct |
6 ms |
9172 KB |
Output is correct |
25 |
Correct |
5 ms |
8276 KB |
Output is correct |
26 |
Correct |
5 ms |
7380 KB |
Output is correct |
27 |
Correct |
5 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
6612 KB |
Output is correct |
29 |
Correct |
4 ms |
7508 KB |
Output is correct |
30 |
Correct |
5 ms |
7764 KB |
Output is correct |
31 |
Correct |
5 ms |
8404 KB |
Output is correct |
32 |
Correct |
5 ms |
8404 KB |
Output is correct |
33 |
Correct |
7 ms |
9812 KB |
Output is correct |
34 |
Correct |
6 ms |
9044 KB |
Output is correct |
35 |
Correct |
8 ms |
9940 KB |
Output is correct |
36 |
Correct |
8 ms |
10388 KB |
Output is correct |
37 |
Correct |
5 ms |
8276 KB |
Output is correct |
38 |
Correct |
6 ms |
7252 KB |
Output is correct |
39 |
Correct |
172 ms |
11540 KB |
Output is correct |
40 |
Correct |
179 ms |
11504 KB |
Output is correct |
41 |
Correct |
226 ms |
11720 KB |
Output is correct |
42 |
Correct |
191 ms |
12036 KB |
Output is correct |
43 |
Correct |
116 ms |
11376 KB |
Output is correct |
44 |
Correct |
66 ms |
11604 KB |
Output is correct |
45 |
Correct |
157 ms |
16076 KB |
Output is correct |
46 |
Correct |
98 ms |
18128 KB |
Output is correct |
47 |
Correct |
253 ms |
18312 KB |
Output is correct |
48 |
Correct |
462 ms |
30180 KB |
Output is correct |
49 |
Correct |
412 ms |
29388 KB |
Output is correct |
50 |
Correct |
321 ms |
18392 KB |
Output is correct |
51 |
Correct |
214 ms |
18376 KB |
Output is correct |
52 |
Correct |
282 ms |
18320 KB |
Output is correct |
53 |
Correct |
320 ms |
17884 KB |
Output is correct |
54 |
Correct |
267 ms |
17800 KB |
Output is correct |
55 |
Correct |
10 ms |
5716 KB |
Output is correct |
56 |
Correct |
12 ms |
5668 KB |
Output is correct |
57 |
Correct |
93 ms |
11856 KB |
Output is correct |
58 |
Correct |
40 ms |
12356 KB |
Output is correct |
59 |
Correct |
101 ms |
18964 KB |
Output is correct |
60 |
Correct |
440 ms |
35308 KB |
Output is correct |
61 |
Correct |
271 ms |
21776 KB |
Output is correct |
62 |
Correct |
281 ms |
22392 KB |
Output is correct |
63 |
Correct |
412 ms |
22388 KB |
Output is correct |
64 |
Correct |
806 ms |
24352 KB |
Output is correct |
65 |
Correct |
443 ms |
21812 KB |
Output is correct |
66 |
Correct |
472 ms |
38316 KB |
Output is correct |
67 |
Correct |
132 ms |
23228 KB |
Output is correct |
68 |
Correct |
356 ms |
27684 KB |
Output is correct |
69 |
Correct |
302 ms |
27952 KB |
Output is correct |
70 |
Correct |
306 ms |
26808 KB |
Output is correct |