#include "race.h"
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
#define mp make_pair
#define f first
#define s second
const ll inf = 1e18;
vector<vector<pair<ll,ll>>> edges;
vector<bool> centroids;
vector<ll> sizes;
ll n,k;
ll get_size(ll node, ll parent = -1) {
ll res = 1;
for(auto i: edges[node]) {
if(centroids[i.f] || i.f==parent)continue;
res+=get_size(i.f,node);
}
return sizes[node]=res;
}
ll get_centroid(ll node,ll _n,ll parent = -1) {
for(auto i: edges[node]) {
if(centroids[i.f] || i.f == parent)continue;
if(sizes[i.f]*2>_n)
return get_centroid(i.f,_n,node);
}
return node;
}
vector<gp_hash_table<ll,ll>>dp;
void dfs(ll node,ll val,ll times,ll parent = -1) {
if(dp.back().find(val)==dp.back().end())
dp.back()[val] = times;
else
dp.back()[val] = min(dp.back()[val],times);
for(auto i: edges[node]) {
if(i.f==parent || centroids[i.f]) continue;
dfs(i.f,val+i.s,times+1,node);
}
}
ll solve(ll node,int _n) {
//find centroid;
dp.clear();
get_size(node);
ll centroid = get_centroid(node,_n);
centroids[centroid] = true;
for(auto i: edges[centroid]) {
if(centroids[i.f])continue;
dp.emplace_back();
dfs(i.f,i.s,1);
}
ll ans = inf;
gp_hash_table<ll,ll> another;
for(auto i : dp) {
for(auto q:i) {
if(q.f==k)
ans = min(ans,q.s);
else {
if(another.find(k-q.f)!=another.end())
ans = min(ans,another[k-q.f]+q.s);
}
}
for(auto q: i) {
if(another.find(q.f)==another.end())
another[q.f] = q.s;
else
another[q.f] = min(another[q.f],q.s);
}
}
//cout << dp.size() << endl;
for(auto i: edges[centroid]) {
if(!centroids[i.f])
ans = min(ans,solve(i.f,sizes[i.f]));
}
return ans;
}
int best_path(int N, int K, int H[][2], int L[]){
n = N;
k = K;
edges.resize(n);
sizes.resize(n);
centroids.resize(n);
pair<ll,ll> in[n];
ll len[n];
for(int i = 0; i<n-1; i++) {
in[i].f = H[i][0]; in[i].s = H[i][1]; len[i] = L[i];
edges[in[i].f].emplace_back(in[i].s,len[i]);
edges[in[i].s].emplace_back(in[i].f,len[i]);
}
ll _ = solve(0,n);
if(_ == inf)
_ =-1;
return _;
}
/*
int main() {
cin >> n >> k;
edges.resize(n);
sizes.resize(n);
centroids.resize(n);
pair<int,int> in[n];
int len[n];
for(int i = 0; i<n-1; i++) {
cin >> in[i].f >> in[i].s >> len[i];
edges[in[i].f].emplace_back(in[i].s,len[i]);
edges[in[i].s].emplace_back(in[i].f,len[i]);
}
auto _ = solve(0);
if(_ == inf)
_ =-1;
cout << _;
}*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2492 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2648 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2492 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2648 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
2652 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
3 ms |
2652 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
3 ms |
2652 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2800 KB |
Output is correct |
32 |
Correct |
3 ms |
2648 KB |
Output is correct |
33 |
Correct |
2 ms |
2652 KB |
Output is correct |
34 |
Correct |
2 ms |
2652 KB |
Output is correct |
35 |
Correct |
3 ms |
2652 KB |
Output is correct |
36 |
Correct |
2 ms |
2652 KB |
Output is correct |
37 |
Correct |
2 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2492 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2648 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
162 ms |
15404 KB |
Output is correct |
20 |
Correct |
168 ms |
15436 KB |
Output is correct |
21 |
Correct |
168 ms |
15184 KB |
Output is correct |
22 |
Correct |
140 ms |
15440 KB |
Output is correct |
23 |
Correct |
193 ms |
16148 KB |
Output is correct |
24 |
Correct |
106 ms |
15428 KB |
Output is correct |
25 |
Correct |
125 ms |
20680 KB |
Output is correct |
26 |
Correct |
86 ms |
21072 KB |
Output is correct |
27 |
Correct |
345 ms |
37704 KB |
Output is correct |
28 |
Correct |
819 ms |
80556 KB |
Output is correct |
29 |
Correct |
832 ms |
79796 KB |
Output is correct |
30 |
Correct |
311 ms |
37708 KB |
Output is correct |
31 |
Correct |
321 ms |
37708 KB |
Output is correct |
32 |
Correct |
406 ms |
37640 KB |
Output is correct |
33 |
Correct |
299 ms |
26280 KB |
Output is correct |
34 |
Correct |
724 ms |
71080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2492 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2648 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
2652 KB |
Output is correct |
22 |
Correct |
2 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2652 KB |
Output is correct |
24 |
Correct |
2 ms |
2652 KB |
Output is correct |
25 |
Correct |
3 ms |
2652 KB |
Output is correct |
26 |
Correct |
2 ms |
2652 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
3 ms |
2652 KB |
Output is correct |
29 |
Correct |
2 ms |
2652 KB |
Output is correct |
30 |
Correct |
2 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2800 KB |
Output is correct |
32 |
Correct |
3 ms |
2648 KB |
Output is correct |
33 |
Correct |
2 ms |
2652 KB |
Output is correct |
34 |
Correct |
2 ms |
2652 KB |
Output is correct |
35 |
Correct |
3 ms |
2652 KB |
Output is correct |
36 |
Correct |
2 ms |
2652 KB |
Output is correct |
37 |
Correct |
2 ms |
2652 KB |
Output is correct |
38 |
Correct |
2 ms |
2652 KB |
Output is correct |
39 |
Correct |
162 ms |
15404 KB |
Output is correct |
40 |
Correct |
168 ms |
15436 KB |
Output is correct |
41 |
Correct |
168 ms |
15184 KB |
Output is correct |
42 |
Correct |
140 ms |
15440 KB |
Output is correct |
43 |
Correct |
193 ms |
16148 KB |
Output is correct |
44 |
Correct |
106 ms |
15428 KB |
Output is correct |
45 |
Correct |
125 ms |
20680 KB |
Output is correct |
46 |
Correct |
86 ms |
21072 KB |
Output is correct |
47 |
Correct |
345 ms |
37704 KB |
Output is correct |
48 |
Correct |
819 ms |
80556 KB |
Output is correct |
49 |
Correct |
832 ms |
79796 KB |
Output is correct |
50 |
Correct |
311 ms |
37708 KB |
Output is correct |
51 |
Correct |
321 ms |
37708 KB |
Output is correct |
52 |
Correct |
406 ms |
37640 KB |
Output is correct |
53 |
Correct |
299 ms |
26280 KB |
Output is correct |
54 |
Correct |
724 ms |
71080 KB |
Output is correct |
55 |
Correct |
20 ms |
5012 KB |
Output is correct |
56 |
Correct |
14 ms |
3676 KB |
Output is correct |
57 |
Correct |
100 ms |
15716 KB |
Output is correct |
58 |
Correct |
75 ms |
56632 KB |
Output is correct |
59 |
Correct |
249 ms |
42684 KB |
Output is correct |
60 |
Correct |
776 ms |
80156 KB |
Output is correct |
61 |
Correct |
348 ms |
41032 KB |
Output is correct |
62 |
Correct |
313 ms |
37580 KB |
Output is correct |
63 |
Correct |
362 ms |
37816 KB |
Output is correct |
64 |
Correct |
683 ms |
50372 KB |
Output is correct |
65 |
Correct |
589 ms |
75172 KB |
Output is correct |
66 |
Correct |
804 ms |
77484 KB |
Output is correct |
67 |
Correct |
184 ms |
105908 KB |
Output is correct |
68 |
Correct |
401 ms |
71380 KB |
Output is correct |
69 |
Correct |
396 ms |
69840 KB |
Output is correct |
70 |
Correct |
362 ms |
68836 KB |
Output is correct |