/******************************
* author : @marvinthang *
* date : 15 / 01 / 2022 *
******************************/
#include "race.h"
#include <bits/stdc++.h>
using namespace std;
#define superspeed ios_base::sync_with_stdio(false); cin.tie(nullptr); //cout.tie(nullptr);
#define file(name) if (fopen (name".inp", "r") ) { freopen (name".inp", "r", stdin); freopen (name".out", "w", stdout); }
#define REP(i, a, b) for (__typeof(a) i = (a) - ((a) > (b)); i != (b) - ((a) > (b)); i += ((a) > (b) ? -1 : 1))
#define FOR(i, a, b) for (__typeof(a) i = (a); i != (b) + ((a) > (b) ? -1 : 1); i += ((a) > (b) ? -1 : 1))
template <class U, class V> ostream & operator << (ostream& out, const pair<U, V> &p) { return out << '(' << p.first << ", " << p.second << ')'; }
template <class T> ostream & operator << (ostream &out, const vector<T> &vt) { out << '{'; for (size_t i = 0; i + 1 < vt.size(); i++) out << vt[i] << ", "; if (!vt.empty()) out << vt.back(); return out << '}'; }
void minimize(int &a, int b) { if (a > b) a = b; }
const int MOD = 1e9 + 7;
const double PI = 3.1415926535897932384626433832795; // acos(-1.0); atan(-1.0);
const int dir[] = {0, 1, 0, -1, 0}; // {0, 1, 1, -1, -1, 1, 0, -1, 0};
const long long oo = 1e18;
const int MAX = 2e5 + 5;
int N, K;
vector <pair <int, int>> adj[MAX];
int minPath[MAX * 10], newPath[MAX * 5], res;
vector <int> save, save2;
bool used[MAX];
void calc(int u, int par, int depth, int weight) {
if (weight > K) return;
res = min(res, minPath[K - weight] + depth);
newPath[weight] = min(newPath[weight], depth);
save.push_back(weight);
save2.push_back(weight);
for (pair <int, int> &x: adj[u]) {
int v = x.second, w = x.first;
if (v == par || used[v]) continue;
calc(v, u, depth + 1, weight + w);
}
}
int sizes[MAX];
int calcSize(int u, int par) {
sizes[u] = 1;
for (pair <int, int> &x: adj[u]) {
int v = x.second;
if (v == par || used[v]) continue;
sizes[u] += calcSize(v, u);
}
return sizes[u];
}
int findCentroid(int u, int par, int n) {
for (pair <int, int> &x: adj[u]) {
int v = x.second;
if (v == par || used[v]) continue;
if (sizes[v] > n / 2) return findCentroid(v, u, n);
}
return u;
}
void solve(int u) {
int n = calcSize(u, 0);
int centroid = findCentroid(u, 0, n);
used[centroid] = true;
save.clear();
for (pair <int, int> &x: adj[centroid]) {
int v = x.second, w = x.first;
if (used[v]) continue;
calc(v, centroid, 1, w);
for (int &x: save2) {
minPath[x] = min(minPath[x], newPath[x]);
newPath[x] = N + 1;
}
save2.clear();
}
res = min(res, minPath[K]);
for (int &x: save) minPath[x] = N + 1;
for (pair <int, int> &x: adj[centroid]) {
int v = x.second;
if (!used[v]) solve(v);
}
}
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 u = h[i][0], v = h[i][1], w = l[i];
++u; ++v;
adj[u].push_back( make_pair(w, v) );
adj[v].push_back( make_pair(w, u) );
}
res = 1e9;
fill(minPath, minPath + 1 + K, N + 1);
fill(newPath, newPath + 1 + K, N + 1);
solve(1);
if (res > N) res = -1;
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5012 KB |
Output is correct |
4 |
Correct |
3 ms |
5020 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
4 ms |
5004 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5016 KB |
Output is correct |
14 |
Correct |
3 ms |
5016 KB |
Output is correct |
15 |
Correct |
4 ms |
5016 KB |
Output is correct |
16 |
Correct |
3 ms |
5016 KB |
Output is correct |
17 |
Correct |
3 ms |
5008 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5012 KB |
Output is correct |
4 |
Correct |
3 ms |
5020 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
4 ms |
5004 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5016 KB |
Output is correct |
14 |
Correct |
3 ms |
5016 KB |
Output is correct |
15 |
Correct |
4 ms |
5016 KB |
Output is correct |
16 |
Correct |
3 ms |
5016 KB |
Output is correct |
17 |
Correct |
3 ms |
5008 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5012 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
7 ms |
12244 KB |
Output is correct |
23 |
Correct |
7 ms |
10964 KB |
Output is correct |
24 |
Correct |
9 ms |
11860 KB |
Output is correct |
25 |
Correct |
6 ms |
11604 KB |
Output is correct |
26 |
Correct |
5 ms |
7636 KB |
Output is correct |
27 |
Correct |
7 ms |
11348 KB |
Output is correct |
28 |
Correct |
4 ms |
6612 KB |
Output is correct |
29 |
Correct |
5 ms |
7636 KB |
Output is correct |
30 |
Correct |
5 ms |
7892 KB |
Output is correct |
31 |
Correct |
6 ms |
10196 KB |
Output is correct |
32 |
Correct |
6 ms |
10580 KB |
Output is correct |
33 |
Correct |
7 ms |
11220 KB |
Output is correct |
34 |
Correct |
6 ms |
9684 KB |
Output is correct |
35 |
Correct |
9 ms |
11348 KB |
Output is correct |
36 |
Correct |
10 ms |
12316 KB |
Output is correct |
37 |
Correct |
7 ms |
11348 KB |
Output is correct |
38 |
Correct |
6 ms |
9044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5012 KB |
Output is correct |
4 |
Correct |
3 ms |
5020 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
4 ms |
5004 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5016 KB |
Output is correct |
14 |
Correct |
3 ms |
5016 KB |
Output is correct |
15 |
Correct |
4 ms |
5016 KB |
Output is correct |
16 |
Correct |
3 ms |
5016 KB |
Output is correct |
17 |
Correct |
3 ms |
5008 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
106 ms |
10752 KB |
Output is correct |
20 |
Correct |
104 ms |
10736 KB |
Output is correct |
21 |
Correct |
142 ms |
11104 KB |
Output is correct |
22 |
Correct |
124 ms |
11412 KB |
Output is correct |
23 |
Correct |
77 ms |
10984 KB |
Output is correct |
24 |
Correct |
50 ms |
10804 KB |
Output is correct |
25 |
Correct |
132 ms |
13548 KB |
Output is correct |
26 |
Correct |
92 ms |
17264 KB |
Output is correct |
27 |
Correct |
156 ms |
16224 KB |
Output is correct |
28 |
Correct |
310 ms |
27456 KB |
Output is correct |
29 |
Correct |
231 ms |
26616 KB |
Output is correct |
30 |
Correct |
180 ms |
16300 KB |
Output is correct |
31 |
Correct |
159 ms |
16252 KB |
Output is correct |
32 |
Correct |
192 ms |
16404 KB |
Output is correct |
33 |
Correct |
202 ms |
15156 KB |
Output is correct |
34 |
Correct |
188 ms |
15040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
2 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5012 KB |
Output is correct |
4 |
Correct |
3 ms |
5020 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
4 ms |
5004 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
3 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
5020 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5016 KB |
Output is correct |
14 |
Correct |
3 ms |
5016 KB |
Output is correct |
15 |
Correct |
4 ms |
5016 KB |
Output is correct |
16 |
Correct |
3 ms |
5016 KB |
Output is correct |
17 |
Correct |
3 ms |
5008 KB |
Output is correct |
18 |
Correct |
3 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
5012 KB |
Output is correct |
20 |
Correct |
3 ms |
4948 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
7 ms |
12244 KB |
Output is correct |
23 |
Correct |
7 ms |
10964 KB |
Output is correct |
24 |
Correct |
9 ms |
11860 KB |
Output is correct |
25 |
Correct |
6 ms |
11604 KB |
Output is correct |
26 |
Correct |
5 ms |
7636 KB |
Output is correct |
27 |
Correct |
7 ms |
11348 KB |
Output is correct |
28 |
Correct |
4 ms |
6612 KB |
Output is correct |
29 |
Correct |
5 ms |
7636 KB |
Output is correct |
30 |
Correct |
5 ms |
7892 KB |
Output is correct |
31 |
Correct |
6 ms |
10196 KB |
Output is correct |
32 |
Correct |
6 ms |
10580 KB |
Output is correct |
33 |
Correct |
7 ms |
11220 KB |
Output is correct |
34 |
Correct |
6 ms |
9684 KB |
Output is correct |
35 |
Correct |
9 ms |
11348 KB |
Output is correct |
36 |
Correct |
10 ms |
12316 KB |
Output is correct |
37 |
Correct |
7 ms |
11348 KB |
Output is correct |
38 |
Correct |
6 ms |
9044 KB |
Output is correct |
39 |
Correct |
106 ms |
10752 KB |
Output is correct |
40 |
Correct |
104 ms |
10736 KB |
Output is correct |
41 |
Correct |
142 ms |
11104 KB |
Output is correct |
42 |
Correct |
124 ms |
11412 KB |
Output is correct |
43 |
Correct |
77 ms |
10984 KB |
Output is correct |
44 |
Correct |
50 ms |
10804 KB |
Output is correct |
45 |
Correct |
132 ms |
13548 KB |
Output is correct |
46 |
Correct |
92 ms |
17264 KB |
Output is correct |
47 |
Correct |
156 ms |
16224 KB |
Output is correct |
48 |
Correct |
310 ms |
27456 KB |
Output is correct |
49 |
Correct |
231 ms |
26616 KB |
Output is correct |
50 |
Correct |
180 ms |
16300 KB |
Output is correct |
51 |
Correct |
159 ms |
16252 KB |
Output is correct |
52 |
Correct |
192 ms |
16404 KB |
Output is correct |
53 |
Correct |
202 ms |
15156 KB |
Output is correct |
54 |
Correct |
188 ms |
15040 KB |
Output is correct |
55 |
Correct |
11 ms |
5796 KB |
Output is correct |
56 |
Correct |
11 ms |
5796 KB |
Output is correct |
57 |
Correct |
78 ms |
11904 KB |
Output is correct |
58 |
Correct |
37 ms |
11864 KB |
Output is correct |
59 |
Correct |
96 ms |
18660 KB |
Output is correct |
60 |
Correct |
405 ms |
36124 KB |
Output is correct |
61 |
Correct |
158 ms |
16624 KB |
Output is correct |
62 |
Correct |
196 ms |
25040 KB |
Output is correct |
63 |
Correct |
232 ms |
25040 KB |
Output is correct |
64 |
Correct |
365 ms |
21432 KB |
Output is correct |
65 |
Correct |
199 ms |
16172 KB |
Output is correct |
66 |
Correct |
346 ms |
32428 KB |
Output is correct |
67 |
Correct |
134 ms |
26144 KB |
Output is correct |
68 |
Correct |
219 ms |
24832 KB |
Output is correct |
69 |
Correct |
228 ms |
24712 KB |
Output is correct |
70 |
Correct |
234 ms |
24220 KB |
Output is correct |