#include "race.h"
#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define forn(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define sz(a) (int)a.size()
const int N = 2e5 + 10;
vector<pair<int, ll>> adj[N];
bool vis[N];
int s[N];
int K;
map<ll, int> dp;
void recalc(int u, int par){
s[u] = 1;
for(auto x: adj[u]){
int v = x.first;
if(v == par || vis[v])continue;
recalc(v, u);
s[u] += s[v];
}
}
int find_centroid(int u, int need, int par){
for(auto x: adj[u]){
int v = x.first;
if(v == par || vis[v])continue;
if(s[v] > need)return find_centroid(v, need, u);
}
return u;
}
int ans = INT_MAX;
void dfs(int u, int par, ll weight, int type, int depth){
if(weight > K)return;
if(type == 0){
if(dp.count(K - weight)){
ans = min(ans, dp[K - weight] + depth);
}
}
else{
if(!dp.count(weight))dp[weight] = depth;
else dp[weight] = min(dp[weight], depth);
}
for(auto x: adj[u]){
ll v = x.first, w = x.second;
if(v == par || vis[v])continue;
dfs(v, u, weight + w, type, depth + 1);
}
}
void centroid_decomp(int u){
recalc(u, -1);
int c = find_centroid(u, s[u] / 2, -1);
vis[c] = true;
dp.clear();
dp[0] = 0;
for(auto x: adj[c]){
ll v = x.first, w = x.second;
if(vis[v])continue;
dfs(v, -1, w, 0, 1);
dfs(v, -1, w, 1, 1);
}
for(auto x: adj[c]){
int v = x.first;
if(vis[v])continue;
centroid_decomp(v);
}
}
int best_path(int n, int k, int h[][2],int l[]){
ans = INT_MAX;
K = k;
for(int i = 0;i < n; ++i){
adj[i].clear();
vis[i] = 0;
s[i] = 0;
}
dp.clear();
for(int i = 0;i < n - 1; ++i){
adj[h[i][0]].pb({h[i][1], l[i]});
adj[h[i][1]].pb({h[i][0], l[i]});
}
centroid_decomp(0);
if(ans == INT_MAX)return -1;
return ans;
}
/*
void solve()
{
int n, k;
cin >> n >> k;
K = k;
vector<vector<int>> h(n - 1, vector<int>(2, 0));
vector<int> l(n - 1);
for(int i = 0;i < n - 1; ++i){
cin >> h[i][0] >> h[i][1];
}
for(int i = 0;i < n - 1; ++i){
cin >> l[i];
}
cout << best_path(n, k, h, l) << "\n";
}
int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t = 1;
//cin >> t;
while(t--)
{
solve();
}
}
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4952 KB |
Output is correct |
7 |
Correct |
2 ms |
4940 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
2 ms |
4940 KB |
Output is correct |
10 |
Correct |
2 ms |
4940 KB |
Output is correct |
11 |
Correct |
2 ms |
4940 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
2 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
2 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4952 KB |
Output is correct |
7 |
Correct |
2 ms |
4940 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
2 ms |
4940 KB |
Output is correct |
10 |
Correct |
2 ms |
4940 KB |
Output is correct |
11 |
Correct |
2 ms |
4940 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
2 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
2 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
2 ms |
4940 KB |
Output is correct |
20 |
Correct |
2 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
3 ms |
5068 KB |
Output is correct |
24 |
Correct |
3 ms |
5068 KB |
Output is correct |
25 |
Correct |
4 ms |
5068 KB |
Output is correct |
26 |
Correct |
3 ms |
5068 KB |
Output is correct |
27 |
Correct |
3 ms |
5068 KB |
Output is correct |
28 |
Correct |
3 ms |
5068 KB |
Output is correct |
29 |
Correct |
3 ms |
5068 KB |
Output is correct |
30 |
Correct |
4 ms |
5020 KB |
Output is correct |
31 |
Correct |
4 ms |
5068 KB |
Output is correct |
32 |
Correct |
4 ms |
5068 KB |
Output is correct |
33 |
Correct |
4 ms |
5068 KB |
Output is correct |
34 |
Correct |
3 ms |
5068 KB |
Output is correct |
35 |
Correct |
3 ms |
5096 KB |
Output is correct |
36 |
Correct |
3 ms |
5068 KB |
Output is correct |
37 |
Correct |
4 ms |
5068 KB |
Output is correct |
38 |
Correct |
4 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4952 KB |
Output is correct |
7 |
Correct |
2 ms |
4940 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
2 ms |
4940 KB |
Output is correct |
10 |
Correct |
2 ms |
4940 KB |
Output is correct |
11 |
Correct |
2 ms |
4940 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
2 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
2 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
187 ms |
11756 KB |
Output is correct |
20 |
Correct |
189 ms |
11756 KB |
Output is correct |
21 |
Correct |
200 ms |
11776 KB |
Output is correct |
22 |
Correct |
199 ms |
11764 KB |
Output is correct |
23 |
Correct |
80 ms |
12140 KB |
Output is correct |
24 |
Correct |
72 ms |
11228 KB |
Output is correct |
25 |
Correct |
129 ms |
15476 KB |
Output is correct |
26 |
Correct |
96 ms |
19172 KB |
Output is correct |
27 |
Correct |
210 ms |
19044 KB |
Output is correct |
28 |
Correct |
270 ms |
33160 KB |
Output is correct |
29 |
Correct |
262 ms |
32068 KB |
Output is correct |
30 |
Correct |
200 ms |
19704 KB |
Output is correct |
31 |
Correct |
197 ms |
19896 KB |
Output is correct |
32 |
Correct |
232 ms |
19720 KB |
Output is correct |
33 |
Correct |
273 ms |
18596 KB |
Output is correct |
34 |
Correct |
176 ms |
18452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
2 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
2 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4952 KB |
Output is correct |
7 |
Correct |
2 ms |
4940 KB |
Output is correct |
8 |
Correct |
2 ms |
4940 KB |
Output is correct |
9 |
Correct |
2 ms |
4940 KB |
Output is correct |
10 |
Correct |
2 ms |
4940 KB |
Output is correct |
11 |
Correct |
2 ms |
4940 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
2 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
2 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
2 ms |
4940 KB |
Output is correct |
20 |
Correct |
2 ms |
4940 KB |
Output is correct |
21 |
Correct |
4 ms |
5068 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
3 ms |
5068 KB |
Output is correct |
24 |
Correct |
3 ms |
5068 KB |
Output is correct |
25 |
Correct |
4 ms |
5068 KB |
Output is correct |
26 |
Correct |
3 ms |
5068 KB |
Output is correct |
27 |
Correct |
3 ms |
5068 KB |
Output is correct |
28 |
Correct |
3 ms |
5068 KB |
Output is correct |
29 |
Correct |
3 ms |
5068 KB |
Output is correct |
30 |
Correct |
4 ms |
5020 KB |
Output is correct |
31 |
Correct |
4 ms |
5068 KB |
Output is correct |
32 |
Correct |
4 ms |
5068 KB |
Output is correct |
33 |
Correct |
4 ms |
5068 KB |
Output is correct |
34 |
Correct |
3 ms |
5068 KB |
Output is correct |
35 |
Correct |
3 ms |
5096 KB |
Output is correct |
36 |
Correct |
3 ms |
5068 KB |
Output is correct |
37 |
Correct |
4 ms |
5068 KB |
Output is correct |
38 |
Correct |
4 ms |
5068 KB |
Output is correct |
39 |
Correct |
187 ms |
11756 KB |
Output is correct |
40 |
Correct |
189 ms |
11756 KB |
Output is correct |
41 |
Correct |
200 ms |
11776 KB |
Output is correct |
42 |
Correct |
199 ms |
11764 KB |
Output is correct |
43 |
Correct |
80 ms |
12140 KB |
Output is correct |
44 |
Correct |
72 ms |
11228 KB |
Output is correct |
45 |
Correct |
129 ms |
15476 KB |
Output is correct |
46 |
Correct |
96 ms |
19172 KB |
Output is correct |
47 |
Correct |
210 ms |
19044 KB |
Output is correct |
48 |
Correct |
270 ms |
33160 KB |
Output is correct |
49 |
Correct |
262 ms |
32068 KB |
Output is correct |
50 |
Correct |
200 ms |
19704 KB |
Output is correct |
51 |
Correct |
197 ms |
19896 KB |
Output is correct |
52 |
Correct |
232 ms |
19720 KB |
Output is correct |
53 |
Correct |
273 ms |
18596 KB |
Output is correct |
54 |
Correct |
176 ms |
18452 KB |
Output is correct |
55 |
Correct |
19 ms |
5996 KB |
Output is correct |
56 |
Correct |
15 ms |
5728 KB |
Output is correct |
57 |
Correct |
131 ms |
12796 KB |
Output is correct |
58 |
Correct |
43 ms |
11972 KB |
Output is correct |
59 |
Correct |
273 ms |
24524 KB |
Output is correct |
60 |
Correct |
866 ms |
44788 KB |
Output is correct |
61 |
Correct |
278 ms |
20000 KB |
Output is correct |
62 |
Correct |
281 ms |
19880 KB |
Output is correct |
63 |
Correct |
359 ms |
20000 KB |
Output is correct |
64 |
Correct |
1026 ms |
25288 KB |
Output is correct |
65 |
Correct |
219 ms |
19168 KB |
Output is correct |
66 |
Correct |
552 ms |
30392 KB |
Output is correct |
67 |
Correct |
124 ms |
18464 KB |
Output is correct |
68 |
Correct |
392 ms |
24004 KB |
Output is correct |
69 |
Correct |
386 ms |
23660 KB |
Output is correct |
70 |
Correct |
368 ms |
23332 KB |
Output is correct |