#include "race.h"
#include <unordered_map>
#include <vector>
#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
vector<pair<ll, ll>> graf[200005];
ll mp[2000005];
bool rmv[200005];
ll sz[200005];
ll rez = -1;
ll k = 0;
ll sbsz(ll u, ll p)
{
sz[u] = 1;
for (auto [v, w] : graf[u])
{
if (rmv[v] || v == p)
continue;
sz[u] += sbsz(v, u);
}
return sz[u];
}
ll gct(ll u, ll vel, ll p)
{
for (auto [v, w] : graf[u])
{
if (rmv[v] || v == p)
continue;
if (sz[v]*2 > vel)
return gct(v, vel, u);
}
return u;
}
void dfs(ll u, ll tr, ll gl, ll p, bool polni)
{
if (tr > k)
return;
if (polni)
{
if (mp[tr] == -1)
mp[tr] = gl;
else
mp[tr] = min(mp[tr], gl);
}
else
{
if (mp[k-tr] != -1)
rez = min((rez == -1 ? gl+mp[k-tr] : rez), gl+mp[k-tr]);
}
for (auto [v, w] : graf[u])
{
if (rmv[v] || v == p)
continue;
dfs(v, tr+w, gl+1, u, polni);
}
}
void del(int tr, int u, int p = -1)
{
if(tr > k) return;
mp[tr] = -1;
for(auto [v, w]: graf[u])
if(v != p && !rmv[v])
del(tr + w, v, u);
}
void decom(ll u)
{
u = gct(u, sbsz(u, -1), -1);
rmv[u] = 1;
mp[0] = 0;
for (auto [v, w] : graf[u])
{
if (rmv[v])
continue;
dfs(v, w, 1, u, false);
dfs(v, w, 1, u, true);
}
for(auto [v, w]: graf[u])
if(!rmv[v])
del(w, v);
for (auto [v, w] : graf[u])
if (!rmv[v])
decom(v);
return;
}
int best_path(int N, int K, int H[][2], int L[])
{
ll n = N;
k = K;
for (ll i = 0; i < n-1; i++)
{
ll u = H[i][0], v = H[i][1];
ll w = L[i];
// cout << "--> " << u << " " << v << " " << w << "\n";
graf[u].push_back({v, w});
graf[v].push_back({u, w});
}
memset(mp, -1, sizeof(mp));
decom(0);
return rez;
}
Compilation message
race.cpp: In function 'll sbsz(ll, ll)':
race.cpp:20:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
20 | for (auto [v, w] : graf[u])
| ^
race.cpp: In function 'll gct(ll, ll, ll)':
race.cpp:31:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
31 | for (auto [v, w] : graf[u])
| ^
race.cpp: In function 'void dfs(ll, ll, ll, ll, bool)':
race.cpp:57:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
57 | for (auto [v, w] : graf[u])
| ^
race.cpp: In function 'void del(int, int, int)':
race.cpp:69:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
69 | for(auto [v, w]: graf[u])
| ^
race.cpp: In function 'void decom(ll)':
race.cpp:79:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
79 | for (auto [v, w] : graf[u])
| ^
race.cpp:86:11: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
86 | for(auto [v, w]: graf[u])
| ^
race.cpp:89:12: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
89 | for (auto [v, w] : graf[u])
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
25948 KB |
Output is correct |
2 |
Correct |
6 ms |
26204 KB |
Output is correct |
3 |
Correct |
5 ms |
26176 KB |
Output is correct |
4 |
Correct |
4 ms |
26176 KB |
Output is correct |
5 |
Correct |
4 ms |
26200 KB |
Output is correct |
6 |
Correct |
5 ms |
25948 KB |
Output is correct |
7 |
Correct |
4 ms |
26204 KB |
Output is correct |
8 |
Correct |
4 ms |
26200 KB |
Output is correct |
9 |
Correct |
5 ms |
25948 KB |
Output is correct |
10 |
Correct |
5 ms |
25948 KB |
Output is correct |
11 |
Correct |
4 ms |
26040 KB |
Output is correct |
12 |
Correct |
4 ms |
25948 KB |
Output is correct |
13 |
Correct |
4 ms |
25948 KB |
Output is correct |
14 |
Correct |
4 ms |
25948 KB |
Output is correct |
15 |
Correct |
4 ms |
25948 KB |
Output is correct |
16 |
Correct |
4 ms |
26176 KB |
Output is correct |
17 |
Correct |
5 ms |
26204 KB |
Output is correct |
18 |
Correct |
5 ms |
26204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
25948 KB |
Output is correct |
2 |
Correct |
6 ms |
26204 KB |
Output is correct |
3 |
Correct |
5 ms |
26176 KB |
Output is correct |
4 |
Correct |
4 ms |
26176 KB |
Output is correct |
5 |
Correct |
4 ms |
26200 KB |
Output is correct |
6 |
Correct |
5 ms |
25948 KB |
Output is correct |
7 |
Correct |
4 ms |
26204 KB |
Output is correct |
8 |
Correct |
4 ms |
26200 KB |
Output is correct |
9 |
Correct |
5 ms |
25948 KB |
Output is correct |
10 |
Correct |
5 ms |
25948 KB |
Output is correct |
11 |
Correct |
4 ms |
26040 KB |
Output is correct |
12 |
Correct |
4 ms |
25948 KB |
Output is correct |
13 |
Correct |
4 ms |
25948 KB |
Output is correct |
14 |
Correct |
4 ms |
25948 KB |
Output is correct |
15 |
Correct |
4 ms |
25948 KB |
Output is correct |
16 |
Correct |
4 ms |
26176 KB |
Output is correct |
17 |
Correct |
5 ms |
26204 KB |
Output is correct |
18 |
Correct |
5 ms |
26204 KB |
Output is correct |
19 |
Correct |
4 ms |
25948 KB |
Output is correct |
20 |
Correct |
4 ms |
25948 KB |
Output is correct |
21 |
Correct |
5 ms |
26200 KB |
Output is correct |
22 |
Correct |
5 ms |
26204 KB |
Output is correct |
23 |
Correct |
5 ms |
26156 KB |
Output is correct |
24 |
Correct |
5 ms |
26204 KB |
Output is correct |
25 |
Correct |
7 ms |
26204 KB |
Output is correct |
26 |
Correct |
6 ms |
26204 KB |
Output is correct |
27 |
Correct |
5 ms |
26204 KB |
Output is correct |
28 |
Correct |
5 ms |
26204 KB |
Output is correct |
29 |
Correct |
6 ms |
26200 KB |
Output is correct |
30 |
Correct |
5 ms |
26200 KB |
Output is correct |
31 |
Correct |
5 ms |
26160 KB |
Output is correct |
32 |
Correct |
5 ms |
26204 KB |
Output is correct |
33 |
Correct |
6 ms |
26204 KB |
Output is correct |
34 |
Correct |
5 ms |
26204 KB |
Output is correct |
35 |
Correct |
5 ms |
26204 KB |
Output is correct |
36 |
Correct |
5 ms |
26204 KB |
Output is correct |
37 |
Correct |
5 ms |
26204 KB |
Output is correct |
38 |
Correct |
5 ms |
26260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
25948 KB |
Output is correct |
2 |
Correct |
6 ms |
26204 KB |
Output is correct |
3 |
Correct |
5 ms |
26176 KB |
Output is correct |
4 |
Correct |
4 ms |
26176 KB |
Output is correct |
5 |
Correct |
4 ms |
26200 KB |
Output is correct |
6 |
Correct |
5 ms |
25948 KB |
Output is correct |
7 |
Correct |
4 ms |
26204 KB |
Output is correct |
8 |
Correct |
4 ms |
26200 KB |
Output is correct |
9 |
Correct |
5 ms |
25948 KB |
Output is correct |
10 |
Correct |
5 ms |
25948 KB |
Output is correct |
11 |
Correct |
4 ms |
26040 KB |
Output is correct |
12 |
Correct |
4 ms |
25948 KB |
Output is correct |
13 |
Correct |
4 ms |
25948 KB |
Output is correct |
14 |
Correct |
4 ms |
25948 KB |
Output is correct |
15 |
Correct |
4 ms |
25948 KB |
Output is correct |
16 |
Correct |
4 ms |
26176 KB |
Output is correct |
17 |
Correct |
5 ms |
26204 KB |
Output is correct |
18 |
Correct |
5 ms |
26204 KB |
Output is correct |
19 |
Correct |
107 ms |
33672 KB |
Output is correct |
20 |
Correct |
110 ms |
34404 KB |
Output is correct |
21 |
Correct |
107 ms |
34248 KB |
Output is correct |
22 |
Correct |
104 ms |
34888 KB |
Output is correct |
23 |
Correct |
62 ms |
35568 KB |
Output is correct |
24 |
Correct |
46 ms |
34280 KB |
Output is correct |
25 |
Correct |
98 ms |
37428 KB |
Output is correct |
26 |
Correct |
78 ms |
40044 KB |
Output is correct |
27 |
Correct |
116 ms |
40532 KB |
Output is correct |
28 |
Correct |
217 ms |
51744 KB |
Output is correct |
29 |
Correct |
180 ms |
51280 KB |
Output is correct |
30 |
Correct |
119 ms |
41136 KB |
Output is correct |
31 |
Correct |
169 ms |
41108 KB |
Output is correct |
32 |
Correct |
144 ms |
41132 KB |
Output is correct |
33 |
Correct |
173 ms |
40132 KB |
Output is correct |
34 |
Correct |
123 ms |
40508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
25948 KB |
Output is correct |
2 |
Correct |
6 ms |
26204 KB |
Output is correct |
3 |
Correct |
5 ms |
26176 KB |
Output is correct |
4 |
Correct |
4 ms |
26176 KB |
Output is correct |
5 |
Correct |
4 ms |
26200 KB |
Output is correct |
6 |
Correct |
5 ms |
25948 KB |
Output is correct |
7 |
Correct |
4 ms |
26204 KB |
Output is correct |
8 |
Correct |
4 ms |
26200 KB |
Output is correct |
9 |
Correct |
5 ms |
25948 KB |
Output is correct |
10 |
Correct |
5 ms |
25948 KB |
Output is correct |
11 |
Correct |
4 ms |
26040 KB |
Output is correct |
12 |
Correct |
4 ms |
25948 KB |
Output is correct |
13 |
Correct |
4 ms |
25948 KB |
Output is correct |
14 |
Correct |
4 ms |
25948 KB |
Output is correct |
15 |
Correct |
4 ms |
25948 KB |
Output is correct |
16 |
Correct |
4 ms |
26176 KB |
Output is correct |
17 |
Correct |
5 ms |
26204 KB |
Output is correct |
18 |
Correct |
5 ms |
26204 KB |
Output is correct |
19 |
Correct |
4 ms |
25948 KB |
Output is correct |
20 |
Correct |
4 ms |
25948 KB |
Output is correct |
21 |
Correct |
5 ms |
26200 KB |
Output is correct |
22 |
Correct |
5 ms |
26204 KB |
Output is correct |
23 |
Correct |
5 ms |
26156 KB |
Output is correct |
24 |
Correct |
5 ms |
26204 KB |
Output is correct |
25 |
Correct |
7 ms |
26204 KB |
Output is correct |
26 |
Correct |
6 ms |
26204 KB |
Output is correct |
27 |
Correct |
5 ms |
26204 KB |
Output is correct |
28 |
Correct |
5 ms |
26204 KB |
Output is correct |
29 |
Correct |
6 ms |
26200 KB |
Output is correct |
30 |
Correct |
5 ms |
26200 KB |
Output is correct |
31 |
Correct |
5 ms |
26160 KB |
Output is correct |
32 |
Correct |
5 ms |
26204 KB |
Output is correct |
33 |
Correct |
6 ms |
26204 KB |
Output is correct |
34 |
Correct |
5 ms |
26204 KB |
Output is correct |
35 |
Correct |
5 ms |
26204 KB |
Output is correct |
36 |
Correct |
5 ms |
26204 KB |
Output is correct |
37 |
Correct |
5 ms |
26204 KB |
Output is correct |
38 |
Correct |
5 ms |
26260 KB |
Output is correct |
39 |
Correct |
107 ms |
33672 KB |
Output is correct |
40 |
Correct |
110 ms |
34404 KB |
Output is correct |
41 |
Correct |
107 ms |
34248 KB |
Output is correct |
42 |
Correct |
104 ms |
34888 KB |
Output is correct |
43 |
Correct |
62 ms |
35568 KB |
Output is correct |
44 |
Correct |
46 ms |
34280 KB |
Output is correct |
45 |
Correct |
98 ms |
37428 KB |
Output is correct |
46 |
Correct |
78 ms |
40044 KB |
Output is correct |
47 |
Correct |
116 ms |
40532 KB |
Output is correct |
48 |
Correct |
217 ms |
51744 KB |
Output is correct |
49 |
Correct |
180 ms |
51280 KB |
Output is correct |
50 |
Correct |
119 ms |
41136 KB |
Output is correct |
51 |
Correct |
169 ms |
41108 KB |
Output is correct |
52 |
Correct |
144 ms |
41132 KB |
Output is correct |
53 |
Correct |
173 ms |
40132 KB |
Output is correct |
54 |
Correct |
123 ms |
40508 KB |
Output is correct |
55 |
Correct |
12 ms |
26716 KB |
Output is correct |
56 |
Correct |
12 ms |
26716 KB |
Output is correct |
57 |
Correct |
63 ms |
33960 KB |
Output is correct |
58 |
Correct |
39 ms |
34104 KB |
Output is correct |
59 |
Correct |
81 ms |
40020 KB |
Output is correct |
60 |
Correct |
362 ms |
51556 KB |
Output is correct |
61 |
Correct |
126 ms |
40616 KB |
Output is correct |
62 |
Correct |
170 ms |
41300 KB |
Output is correct |
63 |
Correct |
241 ms |
41044 KB |
Output is correct |
64 |
Correct |
499 ms |
39580 KB |
Output is correct |
65 |
Correct |
176 ms |
41852 KB |
Output is correct |
66 |
Correct |
331 ms |
47376 KB |
Output is correct |
67 |
Correct |
89 ms |
41772 KB |
Output is correct |
68 |
Correct |
220 ms |
40836 KB |
Output is correct |
69 |
Correct |
185 ms |
38624 KB |
Output is correct |
70 |
Correct |
228 ms |
40916 KB |
Output is correct |