#include <bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
#define ALL(i) i.begin(),i.end()
#define SZ(i) int(i.size())
#define X first
#define Y second
#define eb emplace_back
#define pb push_back
#ifdef tmd
#define debug(...) fprintf(stderr,"#%d-(%s)=",__LINE__,#__VA_ARGS__);_do(__VA_ARGS__);
template<typename T> void _do(T &&x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T &&x, S &&...y) {cerr<<x<<",";_do(y...);}
template<typename IT> ostream& printRng (ostream& os, IT bg, IT ed) {
os << "{";
for (IT it=bg; it!=ed; it++) {
if (it == bg) os << *it;
else os << "," << *it;
}
return os << "}";
}
template<typename T> ostream& operator << (ostream& os, const vector<T>& v) {return printRng(os,ALL(v));}
template<typename T, typename S> ostream& operator << (ostream& os, const map<T,S>& v) {return printRng(os,ALL(v));}
template<typename T, typename S> ostream& operator << (ostream& os, const pair<T,S>& v) {return os<<"("<<v.X<<","<<v.Y<<")";}
#else
#define debug(...)
#endif
#include "race.h"
int n, k;
vector<vector<pii> > edge;
vector<ll> dep;
vector<int> edp;
vector<map<ll,int> > dmp;
const int INF = 0x3f3f3f3f;
int getm (map<ll,int> &mp, ll tg) {
auto ptr = mp.find(tg);
if (ptr == mp.end()) return INF;
else return ptr->Y;
}
int ans = INF;
void dfs (int nd, int par) {
dmp[nd][dep[nd]] = edp[nd];
for (auto &[u, w] : edge[nd]) {
if (u != par) {
dep[u] = dep[nd] + w;
edp[u] = edp[nd] + 1;
dfs(u, nd);
if (SZ(dmp[nd]) < SZ(dmp[u])) {
swap(dmp[nd], dmp[u]);
}
debug(dmp[nd]);
for (auto &[dw, de] : dmp[u]) {
ll rem = k - (dw-dep[nd]) + dep[nd];
int res = getm(dmp[nd], rem);
debug(nd, u, de, res, edp[nd]);
ans = min(ans, de + res - 2*edp[nd]);
}
for (auto &[dw, de] : dmp[u]) {
if (dmp[nd].count(dw)) {
int &x = dmp[nd][dw];
x = min(x, de);
} else {
dmp[nd][dw] = de;
}
}
}
}
debug(nd, dmp[nd]);
}
int best_path(int N, int K, int H[][2], int L[]) {
n = N;
k = K;
edge.resize(N);
for (int i=0; i<n-1; i++) {
int u, v, w;
u = H[i][0];
v = H[i][1];
w = L[i];
edge[u].eb(v, w);
edge[v].eb(u, w);
}
dep.resize(n);
edp.resize(n);
dmp.resize(n);
dfs(0, -1);
return ans <= n ? ans : -1;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
380 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
640 KB |
Output is correct |
22 |
Correct |
2 ms |
768 KB |
Output is correct |
23 |
Correct |
2 ms |
768 KB |
Output is correct |
24 |
Correct |
2 ms |
768 KB |
Output is correct |
25 |
Correct |
2 ms |
640 KB |
Output is correct |
26 |
Correct |
2 ms |
768 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
2 ms |
640 KB |
Output is correct |
29 |
Correct |
2 ms |
768 KB |
Output is correct |
30 |
Correct |
2 ms |
768 KB |
Output is correct |
31 |
Correct |
2 ms |
768 KB |
Output is correct |
32 |
Correct |
2 ms |
640 KB |
Output is correct |
33 |
Correct |
2 ms |
768 KB |
Output is correct |
34 |
Correct |
2 ms |
640 KB |
Output is correct |
35 |
Correct |
2 ms |
648 KB |
Output is correct |
36 |
Correct |
2 ms |
640 KB |
Output is correct |
37 |
Correct |
2 ms |
640 KB |
Output is correct |
38 |
Correct |
2 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
380 KB |
Output is correct |
19 |
Correct |
173 ms |
30412 KB |
Output is correct |
20 |
Correct |
176 ms |
30328 KB |
Output is correct |
21 |
Correct |
161 ms |
30072 KB |
Output is correct |
22 |
Correct |
178 ms |
29688 KB |
Output is correct |
23 |
Correct |
217 ms |
42488 KB |
Output is correct |
24 |
Correct |
198 ms |
33144 KB |
Output is correct |
25 |
Correct |
161 ms |
30200 KB |
Output is correct |
26 |
Correct |
83 ms |
37752 KB |
Output is correct |
27 |
Correct |
278 ms |
48624 KB |
Output is correct |
28 |
Correct |
401 ms |
87928 KB |
Output is correct |
29 |
Correct |
428 ms |
86264 KB |
Output is correct |
30 |
Correct |
278 ms |
48632 KB |
Output is correct |
31 |
Correct |
271 ms |
48632 KB |
Output is correct |
32 |
Correct |
357 ms |
48760 KB |
Output is correct |
33 |
Correct |
355 ms |
53624 KB |
Output is correct |
34 |
Correct |
471 ms |
86392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
380 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
640 KB |
Output is correct |
22 |
Correct |
2 ms |
768 KB |
Output is correct |
23 |
Correct |
2 ms |
768 KB |
Output is correct |
24 |
Correct |
2 ms |
768 KB |
Output is correct |
25 |
Correct |
2 ms |
640 KB |
Output is correct |
26 |
Correct |
2 ms |
768 KB |
Output is correct |
27 |
Correct |
1 ms |
512 KB |
Output is correct |
28 |
Correct |
2 ms |
640 KB |
Output is correct |
29 |
Correct |
2 ms |
768 KB |
Output is correct |
30 |
Correct |
2 ms |
768 KB |
Output is correct |
31 |
Correct |
2 ms |
768 KB |
Output is correct |
32 |
Correct |
2 ms |
640 KB |
Output is correct |
33 |
Correct |
2 ms |
768 KB |
Output is correct |
34 |
Correct |
2 ms |
640 KB |
Output is correct |
35 |
Correct |
2 ms |
648 KB |
Output is correct |
36 |
Correct |
2 ms |
640 KB |
Output is correct |
37 |
Correct |
2 ms |
640 KB |
Output is correct |
38 |
Correct |
2 ms |
640 KB |
Output is correct |
39 |
Correct |
173 ms |
30412 KB |
Output is correct |
40 |
Correct |
176 ms |
30328 KB |
Output is correct |
41 |
Correct |
161 ms |
30072 KB |
Output is correct |
42 |
Correct |
178 ms |
29688 KB |
Output is correct |
43 |
Correct |
217 ms |
42488 KB |
Output is correct |
44 |
Correct |
198 ms |
33144 KB |
Output is correct |
45 |
Correct |
161 ms |
30200 KB |
Output is correct |
46 |
Correct |
83 ms |
37752 KB |
Output is correct |
47 |
Correct |
278 ms |
48624 KB |
Output is correct |
48 |
Correct |
401 ms |
87928 KB |
Output is correct |
49 |
Correct |
428 ms |
86264 KB |
Output is correct |
50 |
Correct |
278 ms |
48632 KB |
Output is correct |
51 |
Correct |
271 ms |
48632 KB |
Output is correct |
52 |
Correct |
357 ms |
48760 KB |
Output is correct |
53 |
Correct |
355 ms |
53624 KB |
Output is correct |
54 |
Correct |
471 ms |
86392 KB |
Output is correct |
55 |
Correct |
20 ms |
4352 KB |
Output is correct |
56 |
Correct |
13 ms |
3072 KB |
Output is correct |
57 |
Correct |
107 ms |
27772 KB |
Output is correct |
58 |
Correct |
65 ms |
20972 KB |
Output is correct |
59 |
Correct |
117 ms |
43924 KB |
Output is correct |
60 |
Correct |
497 ms |
86624 KB |
Output is correct |
61 |
Correct |
319 ms |
52088 KB |
Output is correct |
62 |
Correct |
266 ms |
48760 KB |
Output is correct |
63 |
Correct |
365 ms |
48652 KB |
Output is correct |
64 |
Correct |
630 ms |
104056 KB |
Output is correct |
65 |
Correct |
692 ms |
102136 KB |
Output is correct |
66 |
Correct |
531 ms |
82680 KB |
Output is correct |
67 |
Correct |
290 ms |
43156 KB |
Output is correct |
68 |
Correct |
513 ms |
65528 KB |
Output is correct |
69 |
Correct |
798 ms |
70648 KB |
Output is correct |
70 |
Correct |
508 ms |
62840 KB |
Output is correct |