#include <bits/stdc++.h>
using namespace std;
#define in ({int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';n?-x:x;})
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
long long rnd(long long l, long long r)
{
return l + rng() % (r - l + 1);
}
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args)
{
cerr << *it << " = " << a << endl;
err(++it, args...);
}
#define debug(x) cerr << #x << " is " << x << "\n";
#define fasty ios_base::sync_with_stdio(0),cin.tie(0);
#define forinc(a,b,c) for(int a=b,_c=c;a<=_c;++a)
#define fordec(a,b,c) for(int a=b,_c=c;a>=_c;--a)
#define forv(a,b) for(auto&a:b)
#define fi first
#define se second
#define pb push_back
#define ii pair<int,int>
#define mt make_tuple
#define all(a) a.begin(),a.end()
#define gg exit(0)
#define yn(x) ((x==true)?"YES":"NO")
#define getbit(x,i) ((x>>(i-1))&1)
#define batbit(x,i) (x|(1ll<<(i-1)))
#define tatbit(x,i) (x&~(1<<(i-1)))
//#define int long long
//#define DEBUG
const int N = 2e5 + 10, K = 1e6 + 10;
int dd[N], sz[N], avail[K], min_path[K], curr_node, n, k, res = 1e8;
vector<ii> adj[N];
int calc(int x, int par)
{
sz[x] = 1;
forv(i, adj[x]) if (!dd[i.fi] && i.fi != par) sz[x] += calc(i.fi, x);
return sz[x];
}
int get_ctr(int x, int par, int cnt)
{
forv(i, adj[x]) if (!dd[i.fi] && i.fi != par && sz[i.fi] * 2 > cnt) return get_ctr(i.fi, x, cnt);
return x;
}
void dfs_ans(int x, int par, int curr_w, int curr_len)
{
if (curr_w > k) return;
if (avail[k - curr_w] == curr_node) res = min(res, curr_len + min_path[k - curr_w]);
if (curr_w == k) res = min(res, curr_len);
forv(i, adj[x]) if (!dd[i.fi] && i.fi != par) dfs_ans(i.fi, x, curr_w + i.se, curr_len + 1);
}
void dfs_upd(int x, int par, int curr_w, int curr_len)
{
if (curr_w > k) return;
if (avail[curr_w] != curr_node)
{
avail[curr_w] = curr_node;
min_path[curr_w] = curr_len;
}
else min_path[curr_w] = min(min_path[curr_w], curr_len);
forv(i, adj[x]) if (!dd[i.fi] && i.fi != par) dfs_upd(i.fi, x, curr_w + i.se, curr_len + 1);
}
void centroid(int x, int par = 0)
{
curr_node++;
int cnt = calc(x, x); int node = get_ctr(x, x, cnt);
dd[node] = 1;
forv(i, adj[node]) if (!dd[i.fi])
{
dfs_ans(i.fi, node, i.se, 1);
dfs_upd(i.fi, node, i.se, 1);
}
forv(i, adj[node]) if (!dd[i.fi]) centroid(i.fi, node);
}
int best_path(int _n, int _k, int H[][2], int L[]) {
n = _n; k = _k;
for (int i = 1; i < n; ++i) {
adj[H[i - 1][0]].push_back(make_pair(H[i - 1][1], L[i - 1]));
adj[H[i - 1][1]].push_back(make_pair(H[i - 1][0], L[i - 1]));
}
centroid(1);
return ((res == 1e8) ? -1 : res);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5004 KB |
Output is correct |
2 |
Correct |
3 ms |
5020 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
5012 KB |
Output is correct |
6 |
Correct |
4 ms |
5012 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
5016 KB |
Output is correct |
12 |
Correct |
3 ms |
5004 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
5012 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5004 KB |
Output is correct |
2 |
Correct |
3 ms |
5020 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
5012 KB |
Output is correct |
6 |
Correct |
4 ms |
5012 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
5016 KB |
Output is correct |
12 |
Correct |
3 ms |
5004 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
5012 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
5016 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
6 ms |
10020 KB |
Output is correct |
23 |
Correct |
7 ms |
9388 KB |
Output is correct |
24 |
Correct |
8 ms |
10784 KB |
Output is correct |
25 |
Correct |
6 ms |
8612 KB |
Output is correct |
26 |
Correct |
5 ms |
7628 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
4 ms |
6604 KB |
Output is correct |
29 |
Correct |
4 ms |
7628 KB |
Output is correct |
30 |
Correct |
4 ms |
7868 KB |
Output is correct |
31 |
Correct |
5 ms |
8652 KB |
Output is correct |
32 |
Correct |
5 ms |
8744 KB |
Output is correct |
33 |
Correct |
6 ms |
10316 KB |
Output is correct |
34 |
Correct |
6 ms |
9508 KB |
Output is correct |
35 |
Correct |
6 ms |
10912 KB |
Output is correct |
36 |
Correct |
7 ms |
11596 KB |
Output is correct |
37 |
Correct |
6 ms |
8908 KB |
Output is correct |
38 |
Correct |
6 ms |
7372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5004 KB |
Output is correct |
2 |
Correct |
3 ms |
5020 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
5012 KB |
Output is correct |
6 |
Correct |
4 ms |
5012 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
5016 KB |
Output is correct |
12 |
Correct |
3 ms |
5004 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
5012 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
136 ms |
11972 KB |
Output is correct |
20 |
Correct |
104 ms |
11932 KB |
Output is correct |
21 |
Correct |
110 ms |
11972 KB |
Output is correct |
22 |
Correct |
100 ms |
12036 KB |
Output is correct |
23 |
Correct |
99 ms |
12392 KB |
Output is correct |
24 |
Correct |
51 ms |
12112 KB |
Output is correct |
25 |
Correct |
105 ms |
17016 KB |
Output is correct |
26 |
Correct |
78 ms |
17644 KB |
Output is correct |
27 |
Correct |
161 ms |
19452 KB |
Output is correct |
28 |
Correct |
282 ms |
29788 KB |
Output is correct |
29 |
Correct |
225 ms |
25724 KB |
Output is correct |
30 |
Correct |
145 ms |
19364 KB |
Output is correct |
31 |
Correct |
148 ms |
19268 KB |
Output is correct |
32 |
Correct |
272 ms |
19460 KB |
Output is correct |
33 |
Correct |
216 ms |
18112 KB |
Output is correct |
34 |
Correct |
183 ms |
18980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
5004 KB |
Output is correct |
2 |
Correct |
3 ms |
5020 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
5012 KB |
Output is correct |
6 |
Correct |
4 ms |
5012 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
4 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
3 ms |
5016 KB |
Output is correct |
12 |
Correct |
3 ms |
5004 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
5012 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
5016 KB |
Output is correct |
20 |
Correct |
3 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
5068 KB |
Output is correct |
22 |
Correct |
6 ms |
10020 KB |
Output is correct |
23 |
Correct |
7 ms |
9388 KB |
Output is correct |
24 |
Correct |
8 ms |
10784 KB |
Output is correct |
25 |
Correct |
6 ms |
8612 KB |
Output is correct |
26 |
Correct |
5 ms |
7628 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
4 ms |
6604 KB |
Output is correct |
29 |
Correct |
4 ms |
7628 KB |
Output is correct |
30 |
Correct |
4 ms |
7868 KB |
Output is correct |
31 |
Correct |
5 ms |
8652 KB |
Output is correct |
32 |
Correct |
5 ms |
8744 KB |
Output is correct |
33 |
Correct |
6 ms |
10316 KB |
Output is correct |
34 |
Correct |
6 ms |
9508 KB |
Output is correct |
35 |
Correct |
6 ms |
10912 KB |
Output is correct |
36 |
Correct |
7 ms |
11596 KB |
Output is correct |
37 |
Correct |
6 ms |
8908 KB |
Output is correct |
38 |
Correct |
6 ms |
7372 KB |
Output is correct |
39 |
Correct |
136 ms |
11972 KB |
Output is correct |
40 |
Correct |
104 ms |
11932 KB |
Output is correct |
41 |
Correct |
110 ms |
11972 KB |
Output is correct |
42 |
Correct |
100 ms |
12036 KB |
Output is correct |
43 |
Correct |
99 ms |
12392 KB |
Output is correct |
44 |
Correct |
51 ms |
12112 KB |
Output is correct |
45 |
Correct |
105 ms |
17016 KB |
Output is correct |
46 |
Correct |
78 ms |
17644 KB |
Output is correct |
47 |
Correct |
161 ms |
19452 KB |
Output is correct |
48 |
Correct |
282 ms |
29788 KB |
Output is correct |
49 |
Correct |
225 ms |
25724 KB |
Output is correct |
50 |
Correct |
145 ms |
19364 KB |
Output is correct |
51 |
Correct |
148 ms |
19268 KB |
Output is correct |
52 |
Correct |
272 ms |
19460 KB |
Output is correct |
53 |
Correct |
216 ms |
18112 KB |
Output is correct |
54 |
Correct |
183 ms |
18980 KB |
Output is correct |
55 |
Correct |
10 ms |
5708 KB |
Output is correct |
56 |
Correct |
11 ms |
5656 KB |
Output is correct |
57 |
Correct |
68 ms |
12188 KB |
Output is correct |
58 |
Correct |
30 ms |
11896 KB |
Output is correct |
59 |
Correct |
80 ms |
18500 KB |
Output is correct |
60 |
Correct |
455 ms |
30604 KB |
Output is correct |
61 |
Correct |
159 ms |
19584 KB |
Output is correct |
62 |
Correct |
183 ms |
19656 KB |
Output is correct |
63 |
Correct |
234 ms |
19532 KB |
Output is correct |
64 |
Correct |
456 ms |
21900 KB |
Output is correct |
65 |
Correct |
213 ms |
20256 KB |
Output is correct |
66 |
Correct |
336 ms |
36804 KB |
Output is correct |
67 |
Correct |
131 ms |
20148 KB |
Output is correct |
68 |
Correct |
249 ms |
25468 KB |
Output is correct |
69 |
Correct |
253 ms |
25720 KB |
Output is correct |
70 |
Correct |
249 ms |
24756 KB |
Output is correct |