#include "bits/stdc++.h"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define FORD(i, a, b) for(int i = a; i >= b; i--)
#define REP(i, b) for(int i = 0; i < b; i++)
#define PER(i, b) for(int i = b - 1; i >= 0; i--)
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#ifdef JASPER2
#include "debug.h"
#else
#define debug(...) 166
#endif
//#define int long long
using pii = pair < int, int >;
const ll LINF = 1e18 + 5;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const int MAX = 2e5 + 5;
int n, k;
vector <pii> adj[MAX];
int cnt[MAX * 5];
int sz[MAX];
bool vis[MAX];
int Tsize = 0;
void reSubsize(int u, int p) {
sz[u] = 1;
for (auto &[w, v] : adj[u]) {
if (v != p && !vis[v]) {
reSubsize(v, u);
sz[u] += sz[v];
}
}
}
int getCentroid(int u, int p) {
for (auto &[w, v] : adj[u])
if (v != p && !vis[v] && sz[v] > Tsize / 2)
return getCentroid(v, u);
return u;
}
int f[MAX * 5];
// f(i) : Minimum edge to form i length from subtree to current centroid (0 in sub;
int maxW, ans;
int nCur;
vector <pii> subtree;
void process(int u, int p, int eCnt, int W) {
if (W > k)
return;
if (k >= W && cnt[k - W] == nCur)
ans = min(ans, eCnt + f[k - W]);
subtree.push_back({eCnt, W});
for (auto &[w, v] : adj[u]) {
if (v != p && !vis[v]) {
process(v, u, eCnt + 1, W + w);
}
}
}
void decompose(int u, int p) {
reSubsize(u, 0);
Tsize = sz[u];
int x = getCentroid(u, 0);
++nCur;
cnt[0] = nCur;
debug(nCur);
f[0] = 0;
vis[x] = 1;
vector <pii> valid;
for (auto [w, v] : adj[x]) {
subtree.clear();
if (!vis[v]) {
process(v, x, 1, w);
valid.push_back({w, v});
}
for (auto [eCnt, W] : subtree) {
if (cnt[W] != nCur || (cnt[W] == nCur && f[W] > eCnt)) {
cnt[W] = nCur;
f[W] = eCnt;
}
}
}
for (auto [w, v] : valid) {
decompose(v, x);
}
}
int N, K;
int H[MAX][2];
int L[MAX];
int best_path(int N, int K, int a[][2], int W[]) {
cin >> n >> k;
n = N, k = K;
for (int i = 0; i < n; ++i) {
int u = a[i][0], v = a[i][1], w = W[i];
++u, ++v;
adj[u].push_back({w, v});
adj[v].push_back({w, u});
maxW += w;
}
maxW = min(k, maxW);
ans = INF;
for (int i = 0; i <= k; ++i) f[i] = INF;
decompose(1, 0);
if (ans == INF) ans = -1;
return ans;
}
Compilation message
race.cpp: In function 'void decompose(int, int)':
race.cpp:16:24: warning: statement has no effect [-Wunused-value]
16 | #define debug(...) 166
| ^~~
race.cpp:76:9: note: in expansion of macro 'debug'
76 | debug(nCur);
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16044 KB |
Output is correct |
2 |
Correct |
4 ms |
15708 KB |
Output is correct |
3 |
Correct |
3 ms |
15828 KB |
Output is correct |
4 |
Correct |
3 ms |
15708 KB |
Output is correct |
5 |
Correct |
4 ms |
15708 KB |
Output is correct |
6 |
Correct |
4 ms |
15708 KB |
Output is correct |
7 |
Correct |
4 ms |
15708 KB |
Output is correct |
8 |
Correct |
3 ms |
15708 KB |
Output is correct |
9 |
Correct |
4 ms |
15708 KB |
Output is correct |
10 |
Correct |
3 ms |
15704 KB |
Output is correct |
11 |
Correct |
3 ms |
15708 KB |
Output is correct |
12 |
Correct |
3 ms |
15708 KB |
Output is correct |
13 |
Correct |
3 ms |
15708 KB |
Output is correct |
14 |
Correct |
3 ms |
15708 KB |
Output is correct |
15 |
Correct |
3 ms |
15708 KB |
Output is correct |
16 |
Correct |
3 ms |
15708 KB |
Output is correct |
17 |
Correct |
4 ms |
15732 KB |
Output is correct |
18 |
Correct |
3 ms |
15704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16044 KB |
Output is correct |
2 |
Correct |
4 ms |
15708 KB |
Output is correct |
3 |
Correct |
3 ms |
15828 KB |
Output is correct |
4 |
Correct |
3 ms |
15708 KB |
Output is correct |
5 |
Correct |
4 ms |
15708 KB |
Output is correct |
6 |
Correct |
4 ms |
15708 KB |
Output is correct |
7 |
Correct |
4 ms |
15708 KB |
Output is correct |
8 |
Correct |
3 ms |
15708 KB |
Output is correct |
9 |
Correct |
4 ms |
15708 KB |
Output is correct |
10 |
Correct |
3 ms |
15704 KB |
Output is correct |
11 |
Correct |
3 ms |
15708 KB |
Output is correct |
12 |
Correct |
3 ms |
15708 KB |
Output is correct |
13 |
Correct |
3 ms |
15708 KB |
Output is correct |
14 |
Correct |
3 ms |
15708 KB |
Output is correct |
15 |
Correct |
3 ms |
15708 KB |
Output is correct |
16 |
Correct |
3 ms |
15708 KB |
Output is correct |
17 |
Correct |
4 ms |
15732 KB |
Output is correct |
18 |
Correct |
3 ms |
15704 KB |
Output is correct |
19 |
Correct |
3 ms |
15704 KB |
Output is correct |
20 |
Correct |
3 ms |
15708 KB |
Output is correct |
21 |
Correct |
3 ms |
15708 KB |
Output is correct |
22 |
Correct |
4 ms |
19292 KB |
Output is correct |
23 |
Correct |
5 ms |
19036 KB |
Output is correct |
24 |
Correct |
4 ms |
19548 KB |
Output is correct |
25 |
Correct |
4 ms |
18524 KB |
Output is correct |
26 |
Correct |
4 ms |
17756 KB |
Output is correct |
27 |
Correct |
4 ms |
17756 KB |
Output is correct |
28 |
Correct |
5 ms |
17756 KB |
Output is correct |
29 |
Correct |
4 ms |
17756 KB |
Output is correct |
30 |
Correct |
4 ms |
17752 KB |
Output is correct |
31 |
Correct |
4 ms |
18732 KB |
Output is correct |
32 |
Correct |
4 ms |
18780 KB |
Output is correct |
33 |
Correct |
4 ms |
19376 KB |
Output is correct |
34 |
Correct |
4 ms |
18524 KB |
Output is correct |
35 |
Correct |
4 ms |
19292 KB |
Output is correct |
36 |
Correct |
5 ms |
19548 KB |
Output is correct |
37 |
Correct |
4 ms |
18524 KB |
Output is correct |
38 |
Correct |
4 ms |
17756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16044 KB |
Output is correct |
2 |
Correct |
4 ms |
15708 KB |
Output is correct |
3 |
Correct |
3 ms |
15828 KB |
Output is correct |
4 |
Correct |
3 ms |
15708 KB |
Output is correct |
5 |
Correct |
4 ms |
15708 KB |
Output is correct |
6 |
Correct |
4 ms |
15708 KB |
Output is correct |
7 |
Correct |
4 ms |
15708 KB |
Output is correct |
8 |
Correct |
3 ms |
15708 KB |
Output is correct |
9 |
Correct |
4 ms |
15708 KB |
Output is correct |
10 |
Correct |
3 ms |
15704 KB |
Output is correct |
11 |
Correct |
3 ms |
15708 KB |
Output is correct |
12 |
Correct |
3 ms |
15708 KB |
Output is correct |
13 |
Correct |
3 ms |
15708 KB |
Output is correct |
14 |
Correct |
3 ms |
15708 KB |
Output is correct |
15 |
Correct |
3 ms |
15708 KB |
Output is correct |
16 |
Correct |
3 ms |
15708 KB |
Output is correct |
17 |
Correct |
4 ms |
15732 KB |
Output is correct |
18 |
Correct |
3 ms |
15704 KB |
Output is correct |
19 |
Correct |
76 ms |
21596 KB |
Output is correct |
20 |
Correct |
80 ms |
21784 KB |
Output is correct |
21 |
Correct |
77 ms |
22100 KB |
Output is correct |
22 |
Correct |
70 ms |
22104 KB |
Output is correct |
23 |
Correct |
49 ms |
21864 KB |
Output is correct |
24 |
Correct |
39 ms |
21840 KB |
Output is correct |
25 |
Correct |
82 ms |
24488 KB |
Output is correct |
26 |
Correct |
69 ms |
27872 KB |
Output is correct |
27 |
Correct |
109 ms |
25612 KB |
Output is correct |
28 |
Correct |
187 ms |
36948 KB |
Output is correct |
29 |
Correct |
178 ms |
35700 KB |
Output is correct |
30 |
Correct |
106 ms |
25600 KB |
Output is correct |
31 |
Correct |
121 ms |
25428 KB |
Output is correct |
32 |
Correct |
177 ms |
25424 KB |
Output is correct |
33 |
Correct |
127 ms |
24400 KB |
Output is correct |
34 |
Correct |
115 ms |
24424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
16044 KB |
Output is correct |
2 |
Correct |
4 ms |
15708 KB |
Output is correct |
3 |
Correct |
3 ms |
15828 KB |
Output is correct |
4 |
Correct |
3 ms |
15708 KB |
Output is correct |
5 |
Correct |
4 ms |
15708 KB |
Output is correct |
6 |
Correct |
4 ms |
15708 KB |
Output is correct |
7 |
Correct |
4 ms |
15708 KB |
Output is correct |
8 |
Correct |
3 ms |
15708 KB |
Output is correct |
9 |
Correct |
4 ms |
15708 KB |
Output is correct |
10 |
Correct |
3 ms |
15704 KB |
Output is correct |
11 |
Correct |
3 ms |
15708 KB |
Output is correct |
12 |
Correct |
3 ms |
15708 KB |
Output is correct |
13 |
Correct |
3 ms |
15708 KB |
Output is correct |
14 |
Correct |
3 ms |
15708 KB |
Output is correct |
15 |
Correct |
3 ms |
15708 KB |
Output is correct |
16 |
Correct |
3 ms |
15708 KB |
Output is correct |
17 |
Correct |
4 ms |
15732 KB |
Output is correct |
18 |
Correct |
3 ms |
15704 KB |
Output is correct |
19 |
Correct |
3 ms |
15704 KB |
Output is correct |
20 |
Correct |
3 ms |
15708 KB |
Output is correct |
21 |
Correct |
3 ms |
15708 KB |
Output is correct |
22 |
Correct |
4 ms |
19292 KB |
Output is correct |
23 |
Correct |
5 ms |
19036 KB |
Output is correct |
24 |
Correct |
4 ms |
19548 KB |
Output is correct |
25 |
Correct |
4 ms |
18524 KB |
Output is correct |
26 |
Correct |
4 ms |
17756 KB |
Output is correct |
27 |
Correct |
4 ms |
17756 KB |
Output is correct |
28 |
Correct |
5 ms |
17756 KB |
Output is correct |
29 |
Correct |
4 ms |
17756 KB |
Output is correct |
30 |
Correct |
4 ms |
17752 KB |
Output is correct |
31 |
Correct |
4 ms |
18732 KB |
Output is correct |
32 |
Correct |
4 ms |
18780 KB |
Output is correct |
33 |
Correct |
4 ms |
19376 KB |
Output is correct |
34 |
Correct |
4 ms |
18524 KB |
Output is correct |
35 |
Correct |
4 ms |
19292 KB |
Output is correct |
36 |
Correct |
5 ms |
19548 KB |
Output is correct |
37 |
Correct |
4 ms |
18524 KB |
Output is correct |
38 |
Correct |
4 ms |
17756 KB |
Output is correct |
39 |
Correct |
76 ms |
21596 KB |
Output is correct |
40 |
Correct |
80 ms |
21784 KB |
Output is correct |
41 |
Correct |
77 ms |
22100 KB |
Output is correct |
42 |
Correct |
70 ms |
22104 KB |
Output is correct |
43 |
Correct |
49 ms |
21864 KB |
Output is correct |
44 |
Correct |
39 ms |
21840 KB |
Output is correct |
45 |
Correct |
82 ms |
24488 KB |
Output is correct |
46 |
Correct |
69 ms |
27872 KB |
Output is correct |
47 |
Correct |
109 ms |
25612 KB |
Output is correct |
48 |
Correct |
187 ms |
36948 KB |
Output is correct |
49 |
Correct |
178 ms |
35700 KB |
Output is correct |
50 |
Correct |
106 ms |
25600 KB |
Output is correct |
51 |
Correct |
121 ms |
25428 KB |
Output is correct |
52 |
Correct |
177 ms |
25424 KB |
Output is correct |
53 |
Correct |
127 ms |
24400 KB |
Output is correct |
54 |
Correct |
115 ms |
24424 KB |
Output is correct |
55 |
Correct |
9 ms |
16220 KB |
Output is correct |
56 |
Correct |
9 ms |
16220 KB |
Output is correct |
57 |
Correct |
58 ms |
23000 KB |
Output is correct |
58 |
Correct |
30 ms |
23492 KB |
Output is correct |
59 |
Correct |
77 ms |
31144 KB |
Output is correct |
60 |
Correct |
292 ms |
42712 KB |
Output is correct |
61 |
Correct |
113 ms |
25440 KB |
Output is correct |
62 |
Correct |
116 ms |
27524 KB |
Output is correct |
63 |
Correct |
179 ms |
29672 KB |
Output is correct |
64 |
Correct |
233 ms |
29384 KB |
Output is correct |
65 |
Correct |
134 ms |
25424 KB |
Output is correct |
66 |
Correct |
229 ms |
37856 KB |
Output is correct |
67 |
Correct |
80 ms |
32356 KB |
Output is correct |
68 |
Correct |
162 ms |
29416 KB |
Output is correct |
69 |
Correct |
157 ms |
29136 KB |
Output is correct |
70 |
Correct |
133 ms |
28500 KB |
Output is correct |