#include "race.h"
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define in insert
const int mxn=2e5+10,mxk=1e6+10;
vector<pair<int,int> > g[mxn];
int global_answer=-1,n,k,vis[mxn],sub[mxn],nxt,mx;
int book_keeping,minimum_paths[mxk],achievable[mxk];
void dfs(int to,int fr)
{
sub[to]=1;
for(auto &x : g[to])
{
if(x.ff==fr || vis[x.ff]) continue;
dfs(x.ff,to);
sub[to]+=sub[x.ff];
}
}
void get_cen(int to,int fr,int sz)
{
int lomx=sz-sub[to];
for(auto &x : g[to])
{
if(x.ff==fr || vis[x.ff]) continue;
get_cen(x.ff,to,sz);
lomx=max(lomx,sub[x.ff]);
}
if(lomx<mx)
{
mx=lomx;
nxt=to;
}
}
void dfs2(int to,int fr,int current_cost,int current_length,int fill)
{
if(current_cost>k) return;
if (!fill)
{
if (achievable[k - current_cost] == book_keeping)
if (current_length + minimum_paths[k - current_cost] < global_answer || global_answer == -1)
global_answer = current_length + minimum_paths[k - current_cost];
if (current_cost == k)
if (current_length < global_answer || global_answer == -1)
global_answer = current_length;
}
else
{
if (achievable[current_cost] < book_keeping)
{
achievable[current_cost] = book_keeping;
minimum_paths[current_cost] = current_length;
}
else if (current_length < minimum_paths[current_cost])
{
achievable[current_cost] = book_keeping;
minimum_paths[current_cost] = current_length;
}
}
for(auto &x : g[to])
{
if(x.ff==fr || vis[x.ff]) continue;
dfs2(x.ff,to,current_cost+x.ss,current_length+1,fill);
}
}
void solve(int id)
{
dfs(id,-1);
if(sub[id]<=1) return;
mx=sub[id];
get_cen(id,-1,sub[id]);
id=nxt;
book_keeping++;
for(auto &x : g[id])
{
if(vis[x.ff]) continue;
dfs2(x.ff,id,x.ss,1,0);
dfs2(x.ff,id,x.ss,1,1);
}
vis[id]=1;
for(auto &x : g[id])
{
if(vis[x.ff]) continue;
solve(x.ff);
}
}
int best_path(int N, int K, int h[][2], int l[])
{
n=N;
k=K;
memset(vis,0,sizeof(vis));
for(int i=0; i<N-1; i++)
{
g[h[i][0]].pb({h[i][1],l[i]});
g[h[i][1]].pb({h[i][0],l[i]});
}
solve(0);
return global_answer;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
3 ms |
5716 KB |
Output is correct |
3 |
Correct |
3 ms |
5716 KB |
Output is correct |
4 |
Correct |
3 ms |
5716 KB |
Output is correct |
5 |
Correct |
3 ms |
5716 KB |
Output is correct |
6 |
Correct |
3 ms |
5764 KB |
Output is correct |
7 |
Correct |
4 ms |
5716 KB |
Output is correct |
8 |
Correct |
3 ms |
5716 KB |
Output is correct |
9 |
Correct |
4 ms |
5716 KB |
Output is correct |
10 |
Correct |
3 ms |
5716 KB |
Output is correct |
11 |
Correct |
4 ms |
5716 KB |
Output is correct |
12 |
Correct |
3 ms |
5716 KB |
Output is correct |
13 |
Correct |
3 ms |
5716 KB |
Output is correct |
14 |
Correct |
3 ms |
5716 KB |
Output is correct |
15 |
Correct |
2 ms |
5716 KB |
Output is correct |
16 |
Correct |
3 ms |
5716 KB |
Output is correct |
17 |
Correct |
3 ms |
5716 KB |
Output is correct |
18 |
Correct |
3 ms |
5716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
3 ms |
5716 KB |
Output is correct |
3 |
Correct |
3 ms |
5716 KB |
Output is correct |
4 |
Correct |
3 ms |
5716 KB |
Output is correct |
5 |
Correct |
3 ms |
5716 KB |
Output is correct |
6 |
Correct |
3 ms |
5764 KB |
Output is correct |
7 |
Correct |
4 ms |
5716 KB |
Output is correct |
8 |
Correct |
3 ms |
5716 KB |
Output is correct |
9 |
Correct |
4 ms |
5716 KB |
Output is correct |
10 |
Correct |
3 ms |
5716 KB |
Output is correct |
11 |
Correct |
4 ms |
5716 KB |
Output is correct |
12 |
Correct |
3 ms |
5716 KB |
Output is correct |
13 |
Correct |
3 ms |
5716 KB |
Output is correct |
14 |
Correct |
3 ms |
5716 KB |
Output is correct |
15 |
Correct |
2 ms |
5716 KB |
Output is correct |
16 |
Correct |
3 ms |
5716 KB |
Output is correct |
17 |
Correct |
3 ms |
5716 KB |
Output is correct |
18 |
Correct |
3 ms |
5716 KB |
Output is correct |
19 |
Correct |
3 ms |
5716 KB |
Output is correct |
20 |
Correct |
3 ms |
5716 KB |
Output is correct |
21 |
Correct |
4 ms |
5844 KB |
Output is correct |
22 |
Correct |
6 ms |
10836 KB |
Output is correct |
23 |
Correct |
6 ms |
10188 KB |
Output is correct |
24 |
Correct |
7 ms |
11604 KB |
Output is correct |
25 |
Correct |
6 ms |
9272 KB |
Output is correct |
26 |
Correct |
5 ms |
8404 KB |
Output is correct |
27 |
Correct |
3 ms |
5844 KB |
Output is correct |
28 |
Correct |
5 ms |
7380 KB |
Output is correct |
29 |
Correct |
5 ms |
8404 KB |
Output is correct |
30 |
Correct |
5 ms |
8660 KB |
Output is correct |
31 |
Correct |
5 ms |
9428 KB |
Output is correct |
32 |
Correct |
6 ms |
9556 KB |
Output is correct |
33 |
Correct |
6 ms |
11092 KB |
Output is correct |
34 |
Correct |
6 ms |
10272 KB |
Output is correct |
35 |
Correct |
6 ms |
11732 KB |
Output is correct |
36 |
Correct |
7 ms |
12244 KB |
Output is correct |
37 |
Correct |
5 ms |
9684 KB |
Output is correct |
38 |
Correct |
5 ms |
8148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
3 ms |
5716 KB |
Output is correct |
3 |
Correct |
3 ms |
5716 KB |
Output is correct |
4 |
Correct |
3 ms |
5716 KB |
Output is correct |
5 |
Correct |
3 ms |
5716 KB |
Output is correct |
6 |
Correct |
3 ms |
5764 KB |
Output is correct |
7 |
Correct |
4 ms |
5716 KB |
Output is correct |
8 |
Correct |
3 ms |
5716 KB |
Output is correct |
9 |
Correct |
4 ms |
5716 KB |
Output is correct |
10 |
Correct |
3 ms |
5716 KB |
Output is correct |
11 |
Correct |
4 ms |
5716 KB |
Output is correct |
12 |
Correct |
3 ms |
5716 KB |
Output is correct |
13 |
Correct |
3 ms |
5716 KB |
Output is correct |
14 |
Correct |
3 ms |
5716 KB |
Output is correct |
15 |
Correct |
2 ms |
5716 KB |
Output is correct |
16 |
Correct |
3 ms |
5716 KB |
Output is correct |
17 |
Correct |
3 ms |
5716 KB |
Output is correct |
18 |
Correct |
3 ms |
5716 KB |
Output is correct |
19 |
Correct |
145 ms |
11080 KB |
Output is correct |
20 |
Correct |
156 ms |
11024 KB |
Output is correct |
21 |
Correct |
131 ms |
11056 KB |
Output is correct |
22 |
Correct |
153 ms |
11220 KB |
Output is correct |
23 |
Correct |
92 ms |
11220 KB |
Output is correct |
24 |
Correct |
54 ms |
11164 KB |
Output is correct |
25 |
Correct |
122 ms |
13792 KB |
Output is correct |
26 |
Correct |
89 ms |
16760 KB |
Output is correct |
27 |
Correct |
171 ms |
16408 KB |
Output is correct |
28 |
Correct |
324 ms |
27640 KB |
Output is correct |
29 |
Correct |
300 ms |
26700 KB |
Output is correct |
30 |
Correct |
155 ms |
16512 KB |
Output is correct |
31 |
Correct |
156 ms |
16448 KB |
Output is correct |
32 |
Correct |
220 ms |
16560 KB |
Output is correct |
33 |
Correct |
223 ms |
15392 KB |
Output is correct |
34 |
Correct |
221 ms |
15180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5716 KB |
Output is correct |
2 |
Correct |
3 ms |
5716 KB |
Output is correct |
3 |
Correct |
3 ms |
5716 KB |
Output is correct |
4 |
Correct |
3 ms |
5716 KB |
Output is correct |
5 |
Correct |
3 ms |
5716 KB |
Output is correct |
6 |
Correct |
3 ms |
5764 KB |
Output is correct |
7 |
Correct |
4 ms |
5716 KB |
Output is correct |
8 |
Correct |
3 ms |
5716 KB |
Output is correct |
9 |
Correct |
4 ms |
5716 KB |
Output is correct |
10 |
Correct |
3 ms |
5716 KB |
Output is correct |
11 |
Correct |
4 ms |
5716 KB |
Output is correct |
12 |
Correct |
3 ms |
5716 KB |
Output is correct |
13 |
Correct |
3 ms |
5716 KB |
Output is correct |
14 |
Correct |
3 ms |
5716 KB |
Output is correct |
15 |
Correct |
2 ms |
5716 KB |
Output is correct |
16 |
Correct |
3 ms |
5716 KB |
Output is correct |
17 |
Correct |
3 ms |
5716 KB |
Output is correct |
18 |
Correct |
3 ms |
5716 KB |
Output is correct |
19 |
Correct |
3 ms |
5716 KB |
Output is correct |
20 |
Correct |
3 ms |
5716 KB |
Output is correct |
21 |
Correct |
4 ms |
5844 KB |
Output is correct |
22 |
Correct |
6 ms |
10836 KB |
Output is correct |
23 |
Correct |
6 ms |
10188 KB |
Output is correct |
24 |
Correct |
7 ms |
11604 KB |
Output is correct |
25 |
Correct |
6 ms |
9272 KB |
Output is correct |
26 |
Correct |
5 ms |
8404 KB |
Output is correct |
27 |
Correct |
3 ms |
5844 KB |
Output is correct |
28 |
Correct |
5 ms |
7380 KB |
Output is correct |
29 |
Correct |
5 ms |
8404 KB |
Output is correct |
30 |
Correct |
5 ms |
8660 KB |
Output is correct |
31 |
Correct |
5 ms |
9428 KB |
Output is correct |
32 |
Correct |
6 ms |
9556 KB |
Output is correct |
33 |
Correct |
6 ms |
11092 KB |
Output is correct |
34 |
Correct |
6 ms |
10272 KB |
Output is correct |
35 |
Correct |
6 ms |
11732 KB |
Output is correct |
36 |
Correct |
7 ms |
12244 KB |
Output is correct |
37 |
Correct |
5 ms |
9684 KB |
Output is correct |
38 |
Correct |
5 ms |
8148 KB |
Output is correct |
39 |
Correct |
145 ms |
11080 KB |
Output is correct |
40 |
Correct |
156 ms |
11024 KB |
Output is correct |
41 |
Correct |
131 ms |
11056 KB |
Output is correct |
42 |
Correct |
153 ms |
11220 KB |
Output is correct |
43 |
Correct |
92 ms |
11220 KB |
Output is correct |
44 |
Correct |
54 ms |
11164 KB |
Output is correct |
45 |
Correct |
122 ms |
13792 KB |
Output is correct |
46 |
Correct |
89 ms |
16760 KB |
Output is correct |
47 |
Correct |
171 ms |
16408 KB |
Output is correct |
48 |
Correct |
324 ms |
27640 KB |
Output is correct |
49 |
Correct |
300 ms |
26700 KB |
Output is correct |
50 |
Correct |
155 ms |
16512 KB |
Output is correct |
51 |
Correct |
156 ms |
16448 KB |
Output is correct |
52 |
Correct |
220 ms |
16560 KB |
Output is correct |
53 |
Correct |
223 ms |
15392 KB |
Output is correct |
54 |
Correct |
221 ms |
15180 KB |
Output is correct |
55 |
Correct |
14 ms |
6384 KB |
Output is correct |
56 |
Correct |
11 ms |
6340 KB |
Output is correct |
57 |
Correct |
73 ms |
11288 KB |
Output is correct |
58 |
Correct |
30 ms |
11192 KB |
Output is correct |
59 |
Correct |
103 ms |
17536 KB |
Output is correct |
60 |
Correct |
480 ms |
31716 KB |
Output is correct |
61 |
Correct |
170 ms |
16512 KB |
Output is correct |
62 |
Correct |
189 ms |
16516 KB |
Output is correct |
63 |
Correct |
262 ms |
16456 KB |
Output is correct |
64 |
Correct |
589 ms |
18564 KB |
Output is correct |
65 |
Correct |
292 ms |
16540 KB |
Output is correct |
66 |
Correct |
398 ms |
32452 KB |
Output is correct |
67 |
Correct |
100 ms |
16728 KB |
Output is correct |
68 |
Correct |
250 ms |
21876 KB |
Output is correct |
69 |
Correct |
259 ms |
22092 KB |
Output is correct |
70 |
Correct |
242 ms |
21452 KB |
Output is correct |