#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define A first
#define B second
#define MP make_pair
#define ms(a, x) memset(a, x, sizeof(a))
#pragma GCC optimize ("Ofast")
//#pragma GCC optimize ("trapv")
#define boost() ios_base::sync_with_stdio(false); cin.tie(0)
typedef __int128_t vl;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<long double, long double> pld;
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
const int mxN=2e5+5, LOG=21;
vector <vector<pii>> adj(mxN);
int sz[mxN], cnt[int(1e6)+5], ans=INF;
bool mark[mxN];
void dfs_sz(int u, int p){
sz[u]=1;
for(auto [v, x]:adj[u]){
if(v == p || mark[v]) continue;
dfs_sz(v, u);
sz[u] += sz[v];
}
return;
}
int dfs_cntrd(int u, int p, int subsz){
for(auto [v, x]:adj[u]){
if(v == p || mark[v]) continue;
if(sz[v] > subsz/2) return dfs_cntrd(v, u, subsz);
}
return u;
}
void dfs_cnt(int u, int x, int d, int p, int mode, int k){
if(x > k) return;
(mode ? (mode == 1 ? ans = min(ans, d+cnt[k-x]):cnt[x]=INF):cnt[x] = min(cnt[x], d));
for(auto [v, dist]:adj[u]){
if(mark[v] || v == p) continue;
dfs_cnt(v, dist+x, d+1, u, mode, k);
}
}
void decompose(int u, int subsz, int k){
dfs_sz(u, -1);
int cntrd = dfs_cntrd(u, -1, subsz);
mark[cntrd]=1;
for(auto [v, x]:adj[cntrd]){
if(mark[v]) continue;
dfs_cnt(v, x, 1, -1, 1, k);
dfs_cnt(v, x, 1, -1, 0, k);
}
ans = min(ans, cnt[k]);
for(auto [v, x]:adj[cntrd]){
if(mark[v]) continue;
dfs_cnt(v, x, 1, -1, 2, k);
}
for(auto [v, x]:adj[cntrd]){
if(mark[v]) continue;
decompose(v, sz[v], k);
}
return;
}
int best_path(int n, int k, int h[mxN][2], int l[mxN]){
ms(cnt, INF);
for(int i=0; i<n-1; i++){
adj[h[i][0]+1].push_back({h[i][1]+1, l[i]});
adj[h[i][1]+1].push_back({h[i][0]+1, l[i]});
}
decompose(1, n, k);
if(ans == INF) return -1;
else return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8916 KB |
Output is correct |
3 |
Correct |
5 ms |
8916 KB |
Output is correct |
4 |
Correct |
5 ms |
8916 KB |
Output is correct |
5 |
Correct |
5 ms |
8860 KB |
Output is correct |
6 |
Correct |
5 ms |
8916 KB |
Output is correct |
7 |
Correct |
5 ms |
8916 KB |
Output is correct |
8 |
Correct |
6 ms |
8916 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
5 ms |
8916 KB |
Output is correct |
11 |
Correct |
5 ms |
8872 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
5 ms |
8916 KB |
Output is correct |
14 |
Correct |
5 ms |
8916 KB |
Output is correct |
15 |
Correct |
4 ms |
8916 KB |
Output is correct |
16 |
Correct |
5 ms |
8916 KB |
Output is correct |
17 |
Correct |
5 ms |
8916 KB |
Output is correct |
18 |
Correct |
4 ms |
8864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8916 KB |
Output is correct |
3 |
Correct |
5 ms |
8916 KB |
Output is correct |
4 |
Correct |
5 ms |
8916 KB |
Output is correct |
5 |
Correct |
5 ms |
8860 KB |
Output is correct |
6 |
Correct |
5 ms |
8916 KB |
Output is correct |
7 |
Correct |
5 ms |
8916 KB |
Output is correct |
8 |
Correct |
6 ms |
8916 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
5 ms |
8916 KB |
Output is correct |
11 |
Correct |
5 ms |
8872 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
5 ms |
8916 KB |
Output is correct |
14 |
Correct |
5 ms |
8916 KB |
Output is correct |
15 |
Correct |
4 ms |
8916 KB |
Output is correct |
16 |
Correct |
5 ms |
8916 KB |
Output is correct |
17 |
Correct |
5 ms |
8916 KB |
Output is correct |
18 |
Correct |
4 ms |
8864 KB |
Output is correct |
19 |
Correct |
6 ms |
8856 KB |
Output is correct |
20 |
Correct |
5 ms |
8916 KB |
Output is correct |
21 |
Correct |
5 ms |
8992 KB |
Output is correct |
22 |
Correct |
6 ms |
8916 KB |
Output is correct |
23 |
Correct |
6 ms |
9000 KB |
Output is correct |
24 |
Correct |
6 ms |
8916 KB |
Output is correct |
25 |
Correct |
5 ms |
8996 KB |
Output is correct |
26 |
Correct |
6 ms |
8916 KB |
Output is correct |
27 |
Correct |
5 ms |
8968 KB |
Output is correct |
28 |
Correct |
7 ms |
8916 KB |
Output is correct |
29 |
Correct |
7 ms |
8916 KB |
Output is correct |
30 |
Correct |
5 ms |
8968 KB |
Output is correct |
31 |
Correct |
5 ms |
8916 KB |
Output is correct |
32 |
Correct |
6 ms |
9004 KB |
Output is correct |
33 |
Correct |
6 ms |
8916 KB |
Output is correct |
34 |
Correct |
6 ms |
8916 KB |
Output is correct |
35 |
Correct |
5 ms |
8916 KB |
Output is correct |
36 |
Correct |
6 ms |
8916 KB |
Output is correct |
37 |
Correct |
5 ms |
8996 KB |
Output is correct |
38 |
Correct |
6 ms |
8916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8916 KB |
Output is correct |
3 |
Correct |
5 ms |
8916 KB |
Output is correct |
4 |
Correct |
5 ms |
8916 KB |
Output is correct |
5 |
Correct |
5 ms |
8860 KB |
Output is correct |
6 |
Correct |
5 ms |
8916 KB |
Output is correct |
7 |
Correct |
5 ms |
8916 KB |
Output is correct |
8 |
Correct |
6 ms |
8916 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
5 ms |
8916 KB |
Output is correct |
11 |
Correct |
5 ms |
8872 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
5 ms |
8916 KB |
Output is correct |
14 |
Correct |
5 ms |
8916 KB |
Output is correct |
15 |
Correct |
4 ms |
8916 KB |
Output is correct |
16 |
Correct |
5 ms |
8916 KB |
Output is correct |
17 |
Correct |
5 ms |
8916 KB |
Output is correct |
18 |
Correct |
4 ms |
8864 KB |
Output is correct |
19 |
Correct |
150 ms |
15664 KB |
Output is correct |
20 |
Correct |
147 ms |
15508 KB |
Output is correct |
21 |
Correct |
154 ms |
15628 KB |
Output is correct |
22 |
Correct |
160 ms |
15716 KB |
Output is correct |
23 |
Correct |
79 ms |
15820 KB |
Output is correct |
24 |
Correct |
56 ms |
15724 KB |
Output is correct |
25 |
Correct |
134 ms |
16624 KB |
Output is correct |
26 |
Correct |
109 ms |
18124 KB |
Output is correct |
27 |
Correct |
194 ms |
22824 KB |
Output is correct |
28 |
Correct |
260 ms |
27796 KB |
Output is correct |
29 |
Correct |
248 ms |
27340 KB |
Output is correct |
30 |
Correct |
175 ms |
22732 KB |
Output is correct |
31 |
Correct |
176 ms |
22680 KB |
Output is correct |
32 |
Correct |
205 ms |
22780 KB |
Output is correct |
33 |
Correct |
227 ms |
21456 KB |
Output is correct |
34 |
Correct |
203 ms |
22352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8916 KB |
Output is correct |
2 |
Correct |
5 ms |
8916 KB |
Output is correct |
3 |
Correct |
5 ms |
8916 KB |
Output is correct |
4 |
Correct |
5 ms |
8916 KB |
Output is correct |
5 |
Correct |
5 ms |
8860 KB |
Output is correct |
6 |
Correct |
5 ms |
8916 KB |
Output is correct |
7 |
Correct |
5 ms |
8916 KB |
Output is correct |
8 |
Correct |
6 ms |
8916 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
5 ms |
8916 KB |
Output is correct |
11 |
Correct |
5 ms |
8872 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
5 ms |
8916 KB |
Output is correct |
14 |
Correct |
5 ms |
8916 KB |
Output is correct |
15 |
Correct |
4 ms |
8916 KB |
Output is correct |
16 |
Correct |
5 ms |
8916 KB |
Output is correct |
17 |
Correct |
5 ms |
8916 KB |
Output is correct |
18 |
Correct |
4 ms |
8864 KB |
Output is correct |
19 |
Correct |
6 ms |
8856 KB |
Output is correct |
20 |
Correct |
5 ms |
8916 KB |
Output is correct |
21 |
Correct |
5 ms |
8992 KB |
Output is correct |
22 |
Correct |
6 ms |
8916 KB |
Output is correct |
23 |
Correct |
6 ms |
9000 KB |
Output is correct |
24 |
Correct |
6 ms |
8916 KB |
Output is correct |
25 |
Correct |
5 ms |
8996 KB |
Output is correct |
26 |
Correct |
6 ms |
8916 KB |
Output is correct |
27 |
Correct |
5 ms |
8968 KB |
Output is correct |
28 |
Correct |
7 ms |
8916 KB |
Output is correct |
29 |
Correct |
7 ms |
8916 KB |
Output is correct |
30 |
Correct |
5 ms |
8968 KB |
Output is correct |
31 |
Correct |
5 ms |
8916 KB |
Output is correct |
32 |
Correct |
6 ms |
9004 KB |
Output is correct |
33 |
Correct |
6 ms |
8916 KB |
Output is correct |
34 |
Correct |
6 ms |
8916 KB |
Output is correct |
35 |
Correct |
5 ms |
8916 KB |
Output is correct |
36 |
Correct |
6 ms |
8916 KB |
Output is correct |
37 |
Correct |
5 ms |
8996 KB |
Output is correct |
38 |
Correct |
6 ms |
8916 KB |
Output is correct |
39 |
Correct |
150 ms |
15664 KB |
Output is correct |
40 |
Correct |
147 ms |
15508 KB |
Output is correct |
41 |
Correct |
154 ms |
15628 KB |
Output is correct |
42 |
Correct |
160 ms |
15716 KB |
Output is correct |
43 |
Correct |
79 ms |
15820 KB |
Output is correct |
44 |
Correct |
56 ms |
15724 KB |
Output is correct |
45 |
Correct |
134 ms |
16624 KB |
Output is correct |
46 |
Correct |
109 ms |
18124 KB |
Output is correct |
47 |
Correct |
194 ms |
22824 KB |
Output is correct |
48 |
Correct |
260 ms |
27796 KB |
Output is correct |
49 |
Correct |
248 ms |
27340 KB |
Output is correct |
50 |
Correct |
175 ms |
22732 KB |
Output is correct |
51 |
Correct |
176 ms |
22680 KB |
Output is correct |
52 |
Correct |
205 ms |
22780 KB |
Output is correct |
53 |
Correct |
227 ms |
21456 KB |
Output is correct |
54 |
Correct |
203 ms |
22352 KB |
Output is correct |
55 |
Correct |
14 ms |
9508 KB |
Output is correct |
56 |
Correct |
20 ms |
9508 KB |
Output is correct |
57 |
Correct |
99 ms |
15776 KB |
Output is correct |
58 |
Correct |
38 ms |
15480 KB |
Output is correct |
59 |
Correct |
120 ms |
18372 KB |
Output is correct |
60 |
Correct |
488 ms |
27440 KB |
Output is correct |
61 |
Correct |
197 ms |
22816 KB |
Output is correct |
62 |
Correct |
218 ms |
22652 KB |
Output is correct |
63 |
Correct |
331 ms |
22768 KB |
Output is correct |
64 |
Correct |
613 ms |
23036 KB |
Output is correct |
65 |
Correct |
224 ms |
23532 KB |
Output is correct |
66 |
Correct |
381 ms |
26504 KB |
Output is correct |
67 |
Correct |
135 ms |
23332 KB |
Output is correct |
68 |
Correct |
311 ms |
23528 KB |
Output is correct |
69 |
Correct |
304 ms |
23672 KB |
Output is correct |
70 |
Correct |
305 ms |
22928 KB |
Output is correct |