#include "race.h"
#include <bits/stdc++.h>
#ifndef EVAL
#include "grader.cpp"
#endif
using namespace std;
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
#define pb push_back
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define sz(v) int((v).size())
#define do_not_disturb ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
int mod = 1e9+7;
ll modulo(ll a) {
return ((a % mod) + mod) % mod;
}
ll add(ll a, ll b) {
return modulo(a + b);
}
ll mult(ll a, ll b) {
return modulo(a * b);
}
ll binpow(ll a, ll b) {
ll res = 1;
while (b) {
if (b&1) {
res = mult(res, a);
}
a = mult(a, a);
b >>= 1;
}
return res;
}
const int MAXN = 2e5+7;
vector <pll> graph[MAXN];
ll ans = 9e18;
multiset <pll> paths;
ll n, KK;
int used[MAXN], subtree[MAXN];
void dfs(int v, int p) {
subtree[v] = 1;
for (auto to : graph[v]) {
if (used[to.first] || to.first == p) continue;
dfs(to.first, v);
subtree[v] += subtree[to.first];
}
}
int find_centroid(int v, int p, int saizu) {
for (auto to : graph[v]) {
if (used[to.first] || to.first == p) continue;
if (subtree[to.first] * 2 > saizu) {
return find_centroid(to.first, v, saizu);
}
}
return v;
}
void get_paths(int v, int p, ll dist, int len, bool flag) {
if (flag) {
paths.insert(mp(dist, len));
}
else {
paths.erase(paths.find(mp(dist, len)));
}
for (auto to : graph[v]) {
if (used[to.first] || to.first == p) continue;
get_paths(to.first, v, dist+to.second, len+1, flag);
}
}
void calculate(int v, int p, ll dist, int len) {
auto it = paths.lower_bound(mp(KK-dist, 0));
if (it != paths.end() && (*it).first == KK-dist) {
ans = min(ans, len+(*it).second);
}
for (auto to : graph[v]) {
if (used[to.first] || to.first == p) {
continue;
}
calculate(to.first, v, dist+to.second, len+1);
}
}
void centroid_decomposition(int v) {
dfs(v, v);
auto centroid = find_centroid(v, v, subtree[v]);
used[centroid] = 1;
get_paths(centroid, centroid, 0, 0, 1);
for (auto to : graph[centroid]) {
if (used[to.first]) continue;
get_paths(to.first, centroid, to.second, 1, 0);
calculate(to.first, to.first, to.second, 1);
get_paths(to.first, centroid, to.second, 1, 1);
}
get_paths(centroid, centroid, 0, 0, 0);
for (auto to : graph[centroid]) {
if (!used[to.first]) {
centroid_decomposition(to.first);
}
}
}
int best_path(int N, int k, int h[][2], int l[]) {
n = N;
KK = k;
for (int i = 0; i < n-1; i++) {
graph[h[i][0]].pb(mp(h[i][1], l[i]));
graph[h[i][1]].pb(mp(h[i][0], l[i]));
}
centroid_decomposition(1);
return (ans > n ? -1 : ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5092 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4932 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
4 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5004 KB |
Output is correct |
10 |
Correct |
3 ms |
5004 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5008 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5004 KB |
Output is correct |
18 |
Correct |
3 ms |
4960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5092 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4932 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
4 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5004 KB |
Output is correct |
10 |
Correct |
3 ms |
5004 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5008 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5004 KB |
Output is correct |
18 |
Correct |
3 ms |
4960 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
4 ms |
5008 KB |
Output is correct |
21 |
Correct |
7 ms |
5076 KB |
Output is correct |
22 |
Correct |
6 ms |
5076 KB |
Output is correct |
23 |
Correct |
6 ms |
5076 KB |
Output is correct |
24 |
Correct |
6 ms |
5076 KB |
Output is correct |
25 |
Correct |
7 ms |
5144 KB |
Output is correct |
26 |
Correct |
6 ms |
5144 KB |
Output is correct |
27 |
Correct |
5 ms |
5076 KB |
Output is correct |
28 |
Correct |
5 ms |
5076 KB |
Output is correct |
29 |
Correct |
5 ms |
5076 KB |
Output is correct |
30 |
Correct |
5 ms |
5076 KB |
Output is correct |
31 |
Correct |
6 ms |
5076 KB |
Output is correct |
32 |
Correct |
6 ms |
5152 KB |
Output is correct |
33 |
Correct |
6 ms |
5076 KB |
Output is correct |
34 |
Correct |
6 ms |
5148 KB |
Output is correct |
35 |
Correct |
6 ms |
5172 KB |
Output is correct |
36 |
Correct |
7 ms |
5076 KB |
Output is correct |
37 |
Correct |
6 ms |
5076 KB |
Output is correct |
38 |
Correct |
7 ms |
5268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5092 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4932 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
4 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5004 KB |
Output is correct |
10 |
Correct |
3 ms |
5004 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5008 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5004 KB |
Output is correct |
18 |
Correct |
3 ms |
4960 KB |
Output is correct |
19 |
Correct |
871 ms |
19828 KB |
Output is correct |
20 |
Correct |
864 ms |
19664 KB |
Output is correct |
21 |
Correct |
870 ms |
19660 KB |
Output is correct |
22 |
Correct |
734 ms |
19632 KB |
Output is correct |
23 |
Correct |
946 ms |
20212 KB |
Output is correct |
24 |
Correct |
461 ms |
19340 KB |
Output is correct |
25 |
Correct |
790 ms |
26404 KB |
Output is correct |
26 |
Correct |
510 ms |
27116 KB |
Output is correct |
27 |
Correct |
738 ms |
35068 KB |
Output is correct |
28 |
Correct |
1813 ms |
48452 KB |
Output is correct |
29 |
Correct |
1849 ms |
44876 KB |
Output is correct |
30 |
Correct |
752 ms |
35004 KB |
Output is correct |
31 |
Correct |
790 ms |
35216 KB |
Output is correct |
32 |
Correct |
927 ms |
35072 KB |
Output is correct |
33 |
Correct |
1416 ms |
33844 KB |
Output is correct |
34 |
Correct |
1734 ms |
34656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5092 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
4 ms |
4932 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
4948 KB |
Output is correct |
8 |
Correct |
4 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
5004 KB |
Output is correct |
10 |
Correct |
3 ms |
5004 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
5008 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5004 KB |
Output is correct |
18 |
Correct |
3 ms |
4960 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
4 ms |
5008 KB |
Output is correct |
21 |
Correct |
7 ms |
5076 KB |
Output is correct |
22 |
Correct |
6 ms |
5076 KB |
Output is correct |
23 |
Correct |
6 ms |
5076 KB |
Output is correct |
24 |
Correct |
6 ms |
5076 KB |
Output is correct |
25 |
Correct |
7 ms |
5144 KB |
Output is correct |
26 |
Correct |
6 ms |
5144 KB |
Output is correct |
27 |
Correct |
5 ms |
5076 KB |
Output is correct |
28 |
Correct |
5 ms |
5076 KB |
Output is correct |
29 |
Correct |
5 ms |
5076 KB |
Output is correct |
30 |
Correct |
5 ms |
5076 KB |
Output is correct |
31 |
Correct |
6 ms |
5076 KB |
Output is correct |
32 |
Correct |
6 ms |
5152 KB |
Output is correct |
33 |
Correct |
6 ms |
5076 KB |
Output is correct |
34 |
Correct |
6 ms |
5148 KB |
Output is correct |
35 |
Correct |
6 ms |
5172 KB |
Output is correct |
36 |
Correct |
7 ms |
5076 KB |
Output is correct |
37 |
Correct |
6 ms |
5076 KB |
Output is correct |
38 |
Correct |
7 ms |
5268 KB |
Output is correct |
39 |
Correct |
871 ms |
19828 KB |
Output is correct |
40 |
Correct |
864 ms |
19664 KB |
Output is correct |
41 |
Correct |
870 ms |
19660 KB |
Output is correct |
42 |
Correct |
734 ms |
19632 KB |
Output is correct |
43 |
Correct |
946 ms |
20212 KB |
Output is correct |
44 |
Correct |
461 ms |
19340 KB |
Output is correct |
45 |
Correct |
790 ms |
26404 KB |
Output is correct |
46 |
Correct |
510 ms |
27116 KB |
Output is correct |
47 |
Correct |
738 ms |
35068 KB |
Output is correct |
48 |
Correct |
1813 ms |
48452 KB |
Output is correct |
49 |
Correct |
1849 ms |
44876 KB |
Output is correct |
50 |
Correct |
752 ms |
35004 KB |
Output is correct |
51 |
Correct |
790 ms |
35216 KB |
Output is correct |
52 |
Correct |
927 ms |
35072 KB |
Output is correct |
53 |
Correct |
1416 ms |
33844 KB |
Output is correct |
54 |
Correct |
1734 ms |
34656 KB |
Output is correct |
55 |
Correct |
44 ms |
6484 KB |
Output is correct |
56 |
Correct |
58 ms |
6428 KB |
Output is correct |
57 |
Correct |
749 ms |
20360 KB |
Output is correct |
58 |
Correct |
137 ms |
18872 KB |
Output is correct |
59 |
Correct |
579 ms |
27036 KB |
Output is correct |
60 |
Correct |
1693 ms |
44684 KB |
Output is correct |
61 |
Correct |
752 ms |
35392 KB |
Output is correct |
62 |
Correct |
702 ms |
35004 KB |
Output is correct |
63 |
Correct |
826 ms |
35172 KB |
Output is correct |
64 |
Correct |
2133 ms |
35096 KB |
Output is correct |
65 |
Correct |
1758 ms |
35660 KB |
Output is correct |
66 |
Correct |
1699 ms |
47380 KB |
Output is correct |
67 |
Correct |
378 ms |
34260 KB |
Output is correct |
68 |
Correct |
952 ms |
35076 KB |
Output is correct |
69 |
Correct |
873 ms |
35208 KB |
Output is correct |
70 |
Correct |
807 ms |
33636 KB |
Output is correct |