#include <bits/stdc++.h>
#include "race.h"
using namespace std;
using ll = long long;
using ld = long double;
#define fi first
#define se second
#define vi vector<int>
#define vll vector<long long>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)(x).size()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
inline int rand(int l,int r){return uniform_int_distribution<int>(l, r)(rng);}
inline ll rand(ll l,ll r){return uniform_int_distribution<ll>(l, r)(rng64);}
#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
const int MAXN = 2e5 + 7;
vector<pii> g[MAXN];
bool blocked[MAXN];
int sz[MAXN];
int parent[MAXN];
int n, k;
ll ans;
unordered_map<ll, ll, custom_hash> m;
vector<pll> akt;
int dfs(int v, int p){
sz[v] = 1;
parent[v] = p;
for(auto ele : g[v]){
if(ele.fi == p || blocked[ele.fi]){
continue;
}
sz[v] += dfs(ele.fi, v);
}
return sz[v];
}
void dfs2(int v, int p, pll s){
akt.pb(s);
for(auto ele : g[v]){
if(blocked[ele.fi] || p == ele.fi){
continue;
}
dfs2(ele.fi, v, {s.fi + ele.se, s.se + 1});
}
}
void centroid_decomposition(int centroid){
int roz = dfs(centroid, centroid);
while(true){
bool f = 0;
for(auto ele : g[centroid]){
if(ele.fi == parent[centroid] || blocked[ele.fi]){
continue;
}
if(sz[ele.fi] * 2 >= roz){
centroid = ele.fi;
break;
}
}
if(!f){
break;
}
}
blocked[centroid] = 1;
m.clear();
m[0] = 0;
for(auto ele : g[centroid]){
if(blocked[ele.fi]){
continue;
}
akt.clear();
dfs2(ele.fi, ele.fi, {ele.se, 1});
for(auto ele2 : akt){
if(ele2.fi == k){
ans = min(ans, ele2.se);
}else if(ele2.fi < k){
if(m[k - ele2.fi] != 0){
ans = min(ans, ele2.se + m[k - ele2.fi]);
}
}
}
for(auto ele2 : akt){
if(m[ele2.fi] == 0){
m[ele2.fi] = ele2.se;
}else{
m[ele2.fi] = min(m[ele2.fi], ele2.se);
}
}
}
for(auto ele : g[centroid]){
if(blocked[ele.fi]){
continue;
}
centroid_decomposition(ele.fi);
}
}
int best_path(int N, int K, int H[][2], int L[]){
ans = 1e9;
n = N;
k = K;
//cout << n << ' ' << k << '\n';
for(int i = 0; i < n - 1; i++){
//cout << H[i][0] << ' ' << H[i][1] << ' ' << L[i] << '\n';
g[H[i][0]].pb({H[i][1], L[i]});
g[H[i][1]].pb({H[i][0], L[i]});
}
centroid_decomposition(0);
if(ans == (ll)1e9){
return -1;
}
return (int)ans;
}
/*int main(){
int tab[3][2] = {{0, 1}, {1, 2}, {1, 3}};
int l[3] = {1, 2, 4};
cout << best_path(4, 3, tab, l) << '\n';
return 0;
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4952 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
3 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4952 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
3 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4952 KB |
Output is correct |
21 |
Correct |
4 ms |
5212 KB |
Output is correct |
22 |
Correct |
3 ms |
5212 KB |
Output is correct |
23 |
Correct |
3 ms |
5212 KB |
Output is correct |
24 |
Correct |
3 ms |
5212 KB |
Output is correct |
25 |
Correct |
3 ms |
5356 KB |
Output is correct |
26 |
Correct |
3 ms |
5212 KB |
Output is correct |
27 |
Correct |
3 ms |
5212 KB |
Output is correct |
28 |
Correct |
3 ms |
5212 KB |
Output is correct |
29 |
Correct |
3 ms |
5352 KB |
Output is correct |
30 |
Correct |
3 ms |
5464 KB |
Output is correct |
31 |
Correct |
4 ms |
5208 KB |
Output is correct |
32 |
Correct |
4 ms |
5464 KB |
Output is correct |
33 |
Correct |
4 ms |
5208 KB |
Output is correct |
34 |
Correct |
7 ms |
5212 KB |
Output is correct |
35 |
Correct |
8 ms |
5304 KB |
Output is correct |
36 |
Correct |
9 ms |
5304 KB |
Output is correct |
37 |
Correct |
7 ms |
5212 KB |
Output is correct |
38 |
Correct |
5 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4952 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
3 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
197 ms |
12964 KB |
Output is correct |
20 |
Correct |
201 ms |
12748 KB |
Output is correct |
21 |
Correct |
166 ms |
11876 KB |
Output is correct |
22 |
Correct |
145 ms |
12820 KB |
Output is correct |
23 |
Correct |
168 ms |
13012 KB |
Output is correct |
24 |
Correct |
96 ms |
13000 KB |
Output is correct |
25 |
Execution timed out |
3057 ms |
14804 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
2 ms |
4952 KB |
Output is correct |
5 |
Correct |
2 ms |
4956 KB |
Output is correct |
6 |
Correct |
2 ms |
4956 KB |
Output is correct |
7 |
Correct |
2 ms |
4956 KB |
Output is correct |
8 |
Correct |
2 ms |
4956 KB |
Output is correct |
9 |
Correct |
2 ms |
4956 KB |
Output is correct |
10 |
Correct |
2 ms |
4956 KB |
Output is correct |
11 |
Correct |
2 ms |
4956 KB |
Output is correct |
12 |
Correct |
2 ms |
4956 KB |
Output is correct |
13 |
Correct |
2 ms |
4956 KB |
Output is correct |
14 |
Correct |
2 ms |
4956 KB |
Output is correct |
15 |
Correct |
3 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
2 ms |
4956 KB |
Output is correct |
18 |
Correct |
2 ms |
4956 KB |
Output is correct |
19 |
Correct |
2 ms |
4956 KB |
Output is correct |
20 |
Correct |
2 ms |
4952 KB |
Output is correct |
21 |
Correct |
4 ms |
5212 KB |
Output is correct |
22 |
Correct |
3 ms |
5212 KB |
Output is correct |
23 |
Correct |
3 ms |
5212 KB |
Output is correct |
24 |
Correct |
3 ms |
5212 KB |
Output is correct |
25 |
Correct |
3 ms |
5356 KB |
Output is correct |
26 |
Correct |
3 ms |
5212 KB |
Output is correct |
27 |
Correct |
3 ms |
5212 KB |
Output is correct |
28 |
Correct |
3 ms |
5212 KB |
Output is correct |
29 |
Correct |
3 ms |
5352 KB |
Output is correct |
30 |
Correct |
3 ms |
5464 KB |
Output is correct |
31 |
Correct |
4 ms |
5208 KB |
Output is correct |
32 |
Correct |
4 ms |
5464 KB |
Output is correct |
33 |
Correct |
4 ms |
5208 KB |
Output is correct |
34 |
Correct |
7 ms |
5212 KB |
Output is correct |
35 |
Correct |
8 ms |
5304 KB |
Output is correct |
36 |
Correct |
9 ms |
5304 KB |
Output is correct |
37 |
Correct |
7 ms |
5212 KB |
Output is correct |
38 |
Correct |
5 ms |
5212 KB |
Output is correct |
39 |
Correct |
197 ms |
12964 KB |
Output is correct |
40 |
Correct |
201 ms |
12748 KB |
Output is correct |
41 |
Correct |
166 ms |
11876 KB |
Output is correct |
42 |
Correct |
145 ms |
12820 KB |
Output is correct |
43 |
Correct |
168 ms |
13012 KB |
Output is correct |
44 |
Correct |
96 ms |
13000 KB |
Output is correct |
45 |
Execution timed out |
3057 ms |
14804 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |