#include "race.h"
#include <bits/stdc++.h>
#define sc second
#define ff first/*
#include <stdio.h>
#include <stdlib.h>
#define MAX_N 500000
static int N, K;
static int H[MAX_N][2];
static int L[MAX_N];
static int solution;*/
using namespace std;
typedef pair<int,int> ii;
/*inline
void my_assert(int e) {if (!e) abort();}
void read_input()
{
int i;
my_assert(2==scanf("%d %d",&N,&K));
for(i=0; i<N-1; i++)
my_assert(3==scanf("%d %d %d",&H[i][0],&H[i][1],&L[i]));
my_assert(1==scanf("%d",&solution));
}*/
set<ii> G[200010];
int exist[1000010];
int sz[200010];
int edgecnt[1000010];
int tt;
int n,k;
int dfs(int node,int pad){
sz[node]=1;
for(auto i:G[node]){
if(i.ff==pad)continue;
sz[node]+=dfs(i.ff,node);
}
return sz[node];
}
int centroid(int node,int pad,int tam){
//cout<<node<<endl;
for(auto i:G[node]){
if(i.ff==pad)continue;
if(sz[i.ff]>tam/2)return centroid(i.ff,node,tam);
}
return node;
}
int dfs2(int node,int pad,int dis,int edge,int comp,vector<ii>& v){
//cout<<node<<endl;
int want=k-dis;
int ans=1e9;
if(want>=0 && exist[want]==comp){
ans=min(ans,edgecnt[want]+edge);
}
//cout<<k<<endl;
if(dis<=k){
v.push_back({dis,edge});
for(auto i:G[node]){
if(i.ff==pad)continue;
ans=min(ans,dfs2(i.ff,node,dis+i.sc,edge+1,comp,v));
}
}
return ans;
}
int solve(int node,int pad){
int subsz=dfs(node,pad);
int piv=centroid(node,pad,subsz);
//cout<<123<<endl;
int ans=1e9;
int ac=++tt;
exist[0]=ac;edgecnt[0]=0;
for(auto i:G[piv]){
vector<ii> tmp;
ans=min(ans,dfs2(i.ff,piv,i.sc,1,ac,tmp));
//cout<<ans<<" "<<ac<<endl;
for(auto j:tmp){
if(exist[j.ff]!=ac || (exist[j.ff]==ac && edgecnt[j.ff]>j.sc)){
exist[j.ff]=ac;
edgecnt[j.ff]=j.sc;
}
}
}
vector<ii> tmp (G[piv].begin(),G[piv].end());
for(auto i:tmp){
G[i.ff].erase(ii(piv,i.sc));
G[piv].erase(i);
ans=min(ans,solve(i.ff,piv));
//cout<<ans<<" "<<ac<<endl;
}
// cout<<ans<<" "<<ac<<endl;
return ans;
}
int best_path(int N, int K, int H[][2], int L[])
{
tt=0;
k=K;
memset(exist,0,sizeof exist);
for(int i=0;i<N-1;i++){
int a=H[i][0];
int b=H[i][1];
G[a].insert(ii(b,L[i]));
G[b].insert(ii(a,L[i]));
}
//cout<<123<<endl;
int h=solve(1,0);
if(h==1e9)return -1;
return h;
}/*
int main()
{
int ans;
read_input();
ans = best_path(N,K,H,L);
if(ans==solution)
printf("Correct.\n");
else
printf("Incorrect. Returned %d, Expected %d.\n",ans,solution);
return 0;
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
13676 KB |
Output is correct |
2 |
Correct |
9 ms |
13676 KB |
Output is correct |
3 |
Correct |
9 ms |
13676 KB |
Output is correct |
4 |
Correct |
9 ms |
13676 KB |
Output is correct |
5 |
Correct |
10 ms |
13676 KB |
Output is correct |
6 |
Correct |
9 ms |
13676 KB |
Output is correct |
7 |
Correct |
9 ms |
13676 KB |
Output is correct |
8 |
Correct |
9 ms |
13676 KB |
Output is correct |
9 |
Correct |
9 ms |
13676 KB |
Output is correct |
10 |
Correct |
9 ms |
13696 KB |
Output is correct |
11 |
Correct |
9 ms |
13676 KB |
Output is correct |
12 |
Correct |
9 ms |
13676 KB |
Output is correct |
13 |
Correct |
9 ms |
13676 KB |
Output is correct |
14 |
Correct |
9 ms |
13676 KB |
Output is correct |
15 |
Correct |
9 ms |
13676 KB |
Output is correct |
16 |
Correct |
11 ms |
13676 KB |
Output is correct |
17 |
Correct |
11 ms |
13696 KB |
Output is correct |
18 |
Correct |
9 ms |
13676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
13676 KB |
Output is correct |
2 |
Correct |
9 ms |
13676 KB |
Output is correct |
3 |
Correct |
9 ms |
13676 KB |
Output is correct |
4 |
Correct |
9 ms |
13676 KB |
Output is correct |
5 |
Correct |
10 ms |
13676 KB |
Output is correct |
6 |
Correct |
9 ms |
13676 KB |
Output is correct |
7 |
Correct |
9 ms |
13676 KB |
Output is correct |
8 |
Correct |
9 ms |
13676 KB |
Output is correct |
9 |
Correct |
9 ms |
13676 KB |
Output is correct |
10 |
Correct |
9 ms |
13696 KB |
Output is correct |
11 |
Correct |
9 ms |
13676 KB |
Output is correct |
12 |
Correct |
9 ms |
13676 KB |
Output is correct |
13 |
Correct |
9 ms |
13676 KB |
Output is correct |
14 |
Correct |
9 ms |
13676 KB |
Output is correct |
15 |
Correct |
9 ms |
13676 KB |
Output is correct |
16 |
Correct |
11 ms |
13676 KB |
Output is correct |
17 |
Correct |
11 ms |
13696 KB |
Output is correct |
18 |
Correct |
9 ms |
13676 KB |
Output is correct |
19 |
Correct |
9 ms |
13676 KB |
Output is correct |
20 |
Correct |
9 ms |
13676 KB |
Output is correct |
21 |
Correct |
10 ms |
13804 KB |
Output is correct |
22 |
Correct |
11 ms |
16236 KB |
Output is correct |
23 |
Correct |
13 ms |
15980 KB |
Output is correct |
24 |
Correct |
12 ms |
16620 KB |
Output is correct |
25 |
Correct |
12 ms |
15468 KB |
Output is correct |
26 |
Correct |
11 ms |
15084 KB |
Output is correct |
27 |
Correct |
10 ms |
13804 KB |
Output is correct |
28 |
Correct |
10 ms |
14572 KB |
Output is correct |
29 |
Correct |
11 ms |
15084 KB |
Output is correct |
30 |
Correct |
11 ms |
15212 KB |
Output is correct |
31 |
Correct |
11 ms |
15596 KB |
Output is correct |
32 |
Correct |
12 ms |
15596 KB |
Output is correct |
33 |
Correct |
12 ms |
16364 KB |
Output is correct |
34 |
Correct |
11 ms |
15980 KB |
Output is correct |
35 |
Correct |
12 ms |
16748 KB |
Output is correct |
36 |
Correct |
13 ms |
17004 KB |
Output is correct |
37 |
Correct |
11 ms |
15628 KB |
Output is correct |
38 |
Correct |
12 ms |
14956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
13676 KB |
Output is correct |
2 |
Correct |
9 ms |
13676 KB |
Output is correct |
3 |
Correct |
9 ms |
13676 KB |
Output is correct |
4 |
Correct |
9 ms |
13676 KB |
Output is correct |
5 |
Correct |
10 ms |
13676 KB |
Output is correct |
6 |
Correct |
9 ms |
13676 KB |
Output is correct |
7 |
Correct |
9 ms |
13676 KB |
Output is correct |
8 |
Correct |
9 ms |
13676 KB |
Output is correct |
9 |
Correct |
9 ms |
13676 KB |
Output is correct |
10 |
Correct |
9 ms |
13696 KB |
Output is correct |
11 |
Correct |
9 ms |
13676 KB |
Output is correct |
12 |
Correct |
9 ms |
13676 KB |
Output is correct |
13 |
Correct |
9 ms |
13676 KB |
Output is correct |
14 |
Correct |
9 ms |
13676 KB |
Output is correct |
15 |
Correct |
9 ms |
13676 KB |
Output is correct |
16 |
Correct |
11 ms |
13676 KB |
Output is correct |
17 |
Correct |
11 ms |
13696 KB |
Output is correct |
18 |
Correct |
9 ms |
13676 KB |
Output is correct |
19 |
Correct |
253 ms |
24684 KB |
Output is correct |
20 |
Correct |
240 ms |
24684 KB |
Output is correct |
21 |
Correct |
276 ms |
24996 KB |
Output is correct |
22 |
Correct |
308 ms |
25000 KB |
Output is correct |
23 |
Correct |
158 ms |
24684 KB |
Output is correct |
24 |
Correct |
131 ms |
24556 KB |
Output is correct |
25 |
Correct |
244 ms |
30316 KB |
Output is correct |
26 |
Correct |
152 ms |
31708 KB |
Output is correct |
27 |
Correct |
351 ms |
35692 KB |
Output is correct |
28 |
Correct |
504 ms |
47340 KB |
Output is correct |
29 |
Correct |
508 ms |
43116 KB |
Output is correct |
30 |
Correct |
362 ms |
35564 KB |
Output is correct |
31 |
Correct |
357 ms |
35564 KB |
Output is correct |
32 |
Correct |
437 ms |
35436 KB |
Output is correct |
33 |
Correct |
411 ms |
35564 KB |
Output is correct |
34 |
Correct |
346 ms |
35564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
13676 KB |
Output is correct |
2 |
Correct |
9 ms |
13676 KB |
Output is correct |
3 |
Correct |
9 ms |
13676 KB |
Output is correct |
4 |
Correct |
9 ms |
13676 KB |
Output is correct |
5 |
Correct |
10 ms |
13676 KB |
Output is correct |
6 |
Correct |
9 ms |
13676 KB |
Output is correct |
7 |
Correct |
9 ms |
13676 KB |
Output is correct |
8 |
Correct |
9 ms |
13676 KB |
Output is correct |
9 |
Correct |
9 ms |
13676 KB |
Output is correct |
10 |
Correct |
9 ms |
13696 KB |
Output is correct |
11 |
Correct |
9 ms |
13676 KB |
Output is correct |
12 |
Correct |
9 ms |
13676 KB |
Output is correct |
13 |
Correct |
9 ms |
13676 KB |
Output is correct |
14 |
Correct |
9 ms |
13676 KB |
Output is correct |
15 |
Correct |
9 ms |
13676 KB |
Output is correct |
16 |
Correct |
11 ms |
13676 KB |
Output is correct |
17 |
Correct |
11 ms |
13696 KB |
Output is correct |
18 |
Correct |
9 ms |
13676 KB |
Output is correct |
19 |
Correct |
9 ms |
13676 KB |
Output is correct |
20 |
Correct |
9 ms |
13676 KB |
Output is correct |
21 |
Correct |
10 ms |
13804 KB |
Output is correct |
22 |
Correct |
11 ms |
16236 KB |
Output is correct |
23 |
Correct |
13 ms |
15980 KB |
Output is correct |
24 |
Correct |
12 ms |
16620 KB |
Output is correct |
25 |
Correct |
12 ms |
15468 KB |
Output is correct |
26 |
Correct |
11 ms |
15084 KB |
Output is correct |
27 |
Correct |
10 ms |
13804 KB |
Output is correct |
28 |
Correct |
10 ms |
14572 KB |
Output is correct |
29 |
Correct |
11 ms |
15084 KB |
Output is correct |
30 |
Correct |
11 ms |
15212 KB |
Output is correct |
31 |
Correct |
11 ms |
15596 KB |
Output is correct |
32 |
Correct |
12 ms |
15596 KB |
Output is correct |
33 |
Correct |
12 ms |
16364 KB |
Output is correct |
34 |
Correct |
11 ms |
15980 KB |
Output is correct |
35 |
Correct |
12 ms |
16748 KB |
Output is correct |
36 |
Correct |
13 ms |
17004 KB |
Output is correct |
37 |
Correct |
11 ms |
15628 KB |
Output is correct |
38 |
Correct |
12 ms |
14956 KB |
Output is correct |
39 |
Correct |
253 ms |
24684 KB |
Output is correct |
40 |
Correct |
240 ms |
24684 KB |
Output is correct |
41 |
Correct |
276 ms |
24996 KB |
Output is correct |
42 |
Correct |
308 ms |
25000 KB |
Output is correct |
43 |
Correct |
158 ms |
24684 KB |
Output is correct |
44 |
Correct |
131 ms |
24556 KB |
Output is correct |
45 |
Correct |
244 ms |
30316 KB |
Output is correct |
46 |
Correct |
152 ms |
31708 KB |
Output is correct |
47 |
Correct |
351 ms |
35692 KB |
Output is correct |
48 |
Correct |
504 ms |
47340 KB |
Output is correct |
49 |
Correct |
508 ms |
43116 KB |
Output is correct |
50 |
Correct |
362 ms |
35564 KB |
Output is correct |
51 |
Correct |
357 ms |
35564 KB |
Output is correct |
52 |
Correct |
437 ms |
35436 KB |
Output is correct |
53 |
Correct |
411 ms |
35564 KB |
Output is correct |
54 |
Correct |
346 ms |
35564 KB |
Output is correct |
55 |
Correct |
22 ms |
14828 KB |
Output is correct |
56 |
Correct |
22 ms |
14828 KB |
Output is correct |
57 |
Correct |
136 ms |
26600 KB |
Output is correct |
58 |
Correct |
123 ms |
26620 KB |
Output is correct |
59 |
Correct |
149 ms |
33500 KB |
Output is correct |
60 |
Correct |
766 ms |
52176 KB |
Output is correct |
61 |
Correct |
389 ms |
38824 KB |
Output is correct |
62 |
Correct |
431 ms |
39912 KB |
Output is correct |
63 |
Correct |
518 ms |
38568 KB |
Output is correct |
64 |
Correct |
812 ms |
41512 KB |
Output is correct |
65 |
Correct |
476 ms |
39404 KB |
Output is correct |
66 |
Correct |
686 ms |
53356 KB |
Output is correct |
67 |
Correct |
466 ms |
40684 KB |
Output is correct |
68 |
Correct |
553 ms |
41580 KB |
Output is correct |
69 |
Correct |
577 ms |
41936 KB |
Output is correct |
70 |
Correct |
525 ms |
40208 KB |
Output is correct |