#include <bits/stdc++.h>
#include "race.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
#define sz(x) (int)x.size()
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x);
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef DEBUG
#define dbg(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << endl;
#else
#define dbg(x...)
#endif
const int MAXN = 2e5+5, INF = 1e9;
int n, k, ans = INF, h[MAXN][2], l[MAXN], dep[MAXN];
vector<pii> arr[MAXN];
set<pii> vals[MAXN];
pii offset[MAXN];
void solve(int at, int parent, int cost, int d = 0){
// Find the largest subtree
int big = at, bigCost = 0;
dep[at] = d;
for (pii i : arr[at]){
if (i.first != parent){
solve(i.first, at, i.second, d+1);
if (vals[i.first].size() > vals[big].size()){
big = i.first;
bigCost = i.second;
}
}
}
if (at != big){
swap(vals[at], vals[big]); // Copy the largest subtree
offset[at] = offset[big];
offset[at].first += bigCost;
offset[at].second++;
}
// Manually add the remaining elements
for (pii i : arr[at]){
if (i.first != parent && i.first != big){ // Since vals[big] is empty now (we called swap() on it), we don't have to worry about it anymore
for (pii x : vals[i.first]){
int costNeeded = k - (x.first + offset[i.first].first + i.second) - offset[at].first;
int adjDist = x.second + offset[i.first].second + 1;
auto it = vals[at].lower_bound({costNeeded, -INF});
if (it != vals[at].end() && (*it).first == costNeeded){
ckmin(ans, (*it).second + offset[at].second + adjDist);
}
}
for (pii x : vals[i.first]){
int adjCost = (x.first + offset[i.first].first + i.second) - offset[at].first;
int adjDist = x.second + offset[i.first].second - offset[at].second + 1;
vals[at].insert({adjCost, adjDist});
}
}
}
auto it = vals[at].lower_bound({k - offset[at].first, -INF});
if (it != vals[at].end() && (*it).first == k - offset[at].first){
ckmin(ans, (*it).second + offset[at].second);
// dbg(ans, at, offset[at].first, (*it), offset[at].second)
}
vals[at].insert({-offset[at].first, -offset[at].second});
}
int best_path(int N, int K, int h[][2], int l[]){
n = N, k = K;
for (int i=0; i<n-1; i++){
int a = h[i][0], b = h[i][1];
arr[a].push_back({b, l[i]});
arr[b].push_back({a, l[i]});
}
solve(0, -1, 0);
return ans != INF ? ans : -1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14404 KB |
Output is correct |
5 |
Correct |
8 ms |
14412 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14404 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14404 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14404 KB |
Output is correct |
12 |
Correct |
8 ms |
14404 KB |
Output is correct |
13 |
Correct |
8 ms |
14408 KB |
Output is correct |
14 |
Correct |
8 ms |
14408 KB |
Output is correct |
15 |
Correct |
8 ms |
14380 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
9 ms |
14368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14404 KB |
Output is correct |
5 |
Correct |
8 ms |
14412 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14404 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14404 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14404 KB |
Output is correct |
12 |
Correct |
8 ms |
14404 KB |
Output is correct |
13 |
Correct |
8 ms |
14408 KB |
Output is correct |
14 |
Correct |
8 ms |
14408 KB |
Output is correct |
15 |
Correct |
8 ms |
14380 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
9 ms |
14368 KB |
Output is correct |
19 |
Correct |
8 ms |
14420 KB |
Output is correct |
20 |
Correct |
8 ms |
14412 KB |
Output is correct |
21 |
Correct |
9 ms |
14580 KB |
Output is correct |
22 |
Correct |
9 ms |
14548 KB |
Output is correct |
23 |
Correct |
9 ms |
14580 KB |
Output is correct |
24 |
Correct |
9 ms |
14548 KB |
Output is correct |
25 |
Correct |
9 ms |
14548 KB |
Output is correct |
26 |
Correct |
9 ms |
14548 KB |
Output is correct |
27 |
Correct |
8 ms |
14544 KB |
Output is correct |
28 |
Correct |
9 ms |
14548 KB |
Output is correct |
29 |
Correct |
8 ms |
14548 KB |
Output is correct |
30 |
Correct |
10 ms |
14532 KB |
Output is correct |
31 |
Correct |
9 ms |
14580 KB |
Output is correct |
32 |
Correct |
11 ms |
14548 KB |
Output is correct |
33 |
Correct |
9 ms |
14548 KB |
Output is correct |
34 |
Correct |
9 ms |
14548 KB |
Output is correct |
35 |
Correct |
9 ms |
14556 KB |
Output is correct |
36 |
Correct |
8 ms |
14508 KB |
Output is correct |
37 |
Correct |
8 ms |
14496 KB |
Output is correct |
38 |
Correct |
8 ms |
14548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14404 KB |
Output is correct |
5 |
Correct |
8 ms |
14412 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14404 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14404 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14404 KB |
Output is correct |
12 |
Correct |
8 ms |
14404 KB |
Output is correct |
13 |
Correct |
8 ms |
14408 KB |
Output is correct |
14 |
Correct |
8 ms |
14408 KB |
Output is correct |
15 |
Correct |
8 ms |
14380 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
9 ms |
14368 KB |
Output is correct |
19 |
Correct |
117 ms |
33648 KB |
Output is correct |
20 |
Correct |
123 ms |
33504 KB |
Output is correct |
21 |
Correct |
125 ms |
33188 KB |
Output is correct |
22 |
Correct |
120 ms |
32668 KB |
Output is correct |
23 |
Correct |
143 ms |
44004 KB |
Output is correct |
24 |
Correct |
116 ms |
35628 KB |
Output is correct |
25 |
Correct |
115 ms |
38004 KB |
Output is correct |
26 |
Correct |
68 ms |
44620 KB |
Output is correct |
27 |
Correct |
177 ms |
41340 KB |
Output is correct |
28 |
Correct |
229 ms |
74928 KB |
Output is correct |
29 |
Correct |
208 ms |
73008 KB |
Output is correct |
30 |
Correct |
179 ms |
41416 KB |
Output is correct |
31 |
Correct |
205 ms |
41348 KB |
Output is correct |
32 |
Correct |
224 ms |
41448 KB |
Output is correct |
33 |
Correct |
190 ms |
37844 KB |
Output is correct |
34 |
Correct |
289 ms |
61884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
8 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14404 KB |
Output is correct |
5 |
Correct |
8 ms |
14412 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14404 KB |
Output is correct |
8 |
Correct |
8 ms |
14420 KB |
Output is correct |
9 |
Correct |
8 ms |
14404 KB |
Output is correct |
10 |
Correct |
8 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14404 KB |
Output is correct |
12 |
Correct |
8 ms |
14404 KB |
Output is correct |
13 |
Correct |
8 ms |
14408 KB |
Output is correct |
14 |
Correct |
8 ms |
14408 KB |
Output is correct |
15 |
Correct |
8 ms |
14380 KB |
Output is correct |
16 |
Correct |
8 ms |
14420 KB |
Output is correct |
17 |
Correct |
8 ms |
14420 KB |
Output is correct |
18 |
Correct |
9 ms |
14368 KB |
Output is correct |
19 |
Correct |
8 ms |
14420 KB |
Output is correct |
20 |
Correct |
8 ms |
14412 KB |
Output is correct |
21 |
Correct |
9 ms |
14580 KB |
Output is correct |
22 |
Correct |
9 ms |
14548 KB |
Output is correct |
23 |
Correct |
9 ms |
14580 KB |
Output is correct |
24 |
Correct |
9 ms |
14548 KB |
Output is correct |
25 |
Correct |
9 ms |
14548 KB |
Output is correct |
26 |
Correct |
9 ms |
14548 KB |
Output is correct |
27 |
Correct |
8 ms |
14544 KB |
Output is correct |
28 |
Correct |
9 ms |
14548 KB |
Output is correct |
29 |
Correct |
8 ms |
14548 KB |
Output is correct |
30 |
Correct |
10 ms |
14532 KB |
Output is correct |
31 |
Correct |
9 ms |
14580 KB |
Output is correct |
32 |
Correct |
11 ms |
14548 KB |
Output is correct |
33 |
Correct |
9 ms |
14548 KB |
Output is correct |
34 |
Correct |
9 ms |
14548 KB |
Output is correct |
35 |
Correct |
9 ms |
14556 KB |
Output is correct |
36 |
Correct |
8 ms |
14508 KB |
Output is correct |
37 |
Correct |
8 ms |
14496 KB |
Output is correct |
38 |
Correct |
8 ms |
14548 KB |
Output is correct |
39 |
Correct |
117 ms |
33648 KB |
Output is correct |
40 |
Correct |
123 ms |
33504 KB |
Output is correct |
41 |
Correct |
125 ms |
33188 KB |
Output is correct |
42 |
Correct |
120 ms |
32668 KB |
Output is correct |
43 |
Correct |
143 ms |
44004 KB |
Output is correct |
44 |
Correct |
116 ms |
35628 KB |
Output is correct |
45 |
Correct |
115 ms |
38004 KB |
Output is correct |
46 |
Correct |
68 ms |
44620 KB |
Output is correct |
47 |
Correct |
177 ms |
41340 KB |
Output is correct |
48 |
Correct |
229 ms |
74928 KB |
Output is correct |
49 |
Correct |
208 ms |
73008 KB |
Output is correct |
50 |
Correct |
179 ms |
41416 KB |
Output is correct |
51 |
Correct |
205 ms |
41348 KB |
Output is correct |
52 |
Correct |
224 ms |
41448 KB |
Output is correct |
53 |
Correct |
190 ms |
37844 KB |
Output is correct |
54 |
Correct |
289 ms |
61884 KB |
Output is correct |
55 |
Correct |
18 ms |
16856 KB |
Output is correct |
56 |
Correct |
15 ms |
15948 KB |
Output is correct |
57 |
Correct |
70 ms |
30012 KB |
Output is correct |
58 |
Correct |
46 ms |
25544 KB |
Output is correct |
59 |
Correct |
66 ms |
44652 KB |
Output is correct |
60 |
Correct |
191 ms |
73792 KB |
Output is correct |
61 |
Correct |
215 ms |
45864 KB |
Output is correct |
62 |
Correct |
173 ms |
41628 KB |
Output is correct |
63 |
Correct |
222 ms |
41728 KB |
Output is correct |
64 |
Correct |
388 ms |
82016 KB |
Output is correct |
65 |
Correct |
354 ms |
78044 KB |
Output is correct |
66 |
Correct |
220 ms |
68628 KB |
Output is correct |
67 |
Correct |
175 ms |
39660 KB |
Output is correct |
68 |
Correct |
300 ms |
54720 KB |
Output is correct |
69 |
Correct |
313 ms |
55316 KB |
Output is correct |
70 |
Correct |
286 ms |
53072 KB |
Output is correct |