#include "race.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef pair<ll, ll> ii;
typedef vector<ii> vii;
ll n, k;
vector<set<ii>> al;
vi size;
vi attached;
vi pathmin;
vi pathminbk;
ll pmbk = 0;
vii paths;
ll mink = INT_MAX;
void calcsize(ll u, ll p) {
// cout << "calcsize" << " " << u << " " << p << endl;
attached.emplace_back(u);
size[u] = 1;
for (auto &[v, w]:al[u]) if (v != p) {
calcsize(v, u);
size[u] += size[v];
}
}
ll centoid(ll s) {
attached.clear();
calcsize(s, -1);
ll ans = s;
ll mimasub = size[s];
for (auto &u:attached) {
ll masub = attached.size() - size[u];
for (auto &[v, _]:al[u]) if (size[v] < size[u]) {
masub = max(masub, size[v]);
}
if (masub < mimasub) {
ans = u;
mimasub = masub;
}
}
return ans;
}
void dfs1(ll u, ll p, ll nume, ll lene) {
if (lene > k) return;
paths.emplace_back(nume, lene);
for (auto &[v,w]:al[u]) if (v != p) {
dfs1(v, u, nume+1, lene+w);
}
}
void calcpaths(ll s) {
ll u = centoid(s);
// cout << "centoid is " << u << endl;
for (auto &[v, w]:al[u]) {
paths.clear();
dfs1(v, u, 1, w);
for (auto &[nume, lene]:paths) {
if (pathminbk[k-lene] == pmbk) {
// if (pathmin[k-lene] + nume < mink) cout << "CHANGED k = " << k << " lene = " << lene << endl;
mink = min(mink, pathmin[k-lene] + nume);
}
}
for (auto &[nume, lene]:paths) {
if (pathminbk[lene] != pmbk) {
pathmin[lene] = nume;
pathminbk[lene] = pmbk;
}
pathmin[lene] = min(pathmin[lene], nume);
}
}
for (auto &[v, w]:al[u]) {
al[v].erase(ii(u, w));
pmbk++;
pathmin[0] = 0;
pathminbk[0] = pmbk;
calcpaths(v);
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N;
k = K;
pathmin.assign(k+10, INT_MAX);
pathmin[0] = 0;
pathminbk.assign(k+10, 0);
size.assign(n, 0);
al.resize(n);
for (int i=0;i<n-1;i++) {
ll u = H[i][0];
ll v = H[i][1];
al[u].emplace(v, L[i]);
al[v].emplace(u, L[i]);
}
calcpaths(0);
// for (int i=0;i<=k;i++) {
// cout << "k = " << i << ": " << pathmin[i] << endl;
// }
// cout << mink << endl;
if (mink > n-1) return -1;
return mink;
}
Compilation message
race.cpp: In function 'void calcsize(ll, ll)':
race.cpp:26:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
26 | for (auto &[v, w]:al[u]) if (v != p) {
| ^
race.cpp: In function 'll centoid(ll)':
race.cpp:39:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
39 | for (auto &[v, _]:al[u]) if (size[v] < size[u]) {
| ^
race.cpp: In function 'void dfs1(ll, ll, ll, ll)':
race.cpp:54:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
54 | for (auto &[v,w]:al[u]) if (v != p) {
| ^
race.cpp: In function 'void calcpaths(ll)':
race.cpp:62:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
62 | for (auto &[v, w]:al[u]) {
| ^
race.cpp:65:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
65 | for (auto &[nume, lene]:paths) {
| ^
race.cpp:72:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
72 | for (auto &[nume, lene]:paths) {
| ^
race.cpp:81:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
81 | for (auto &[v, w]:al[u]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2648 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2648 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2488 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
5 ms |
17064 KB |
Output is correct |
23 |
Correct |
4 ms |
14428 KB |
Output is correct |
24 |
Correct |
4 ms |
16076 KB |
Output is correct |
25 |
Correct |
4 ms |
15704 KB |
Output is correct |
26 |
Correct |
3 ms |
7772 KB |
Output is correct |
27 |
Correct |
4 ms |
15196 KB |
Output is correct |
28 |
Correct |
2 ms |
5724 KB |
Output is correct |
29 |
Correct |
3 ms |
7772 KB |
Output is correct |
30 |
Correct |
3 ms |
8284 KB |
Output is correct |
31 |
Correct |
4 ms |
12892 KB |
Output is correct |
32 |
Correct |
4 ms |
13608 KB |
Output is correct |
33 |
Correct |
4 ms |
14940 KB |
Output is correct |
34 |
Correct |
4 ms |
11868 KB |
Output is correct |
35 |
Correct |
4 ms |
15196 KB |
Output is correct |
36 |
Correct |
4 ms |
17244 KB |
Output is correct |
37 |
Correct |
5 ms |
14940 KB |
Output is correct |
38 |
Correct |
3 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2648 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2488 KB |
Output is correct |
19 |
Correct |
150 ms |
25156 KB |
Output is correct |
20 |
Correct |
138 ms |
25040 KB |
Output is correct |
21 |
Correct |
225 ms |
25828 KB |
Output is correct |
22 |
Correct |
141 ms |
25732 KB |
Output is correct |
23 |
Correct |
107 ms |
25300 KB |
Output is correct |
24 |
Correct |
88 ms |
25300 KB |
Output is correct |
25 |
Correct |
147 ms |
27604 KB |
Output is correct |
26 |
Correct |
89 ms |
30924 KB |
Output is correct |
27 |
Correct |
252 ms |
45472 KB |
Output is correct |
28 |
Correct |
328 ms |
54832 KB |
Output is correct |
29 |
Correct |
342 ms |
54112 KB |
Output is correct |
30 |
Correct |
268 ms |
45508 KB |
Output is correct |
31 |
Correct |
258 ms |
45488 KB |
Output is correct |
32 |
Correct |
316 ms |
45596 KB |
Output is correct |
33 |
Correct |
307 ms |
45512 KB |
Output is correct |
34 |
Correct |
260 ms |
46536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2648 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2488 KB |
Output is correct |
19 |
Correct |
1 ms |
2392 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
5 ms |
17064 KB |
Output is correct |
23 |
Correct |
4 ms |
14428 KB |
Output is correct |
24 |
Correct |
4 ms |
16076 KB |
Output is correct |
25 |
Correct |
4 ms |
15704 KB |
Output is correct |
26 |
Correct |
3 ms |
7772 KB |
Output is correct |
27 |
Correct |
4 ms |
15196 KB |
Output is correct |
28 |
Correct |
2 ms |
5724 KB |
Output is correct |
29 |
Correct |
3 ms |
7772 KB |
Output is correct |
30 |
Correct |
3 ms |
8284 KB |
Output is correct |
31 |
Correct |
4 ms |
12892 KB |
Output is correct |
32 |
Correct |
4 ms |
13608 KB |
Output is correct |
33 |
Correct |
4 ms |
14940 KB |
Output is correct |
34 |
Correct |
4 ms |
11868 KB |
Output is correct |
35 |
Correct |
4 ms |
15196 KB |
Output is correct |
36 |
Correct |
4 ms |
17244 KB |
Output is correct |
37 |
Correct |
5 ms |
14940 KB |
Output is correct |
38 |
Correct |
3 ms |
10588 KB |
Output is correct |
39 |
Correct |
150 ms |
25156 KB |
Output is correct |
40 |
Correct |
138 ms |
25040 KB |
Output is correct |
41 |
Correct |
225 ms |
25828 KB |
Output is correct |
42 |
Correct |
141 ms |
25732 KB |
Output is correct |
43 |
Correct |
107 ms |
25300 KB |
Output is correct |
44 |
Correct |
88 ms |
25300 KB |
Output is correct |
45 |
Correct |
147 ms |
27604 KB |
Output is correct |
46 |
Correct |
89 ms |
30924 KB |
Output is correct |
47 |
Correct |
252 ms |
45472 KB |
Output is correct |
48 |
Correct |
328 ms |
54832 KB |
Output is correct |
49 |
Correct |
342 ms |
54112 KB |
Output is correct |
50 |
Correct |
268 ms |
45508 KB |
Output is correct |
51 |
Correct |
258 ms |
45488 KB |
Output is correct |
52 |
Correct |
316 ms |
45596 KB |
Output is correct |
53 |
Correct |
307 ms |
45512 KB |
Output is correct |
54 |
Correct |
260 ms |
46536 KB |
Output is correct |
55 |
Correct |
12 ms |
4768 KB |
Output is correct |
56 |
Correct |
10 ms |
4548 KB |
Output is correct |
57 |
Correct |
82 ms |
26336 KB |
Output is correct |
58 |
Correct |
58 ms |
24784 KB |
Output is correct |
59 |
Correct |
94 ms |
33736 KB |
Output is correct |
60 |
Correct |
462 ms |
72676 KB |
Output is correct |
61 |
Correct |
265 ms |
45836 KB |
Output is correct |
62 |
Correct |
297 ms |
61244 KB |
Output is correct |
63 |
Correct |
368 ms |
61128 KB |
Output is correct |
64 |
Correct |
518 ms |
56996 KB |
Output is correct |
65 |
Correct |
325 ms |
46700 KB |
Output is correct |
66 |
Correct |
400 ms |
68448 KB |
Output is correct |
67 |
Correct |
314 ms |
61840 KB |
Output is correct |
68 |
Correct |
390 ms |
61116 KB |
Output is correct |
69 |
Correct |
469 ms |
61644 KB |
Output is correct |
70 |
Correct |
368 ms |
59448 KB |
Output is correct |