#include "race.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 2e5;
const int MAXK = 1e6;
int N, K;
vector<pii> adj[MAXN+10];
bool del[MAXN+10];
int sz[MAXN+10], ans=987654321;
void getsz(int now, int bef)
{
sz[now]=1;
for(auto nxt : adj[now])
{
if(nxt.first==bef) continue;
if(del[nxt.first]) continue;
getsz(nxt.first, now);
sz[now]+=sz[nxt.first];
}
}
int getcen(int now, int bef, int tot)
{
for(auto nxt : adj[now])
{
if(nxt.first==bef) continue;
if(del[nxt.first]) continue;
if(sz[nxt.first]>tot/2) return getcen(nxt.first, now, tot);
}
return now;
}
int M[MAXK+10];
vector<int> V;
void dfs1(int now, int bef, ll dist, int road)
{
if(K-dist>=0) ans=min(ans, M[K-dist]+road);
for(auto nxt : adj[now])
{
if(nxt.first==bef) continue;
if(del[nxt.first]) continue;
dfs1(nxt.first, now, dist+nxt.second, road+1);
}
}
void dfs2(int now, int bef, ll dist, int road)
{
if(dist<=K) M[dist]=min(M[dist], road), V.push_back(dist);
for(auto nxt : adj[now])
{
if(nxt.first==bef) continue;
if(del[nxt.first]) continue;
dfs2(nxt.first, now, dist+nxt.second, road+1);
}
}
void decomp(int now)
{
getsz(now, now);
now=getcen(now, now, sz[now]);
del[now]=true;
M[0]=0; V.clear();
for(auto nxt : adj[now])
{
if(del[nxt.first]) continue;
dfs1(nxt.first, nxt.first, nxt.second, 1);
dfs2(nxt.first, nxt.first, nxt.second, 1);
}
for(auto it : V) M[it]=987654321;
for(auto nxt : adj[now])
{
if(del[nxt.first]) continue;
decomp(nxt.first);
}
}
int best_path(int _N, int _K, int H[][2], int L[])
{
int i, j;
N=_N; K=_K;
for(i=0; i<N-1; i++)
{
int u, v, w;
u=H[i][0]+1; v=H[i][1]+1; w=L[i];
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
for(i=0; i<=K; i++) M[i]=987654321;
decomp(1);
if(ans==987654321) ans=-1;
return ans;
}
Compilation message
race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:90:12: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
4984 KB |
Output is correct |
3 |
Correct |
7 ms |
5116 KB |
Output is correct |
4 |
Correct |
6 ms |
4984 KB |
Output is correct |
5 |
Correct |
6 ms |
4984 KB |
Output is correct |
6 |
Correct |
7 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
4984 KB |
Output is correct |
8 |
Correct |
6 ms |
4984 KB |
Output is correct |
9 |
Correct |
6 ms |
4984 KB |
Output is correct |
10 |
Correct |
7 ms |
5112 KB |
Output is correct |
11 |
Correct |
7 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
4984 KB |
Output is correct |
13 |
Correct |
6 ms |
4984 KB |
Output is correct |
14 |
Correct |
6 ms |
5112 KB |
Output is correct |
15 |
Correct |
6 ms |
4984 KB |
Output is correct |
16 |
Correct |
6 ms |
4984 KB |
Output is correct |
17 |
Correct |
6 ms |
5112 KB |
Output is correct |
18 |
Correct |
6 ms |
4984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
4984 KB |
Output is correct |
3 |
Correct |
7 ms |
5116 KB |
Output is correct |
4 |
Correct |
6 ms |
4984 KB |
Output is correct |
5 |
Correct |
6 ms |
4984 KB |
Output is correct |
6 |
Correct |
7 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
4984 KB |
Output is correct |
8 |
Correct |
6 ms |
4984 KB |
Output is correct |
9 |
Correct |
6 ms |
4984 KB |
Output is correct |
10 |
Correct |
7 ms |
5112 KB |
Output is correct |
11 |
Correct |
7 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
4984 KB |
Output is correct |
13 |
Correct |
6 ms |
4984 KB |
Output is correct |
14 |
Correct |
6 ms |
5112 KB |
Output is correct |
15 |
Correct |
6 ms |
4984 KB |
Output is correct |
16 |
Correct |
6 ms |
4984 KB |
Output is correct |
17 |
Correct |
6 ms |
5112 KB |
Output is correct |
18 |
Correct |
6 ms |
4984 KB |
Output is correct |
19 |
Correct |
6 ms |
5112 KB |
Output is correct |
20 |
Correct |
7 ms |
5112 KB |
Output is correct |
21 |
Correct |
7 ms |
5112 KB |
Output is correct |
22 |
Correct |
10 ms |
8696 KB |
Output is correct |
23 |
Correct |
10 ms |
8056 KB |
Output is correct |
24 |
Correct |
10 ms |
8440 KB |
Output is correct |
25 |
Correct |
10 ms |
8440 KB |
Output is correct |
26 |
Correct |
25 ms |
6392 KB |
Output is correct |
27 |
Correct |
10 ms |
8184 KB |
Output is correct |
28 |
Correct |
8 ms |
5880 KB |
Output is correct |
29 |
Correct |
9 ms |
6392 KB |
Output is correct |
30 |
Correct |
9 ms |
6520 KB |
Output is correct |
31 |
Correct |
9 ms |
7644 KB |
Output is correct |
32 |
Correct |
10 ms |
8028 KB |
Output is correct |
33 |
Correct |
22 ms |
8208 KB |
Output is correct |
34 |
Correct |
10 ms |
7416 KB |
Output is correct |
35 |
Correct |
10 ms |
8312 KB |
Output is correct |
36 |
Correct |
11 ms |
8824 KB |
Output is correct |
37 |
Correct |
10 ms |
8188 KB |
Output is correct |
38 |
Correct |
9 ms |
7160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
4984 KB |
Output is correct |
3 |
Correct |
7 ms |
5116 KB |
Output is correct |
4 |
Correct |
6 ms |
4984 KB |
Output is correct |
5 |
Correct |
6 ms |
4984 KB |
Output is correct |
6 |
Correct |
7 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
4984 KB |
Output is correct |
8 |
Correct |
6 ms |
4984 KB |
Output is correct |
9 |
Correct |
6 ms |
4984 KB |
Output is correct |
10 |
Correct |
7 ms |
5112 KB |
Output is correct |
11 |
Correct |
7 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
4984 KB |
Output is correct |
13 |
Correct |
6 ms |
4984 KB |
Output is correct |
14 |
Correct |
6 ms |
5112 KB |
Output is correct |
15 |
Correct |
6 ms |
4984 KB |
Output is correct |
16 |
Correct |
6 ms |
4984 KB |
Output is correct |
17 |
Correct |
6 ms |
5112 KB |
Output is correct |
18 |
Correct |
6 ms |
4984 KB |
Output is correct |
19 |
Correct |
189 ms |
10488 KB |
Output is correct |
20 |
Correct |
198 ms |
10504 KB |
Output is correct |
21 |
Correct |
184 ms |
10744 KB |
Output is correct |
22 |
Correct |
170 ms |
11144 KB |
Output is correct |
23 |
Correct |
149 ms |
10668 KB |
Output is correct |
24 |
Correct |
93 ms |
10460 KB |
Output is correct |
25 |
Correct |
230 ms |
14712 KB |
Output is correct |
26 |
Correct |
130 ms |
18040 KB |
Output is correct |
27 |
Correct |
261 ms |
19016 KB |
Output is correct |
28 |
Correct |
602 ms |
30200 KB |
Output is correct |
29 |
Correct |
674 ms |
29432 KB |
Output is correct |
30 |
Correct |
272 ms |
18880 KB |
Output is correct |
31 |
Correct |
269 ms |
19052 KB |
Output is correct |
32 |
Correct |
349 ms |
19064 KB |
Output is correct |
33 |
Correct |
479 ms |
17816 KB |
Output is correct |
34 |
Correct |
501 ms |
18680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5112 KB |
Output is correct |
2 |
Correct |
6 ms |
4984 KB |
Output is correct |
3 |
Correct |
7 ms |
5116 KB |
Output is correct |
4 |
Correct |
6 ms |
4984 KB |
Output is correct |
5 |
Correct |
6 ms |
4984 KB |
Output is correct |
6 |
Correct |
7 ms |
5112 KB |
Output is correct |
7 |
Correct |
6 ms |
4984 KB |
Output is correct |
8 |
Correct |
6 ms |
4984 KB |
Output is correct |
9 |
Correct |
6 ms |
4984 KB |
Output is correct |
10 |
Correct |
7 ms |
5112 KB |
Output is correct |
11 |
Correct |
7 ms |
5112 KB |
Output is correct |
12 |
Correct |
6 ms |
4984 KB |
Output is correct |
13 |
Correct |
6 ms |
4984 KB |
Output is correct |
14 |
Correct |
6 ms |
5112 KB |
Output is correct |
15 |
Correct |
6 ms |
4984 KB |
Output is correct |
16 |
Correct |
6 ms |
4984 KB |
Output is correct |
17 |
Correct |
6 ms |
5112 KB |
Output is correct |
18 |
Correct |
6 ms |
4984 KB |
Output is correct |
19 |
Correct |
6 ms |
5112 KB |
Output is correct |
20 |
Correct |
7 ms |
5112 KB |
Output is correct |
21 |
Correct |
7 ms |
5112 KB |
Output is correct |
22 |
Correct |
10 ms |
8696 KB |
Output is correct |
23 |
Correct |
10 ms |
8056 KB |
Output is correct |
24 |
Correct |
10 ms |
8440 KB |
Output is correct |
25 |
Correct |
10 ms |
8440 KB |
Output is correct |
26 |
Correct |
25 ms |
6392 KB |
Output is correct |
27 |
Correct |
10 ms |
8184 KB |
Output is correct |
28 |
Correct |
8 ms |
5880 KB |
Output is correct |
29 |
Correct |
9 ms |
6392 KB |
Output is correct |
30 |
Correct |
9 ms |
6520 KB |
Output is correct |
31 |
Correct |
9 ms |
7644 KB |
Output is correct |
32 |
Correct |
10 ms |
8028 KB |
Output is correct |
33 |
Correct |
22 ms |
8208 KB |
Output is correct |
34 |
Correct |
10 ms |
7416 KB |
Output is correct |
35 |
Correct |
10 ms |
8312 KB |
Output is correct |
36 |
Correct |
11 ms |
8824 KB |
Output is correct |
37 |
Correct |
10 ms |
8188 KB |
Output is correct |
38 |
Correct |
9 ms |
7160 KB |
Output is correct |
39 |
Correct |
189 ms |
10488 KB |
Output is correct |
40 |
Correct |
198 ms |
10504 KB |
Output is correct |
41 |
Correct |
184 ms |
10744 KB |
Output is correct |
42 |
Correct |
170 ms |
11144 KB |
Output is correct |
43 |
Correct |
149 ms |
10668 KB |
Output is correct |
44 |
Correct |
93 ms |
10460 KB |
Output is correct |
45 |
Correct |
230 ms |
14712 KB |
Output is correct |
46 |
Correct |
130 ms |
18040 KB |
Output is correct |
47 |
Correct |
261 ms |
19016 KB |
Output is correct |
48 |
Correct |
602 ms |
30200 KB |
Output is correct |
49 |
Correct |
674 ms |
29432 KB |
Output is correct |
50 |
Correct |
272 ms |
18880 KB |
Output is correct |
51 |
Correct |
269 ms |
19052 KB |
Output is correct |
52 |
Correct |
349 ms |
19064 KB |
Output is correct |
53 |
Correct |
479 ms |
17816 KB |
Output is correct |
54 |
Correct |
501 ms |
18680 KB |
Output is correct |
55 |
Correct |
19 ms |
5880 KB |
Output is correct |
56 |
Correct |
18 ms |
5752 KB |
Output is correct |
57 |
Correct |
95 ms |
12508 KB |
Output is correct |
58 |
Correct |
48 ms |
12652 KB |
Output is correct |
59 |
Correct |
129 ms |
18680 KB |
Output is correct |
60 |
Correct |
706 ms |
34328 KB |
Output is correct |
61 |
Correct |
253 ms |
19324 KB |
Output is correct |
62 |
Correct |
273 ms |
23820 KB |
Output is correct |
63 |
Correct |
341 ms |
23852 KB |
Output is correct |
64 |
Correct |
608 ms |
22324 KB |
Output is correct |
65 |
Correct |
423 ms |
19844 KB |
Output is correct |
66 |
Correct |
692 ms |
31448 KB |
Output is correct |
67 |
Correct |
148 ms |
25444 KB |
Output is correct |
68 |
Correct |
363 ms |
24040 KB |
Output is correct |
69 |
Correct |
385 ms |
24184 KB |
Output is correct |
70 |
Correct |
344 ms |
23388 KB |
Output is correct |