#include<bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<ll, null_type, less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>
// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
typedef long long ll;
typedef long double ldb;
typedef vector<int> vi;
typedef vector<long long> vl;
typedef vector<double> vdb;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvl;
typedef vector<string> vs;
typedef set<int> si;
typedef set<long long> sl;
typedef set<double> sdb;
typedef set<string> ss;
typedef set<char> sc;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ftb(i, a, b) for (int i = a, _b = b; i <= _b; ++i)
#define ft(i, a, b) for (int i = a, _b = b; i < _b; ++i)
#define fgb(i, a, b) for (int i = a, _b = b; i >= _b; --i)
#define fg(i, a, b) for (int i = a, _b = b; i > _b; --i)
#define endl "\n"
const int N = 2*1e5;
const int K = 1e6;
int subtree[N + 1];
int dis[K + 1];
bool check[N + 1];
vector<pii> adj[N + 1];
int sz = 0;
void find_subtree(int node, int parent) {
subtree[node] = 1;
for (pii it : adj[node]) {
if (it.first != parent && !check[it.first]) {
find_subtree(it.first, node);
subtree[node] += subtree[it.first];
}
}
}
int centroid(int node, int parent) {
for (pii it : adj[node]) {
if (it.first != parent && !check[it.first]) {
if (subtree[it.first] > sz / 2) {
return centroid(it.first, node);
}
}
}
return node;
}
void dfs(int node, int type, int parent, int depth, int length, int k, int& ans) {
if (length > k) return;
if (type == 3) {
dis[length] = 1e9;
for (pii it : adj[node]) {
if (it.first != parent && !check[it.first]) {
dfs(it.first, type, node, depth + 1, length + it.second, k, ans);
}
}
}
else if (type == 2) {
dis[length] = min(dis[length], depth);
for (pii it : adj[node]) {
if (it.first != parent && !check[it.first]) {
dfs(it.first, type, node, depth + 1, length + it.second, k, ans);
}
}
}
else {
ans = min(ans, depth + dis[k - length]);
for (pii it : adj[node]) {
if (it.first != parent && !check[it.first]) {
dfs(it.first, type, node, depth + 1, length + it.second, k, ans);
}
}
}
}
void solve(int i, int& ans, int k) {
find_subtree(i, -1);
sz = subtree[i];
int node = centroid(i, -1);
check[node] = true;
dis[0] = 0;
for (pii it : adj[node]) {
if (check[it.first]) continue;
dfs(it.first, 1, -1, 1, it.second, k, ans);
dfs(it.first, 2, -1, 1, it.second, k, ans);
}
dfs(node, 3, -1, 0, 0, k, ans);
for (pii it : adj[node]) {
if (!check[it.first]) solve(it.first, ans, k);
}
}
int best_path(int n, int k, int h[][2], int l[]) {
ftb(i, 0, k) {
dis[i] = 1e9;
}
ft(i, 0, n - 1) {
adj[h[i][0]].push_back({ h[i][1],l[i] });
adj[h[i][1]].push_back({ h[i][0],l[i] });
}
int ans = 1e9;
solve(0, ans, k);
if (ans == 1e9) ans = -1;
return ans;
}
// int main() {
// int n, k;
// cin >> n >> k;
// int h[n - 1][2];
// int l[n - 1];
// ft(i, 0, n - 1) {
// int x, y, z;
// cin >> x >> y >> z;
// h[i][0] = x;
// h[i][1] = y;
// l[i] = z;
// }
// cout << best_path(n, k, h, l);
// }
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10700 KB |
Output is correct |
4 |
Correct |
2 ms |
10704 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10704 KB |
Output is correct |
7 |
Correct |
2 ms |
10700 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10592 KB |
Output is correct |
16 |
Correct |
2 ms |
10592 KB |
Output is correct |
17 |
Correct |
2 ms |
10592 KB |
Output is correct |
18 |
Correct |
2 ms |
10592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10700 KB |
Output is correct |
4 |
Correct |
2 ms |
10704 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10704 KB |
Output is correct |
7 |
Correct |
2 ms |
10700 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10592 KB |
Output is correct |
16 |
Correct |
2 ms |
10592 KB |
Output is correct |
17 |
Correct |
2 ms |
10592 KB |
Output is correct |
18 |
Correct |
2 ms |
10592 KB |
Output is correct |
19 |
Correct |
2 ms |
10688 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
12640 KB |
Output is correct |
23 |
Correct |
3 ms |
12636 KB |
Output is correct |
24 |
Correct |
3 ms |
12636 KB |
Output is correct |
25 |
Correct |
3 ms |
12636 KB |
Output is correct |
26 |
Correct |
3 ms |
10716 KB |
Output is correct |
27 |
Correct |
3 ms |
12636 KB |
Output is correct |
28 |
Correct |
4 ms |
10704 KB |
Output is correct |
29 |
Correct |
2 ms |
10692 KB |
Output is correct |
30 |
Correct |
3 ms |
10592 KB |
Output is correct |
31 |
Correct |
3 ms |
12640 KB |
Output is correct |
32 |
Correct |
3 ms |
12736 KB |
Output is correct |
33 |
Correct |
3 ms |
12648 KB |
Output is correct |
34 |
Correct |
4 ms |
12784 KB |
Output is correct |
35 |
Correct |
3 ms |
12740 KB |
Output is correct |
36 |
Correct |
3 ms |
12648 KB |
Output is correct |
37 |
Correct |
3 ms |
12648 KB |
Output is correct |
38 |
Correct |
3 ms |
12648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10700 KB |
Output is correct |
4 |
Correct |
2 ms |
10704 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10704 KB |
Output is correct |
7 |
Correct |
2 ms |
10700 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10592 KB |
Output is correct |
16 |
Correct |
2 ms |
10592 KB |
Output is correct |
17 |
Correct |
2 ms |
10592 KB |
Output is correct |
18 |
Correct |
2 ms |
10592 KB |
Output is correct |
19 |
Correct |
100 ms |
18296 KB |
Output is correct |
20 |
Correct |
125 ms |
18312 KB |
Output is correct |
21 |
Correct |
90 ms |
18260 KB |
Output is correct |
22 |
Correct |
84 ms |
18264 KB |
Output is correct |
23 |
Correct |
52 ms |
18652 KB |
Output is correct |
24 |
Correct |
39 ms |
18412 KB |
Output is correct |
25 |
Correct |
89 ms |
21844 KB |
Output is correct |
26 |
Correct |
79 ms |
25604 KB |
Output is correct |
27 |
Correct |
109 ms |
24148 KB |
Output is correct |
28 |
Correct |
154 ms |
38480 KB |
Output is correct |
29 |
Correct |
153 ms |
37460 KB |
Output is correct |
30 |
Correct |
106 ms |
24148 KB |
Output is correct |
31 |
Correct |
124 ms |
24144 KB |
Output is correct |
32 |
Correct |
115 ms |
24292 KB |
Output is correct |
33 |
Correct |
145 ms |
22852 KB |
Output is correct |
34 |
Correct |
117 ms |
24040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10588 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10700 KB |
Output is correct |
4 |
Correct |
2 ms |
10704 KB |
Output is correct |
5 |
Correct |
2 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10704 KB |
Output is correct |
7 |
Correct |
2 ms |
10700 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
2 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10588 KB |
Output is correct |
12 |
Correct |
2 ms |
10588 KB |
Output is correct |
13 |
Correct |
2 ms |
10588 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
2 ms |
10592 KB |
Output is correct |
16 |
Correct |
2 ms |
10592 KB |
Output is correct |
17 |
Correct |
2 ms |
10592 KB |
Output is correct |
18 |
Correct |
2 ms |
10592 KB |
Output is correct |
19 |
Correct |
2 ms |
10688 KB |
Output is correct |
20 |
Correct |
2 ms |
10588 KB |
Output is correct |
21 |
Correct |
2 ms |
10588 KB |
Output is correct |
22 |
Correct |
3 ms |
12640 KB |
Output is correct |
23 |
Correct |
3 ms |
12636 KB |
Output is correct |
24 |
Correct |
3 ms |
12636 KB |
Output is correct |
25 |
Correct |
3 ms |
12636 KB |
Output is correct |
26 |
Correct |
3 ms |
10716 KB |
Output is correct |
27 |
Correct |
3 ms |
12636 KB |
Output is correct |
28 |
Correct |
4 ms |
10704 KB |
Output is correct |
29 |
Correct |
2 ms |
10692 KB |
Output is correct |
30 |
Correct |
3 ms |
10592 KB |
Output is correct |
31 |
Correct |
3 ms |
12640 KB |
Output is correct |
32 |
Correct |
3 ms |
12736 KB |
Output is correct |
33 |
Correct |
3 ms |
12648 KB |
Output is correct |
34 |
Correct |
4 ms |
12784 KB |
Output is correct |
35 |
Correct |
3 ms |
12740 KB |
Output is correct |
36 |
Correct |
3 ms |
12648 KB |
Output is correct |
37 |
Correct |
3 ms |
12648 KB |
Output is correct |
38 |
Correct |
3 ms |
12648 KB |
Output is correct |
39 |
Correct |
100 ms |
18296 KB |
Output is correct |
40 |
Correct |
125 ms |
18312 KB |
Output is correct |
41 |
Correct |
90 ms |
18260 KB |
Output is correct |
42 |
Correct |
84 ms |
18264 KB |
Output is correct |
43 |
Correct |
52 ms |
18652 KB |
Output is correct |
44 |
Correct |
39 ms |
18412 KB |
Output is correct |
45 |
Correct |
89 ms |
21844 KB |
Output is correct |
46 |
Correct |
79 ms |
25604 KB |
Output is correct |
47 |
Correct |
109 ms |
24148 KB |
Output is correct |
48 |
Correct |
154 ms |
38480 KB |
Output is correct |
49 |
Correct |
153 ms |
37460 KB |
Output is correct |
50 |
Correct |
106 ms |
24148 KB |
Output is correct |
51 |
Correct |
124 ms |
24144 KB |
Output is correct |
52 |
Correct |
115 ms |
24292 KB |
Output is correct |
53 |
Correct |
145 ms |
22852 KB |
Output is correct |
54 |
Correct |
117 ms |
24040 KB |
Output is correct |
55 |
Correct |
8 ms |
11120 KB |
Output is correct |
56 |
Correct |
9 ms |
11272 KB |
Output is correct |
57 |
Correct |
62 ms |
18516 KB |
Output is correct |
58 |
Correct |
28 ms |
18116 KB |
Output is correct |
59 |
Correct |
78 ms |
25476 KB |
Output is correct |
60 |
Correct |
307 ms |
40016 KB |
Output is correct |
61 |
Correct |
138 ms |
24240 KB |
Output is correct |
62 |
Correct |
135 ms |
26448 KB |
Output is correct |
63 |
Correct |
222 ms |
26452 KB |
Output is correct |
64 |
Correct |
261 ms |
26448 KB |
Output is correct |
65 |
Correct |
158 ms |
24916 KB |
Output is correct |
66 |
Correct |
265 ms |
37240 KB |
Output is correct |
67 |
Correct |
86 ms |
27088 KB |
Output is correct |
68 |
Correct |
147 ms |
27192 KB |
Output is correct |
69 |
Correct |
157 ms |
27116 KB |
Output is correct |
70 |
Correct |
162 ms |
26708 KB |
Output is correct |