#include <bits/stdc++.h>
using namespace std;
#undef _GLIBCXX_DEBUG // disable run-time bound checking, etc
#pragma GCC optimize("Ofast,inline") // Ofast = O3,fast-math,allow-store-data-races,no-protect-parens
#pragma GCC optimize ("unroll-loops")
// #pragma GCC target("bmi,bmi2,lzcnt,popcnt") // bit manipulation
// #pragma GCC target("movbe") // byte swap
// #pragma GCC target("aes,pclmul,rdrnd") // encryption
// #pragma GCC target("avx,avx2,f16c,fma,sse3,ssse3,sse4.1,sse4.2") // SIMD
// #include <bits/extc++.h>
// using namespace __gnu_pbds;
// template<class T>using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
// template<class T>using ordered_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define ll long long
#define INF (ll)1e16+7
#define fo(i, n) for(ll i=0;i<((ll)n);i++)
#define deb(x) cout << #x << " = " << (x) << endl;
#define deb2(x, y) cout << #x << " = " << (x) << ", " << #y << " = " << (y) << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define LSOne(S) ((S) & (-S))
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
inline int readint(){ int v = 0; char c; while((c = getchar()) != EOF && c != ' ' && c != '\n'){ v *= 10; v += c - '0'; } return v; }
inline int readintsigned() { int v = 0; int sign = 1; char c = getchar(); if (c == '-') { sign = -1; } else { v += c - '0'; } while ((c = getchar()) != EOF && c != ' ' && c != '\n') { v *= 10; v += c - '0'; } return v * sign; }
inline string readstring() { string s; char c; while ((c = getchar()) != EOF && c != '\n' && c != ' ') { s.push_back(c); } return s; }
template <class result_t=std::chrono::milliseconds,class clock_t=std::chrono::steady_clock,class duration_t = std::chrono::milliseconds>
auto since(std::chrono::time_point<clock_t, duration_t> const& start){return std::chrono::duration_cast<result_t>(clock_t::now() - start);}
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<vpii> vvpii;
typedef vector<vpl> vvpl;
vvpl adj(200001);
int removed[200001], subtreeSiz[200001];
ll k, currIt = 0;
vector<pl> found(1000001, pl{1, INF});
vpl saved;
int getSiz(int u, int last = -1){
if(removed[u]) return 0;
int res = 1;
for(auto &[v, w] : adj[u]){
if(v == last) continue;
res += getSiz(v, u);
}
subtreeSiz[u] = res;
return res;
}
int getCentroid(int u, int last, int siz){
for(auto &[v, w] : adj[u]){
if(v == last || removed[v]) continue;
if(subtreeSiz[v] * 2 > siz) return getCentroid(v, u, siz);
}
return u;
}
ll compute(int u, int last, ll l, int steps = 1){
if(removed[u] || k-l<0) return INF;
saved.pb({l, steps});
ll res = INF;
if(found[k-l].F==currIt) res = found[k-l].S+steps;
for(auto &[v, w] : adj[u]){
if(v == last) continue;
res = min(res, compute(v, u, l+w, steps+1));
}
return res;
}
ll CentroidDecomp(int node){
int centroid = getCentroid(node, -1, getSiz(node));
currIt--;
removed[centroid] = 1;
ll res = INF;
found[0] = {currIt, 0};
for(auto &[v, w] : adj[centroid]){
saved.clear();
res = min(res, compute(v, -1, w));
for(auto &[v1, v2] : saved) {
found[v1] = min(found[v1], {currIt, v2});
}
}
for(auto &[v, w] : adj[centroid]){
if(removed[v]) continue;
res = min(res, CentroidDecomp(v));
}
return res;
}
int best_path(int N, int K, int H[][2], int L[]){
k = K;
fo(i, N-1){
adj[H[i][0]].pb({H[i][1], L[i]});
adj[H[i][1]].pb({H[i][0], L[i]});
}
ll res = CentroidDecomp(0);
return (res>1e16?-1:res);
}
// #define MAX_N 500000
// static int N, K;
// static int H[MAX_N][2];
// static int L[MAX_N];
// static int solution;
// 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));
// }
// 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 |
5 ms |
24920 KB |
Output is correct |
2 |
Correct |
5 ms |
24920 KB |
Output is correct |
3 |
Correct |
5 ms |
24924 KB |
Output is correct |
4 |
Correct |
5 ms |
24924 KB |
Output is correct |
5 |
Correct |
5 ms |
24668 KB |
Output is correct |
6 |
Correct |
5 ms |
24920 KB |
Output is correct |
7 |
Correct |
5 ms |
24920 KB |
Output is correct |
8 |
Correct |
5 ms |
24924 KB |
Output is correct |
9 |
Correct |
5 ms |
24736 KB |
Output is correct |
10 |
Correct |
5 ms |
24924 KB |
Output is correct |
11 |
Correct |
5 ms |
24924 KB |
Output is correct |
12 |
Correct |
5 ms |
24924 KB |
Output is correct |
13 |
Correct |
5 ms |
24684 KB |
Output is correct |
14 |
Correct |
6 ms |
24924 KB |
Output is correct |
15 |
Correct |
5 ms |
24924 KB |
Output is correct |
16 |
Correct |
5 ms |
24924 KB |
Output is correct |
17 |
Correct |
5 ms |
24924 KB |
Output is correct |
18 |
Correct |
5 ms |
24924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
24920 KB |
Output is correct |
2 |
Correct |
5 ms |
24920 KB |
Output is correct |
3 |
Correct |
5 ms |
24924 KB |
Output is correct |
4 |
Correct |
5 ms |
24924 KB |
Output is correct |
5 |
Correct |
5 ms |
24668 KB |
Output is correct |
6 |
Correct |
5 ms |
24920 KB |
Output is correct |
7 |
Correct |
5 ms |
24920 KB |
Output is correct |
8 |
Correct |
5 ms |
24924 KB |
Output is correct |
9 |
Correct |
5 ms |
24736 KB |
Output is correct |
10 |
Correct |
5 ms |
24924 KB |
Output is correct |
11 |
Correct |
5 ms |
24924 KB |
Output is correct |
12 |
Correct |
5 ms |
24924 KB |
Output is correct |
13 |
Correct |
5 ms |
24684 KB |
Output is correct |
14 |
Correct |
6 ms |
24924 KB |
Output is correct |
15 |
Correct |
5 ms |
24924 KB |
Output is correct |
16 |
Correct |
5 ms |
24924 KB |
Output is correct |
17 |
Correct |
5 ms |
24924 KB |
Output is correct |
18 |
Correct |
5 ms |
24924 KB |
Output is correct |
19 |
Correct |
5 ms |
24668 KB |
Output is correct |
20 |
Correct |
5 ms |
24924 KB |
Output is correct |
21 |
Correct |
5 ms |
24924 KB |
Output is correct |
22 |
Correct |
5 ms |
24820 KB |
Output is correct |
23 |
Correct |
5 ms |
24920 KB |
Output is correct |
24 |
Correct |
6 ms |
24924 KB |
Output is correct |
25 |
Correct |
6 ms |
24924 KB |
Output is correct |
26 |
Correct |
6 ms |
24920 KB |
Output is correct |
27 |
Correct |
5 ms |
24924 KB |
Output is correct |
28 |
Correct |
6 ms |
24924 KB |
Output is correct |
29 |
Correct |
6 ms |
24924 KB |
Output is correct |
30 |
Correct |
5 ms |
24924 KB |
Output is correct |
31 |
Correct |
5 ms |
24924 KB |
Output is correct |
32 |
Correct |
7 ms |
24924 KB |
Output is correct |
33 |
Correct |
6 ms |
24924 KB |
Output is correct |
34 |
Correct |
7 ms |
24924 KB |
Output is correct |
35 |
Correct |
5 ms |
24924 KB |
Output is correct |
36 |
Correct |
5 ms |
24924 KB |
Output is correct |
37 |
Correct |
5 ms |
25176 KB |
Output is correct |
38 |
Correct |
6 ms |
24836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
24920 KB |
Output is correct |
2 |
Correct |
5 ms |
24920 KB |
Output is correct |
3 |
Correct |
5 ms |
24924 KB |
Output is correct |
4 |
Correct |
5 ms |
24924 KB |
Output is correct |
5 |
Correct |
5 ms |
24668 KB |
Output is correct |
6 |
Correct |
5 ms |
24920 KB |
Output is correct |
7 |
Correct |
5 ms |
24920 KB |
Output is correct |
8 |
Correct |
5 ms |
24924 KB |
Output is correct |
9 |
Correct |
5 ms |
24736 KB |
Output is correct |
10 |
Correct |
5 ms |
24924 KB |
Output is correct |
11 |
Correct |
5 ms |
24924 KB |
Output is correct |
12 |
Correct |
5 ms |
24924 KB |
Output is correct |
13 |
Correct |
5 ms |
24684 KB |
Output is correct |
14 |
Correct |
6 ms |
24924 KB |
Output is correct |
15 |
Correct |
5 ms |
24924 KB |
Output is correct |
16 |
Correct |
5 ms |
24924 KB |
Output is correct |
17 |
Correct |
5 ms |
24924 KB |
Output is correct |
18 |
Correct |
5 ms |
24924 KB |
Output is correct |
19 |
Correct |
73 ms |
32596 KB |
Output is correct |
20 |
Correct |
72 ms |
32604 KB |
Output is correct |
21 |
Correct |
80 ms |
33056 KB |
Output is correct |
22 |
Correct |
80 ms |
33232 KB |
Output is correct |
23 |
Correct |
50 ms |
32808 KB |
Output is correct |
24 |
Correct |
39 ms |
32196 KB |
Output is correct |
25 |
Correct |
85 ms |
34388 KB |
Output is correct |
26 |
Correct |
74 ms |
38508 KB |
Output is correct |
27 |
Correct |
142 ms |
38868 KB |
Output is correct |
28 |
Correct |
151 ms |
46200 KB |
Output is correct |
29 |
Correct |
149 ms |
45696 KB |
Output is correct |
30 |
Correct |
102 ms |
38736 KB |
Output is correct |
31 |
Correct |
102 ms |
38856 KB |
Output is correct |
32 |
Correct |
119 ms |
38940 KB |
Output is correct |
33 |
Correct |
138 ms |
37712 KB |
Output is correct |
34 |
Correct |
113 ms |
37456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
24920 KB |
Output is correct |
2 |
Correct |
5 ms |
24920 KB |
Output is correct |
3 |
Correct |
5 ms |
24924 KB |
Output is correct |
4 |
Correct |
5 ms |
24924 KB |
Output is correct |
5 |
Correct |
5 ms |
24668 KB |
Output is correct |
6 |
Correct |
5 ms |
24920 KB |
Output is correct |
7 |
Correct |
5 ms |
24920 KB |
Output is correct |
8 |
Correct |
5 ms |
24924 KB |
Output is correct |
9 |
Correct |
5 ms |
24736 KB |
Output is correct |
10 |
Correct |
5 ms |
24924 KB |
Output is correct |
11 |
Correct |
5 ms |
24924 KB |
Output is correct |
12 |
Correct |
5 ms |
24924 KB |
Output is correct |
13 |
Correct |
5 ms |
24684 KB |
Output is correct |
14 |
Correct |
6 ms |
24924 KB |
Output is correct |
15 |
Correct |
5 ms |
24924 KB |
Output is correct |
16 |
Correct |
5 ms |
24924 KB |
Output is correct |
17 |
Correct |
5 ms |
24924 KB |
Output is correct |
18 |
Correct |
5 ms |
24924 KB |
Output is correct |
19 |
Correct |
5 ms |
24668 KB |
Output is correct |
20 |
Correct |
5 ms |
24924 KB |
Output is correct |
21 |
Correct |
5 ms |
24924 KB |
Output is correct |
22 |
Correct |
5 ms |
24820 KB |
Output is correct |
23 |
Correct |
5 ms |
24920 KB |
Output is correct |
24 |
Correct |
6 ms |
24924 KB |
Output is correct |
25 |
Correct |
6 ms |
24924 KB |
Output is correct |
26 |
Correct |
6 ms |
24920 KB |
Output is correct |
27 |
Correct |
5 ms |
24924 KB |
Output is correct |
28 |
Correct |
6 ms |
24924 KB |
Output is correct |
29 |
Correct |
6 ms |
24924 KB |
Output is correct |
30 |
Correct |
5 ms |
24924 KB |
Output is correct |
31 |
Correct |
5 ms |
24924 KB |
Output is correct |
32 |
Correct |
7 ms |
24924 KB |
Output is correct |
33 |
Correct |
6 ms |
24924 KB |
Output is correct |
34 |
Correct |
7 ms |
24924 KB |
Output is correct |
35 |
Correct |
5 ms |
24924 KB |
Output is correct |
36 |
Correct |
5 ms |
24924 KB |
Output is correct |
37 |
Correct |
5 ms |
25176 KB |
Output is correct |
38 |
Correct |
6 ms |
24836 KB |
Output is correct |
39 |
Correct |
73 ms |
32596 KB |
Output is correct |
40 |
Correct |
72 ms |
32604 KB |
Output is correct |
41 |
Correct |
80 ms |
33056 KB |
Output is correct |
42 |
Correct |
80 ms |
33232 KB |
Output is correct |
43 |
Correct |
50 ms |
32808 KB |
Output is correct |
44 |
Correct |
39 ms |
32196 KB |
Output is correct |
45 |
Correct |
85 ms |
34388 KB |
Output is correct |
46 |
Correct |
74 ms |
38508 KB |
Output is correct |
47 |
Correct |
142 ms |
38868 KB |
Output is correct |
48 |
Correct |
151 ms |
46200 KB |
Output is correct |
49 |
Correct |
149 ms |
45696 KB |
Output is correct |
50 |
Correct |
102 ms |
38736 KB |
Output is correct |
51 |
Correct |
102 ms |
38856 KB |
Output is correct |
52 |
Correct |
119 ms |
38940 KB |
Output is correct |
53 |
Correct |
138 ms |
37712 KB |
Output is correct |
54 |
Correct |
113 ms |
37456 KB |
Output is correct |
55 |
Correct |
10 ms |
25436 KB |
Output is correct |
56 |
Correct |
10 ms |
25640 KB |
Output is correct |
57 |
Correct |
59 ms |
33588 KB |
Output is correct |
58 |
Correct |
32 ms |
32196 KB |
Output is correct |
59 |
Correct |
82 ms |
38348 KB |
Output is correct |
60 |
Correct |
250 ms |
50260 KB |
Output is correct |
61 |
Correct |
114 ms |
38896 KB |
Output is correct |
62 |
Correct |
141 ms |
38772 KB |
Output is correct |
63 |
Correct |
138 ms |
38992 KB |
Output is correct |
64 |
Correct |
264 ms |
39876 KB |
Output is correct |
65 |
Correct |
137 ms |
38484 KB |
Output is correct |
66 |
Correct |
220 ms |
44220 KB |
Output is correct |
67 |
Correct |
70 ms |
37564 KB |
Output is correct |
68 |
Correct |
162 ms |
38480 KB |
Output is correct |
69 |
Correct |
152 ms |
38372 KB |
Output is correct |
70 |
Correct |
143 ms |
37968 KB |
Output is correct |