#include <bits/stdc++.h>
#include "race.h"
#define ll long long
#define fi first
#define se second
#define endl '\n'
#define MASK(i) (1LL << (i))
#define ull unsigned long long
#define ld long double
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define BIT(x , i) ((x >> (i)) & 1)
#define TASK "task"
#define sz(s) (int) (s).size()
using namespace std;
const int mxN = 2e5 + 227;
const int inf = 1e9 + 277;
const int mod = 1e9 + 7;
const ll infll = 1e18 + 7;
const int base = 307;
const int LOG = 20;
template <typename T1, typename T2> bool minimize(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maximize(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
bool pass[mxN];
int n;
int k;
int res = inf;
int h[mxN][2];
int l[mxN];
int sub[mxN];
int dist[mxN];
int parcen[mxN];
vector<int> node;
vector<pair<int, int>> adj[mxN];
int rdist[mxN];
bool removed[mxN];
int mp[1000007];
vector<int> visit;
int tst = 0;
void dfs_sz(int u, int p)
{
sub[u] = 1;
for(auto it : adj[u]) {
int v = it.fi;
int c = it.se;
if(v == p || removed[v] == true) continue;
dfs_sz(v, u);
sub[u] += sub[v];
}
}
int dfs_cen(int u, int p, int n)
{
for(auto it : adj[u]) {
int v = it.fi;
int c = it.se;
if(v == p || removed[v] == true) continue;
if(sub[v] > n / 2) return dfs_cen(v, u, n);
}
return u;
}
void find_best(int u, int p, int root)
{
if(dist[u] >= res || rdist[u] >= k) return;
visit.pb(u);
if(u != root) node.pb(u);
for(auto it : adj[u]) {
int v = it.fi;
int c = it.se;
if(v == p || removed[v] == true || dist[u] + 1 > res || rdist[u] + c > k) continue;
rdist[v] = rdist[u] + c;
dist[v] = dist[u] + 1;
if(rdist[v] == k) minimize(res, dist[v]);
minimize(res, dist[v] + mp[k - rdist[v]]);
find_best(v, u, root);
if(u == root) {
for(auto it : node) {
minimize(mp[rdist[it]], dist[it]);
}
node.clear();
}
}
}
void init_centroid(int u, int p)
{
dfs_sz(u, -1);
int c = dfs_cen(u, -1, sub[u]);
node.clear();
visit.clear();
rdist[c] = 0;
dist[c] = 0;
find_best(c, -1, c);
removed[c] = true;
for(auto it : visit) mp[rdist[it]] = inf;
for(auto it : adj[c]) {
int v = it.fi;
if(removed[v] == false) init_centroid(v, c);
}
}
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];
int v = h[i][1];
++u;
++v;
adj[u].pb({v, l[i]});
adj[v].pb({u, l[i]});
}
for(int i = 0; i <= k; i++) mp[i] = inf;
init_centroid(1, -1);
if(res == inf) return -1;
return res;
}
Compilation message
race.cpp: In function 'void dfs_sz(int, int)':
race.cpp:54:13: warning: unused variable 'c' [-Wunused-variable]
54 | int c = it.se;
| ^
race.cpp: In function 'int dfs_cen(int, int, int)':
race.cpp:65:13: warning: unused variable 'c' [-Wunused-variable]
65 | int c = it.se;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 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 |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5020 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 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 |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5020 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5024 KB |
Output is correct |
19 |
Correct |
3 ms |
5024 KB |
Output is correct |
20 |
Correct |
3 ms |
5020 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
5 ms |
8620 KB |
Output is correct |
23 |
Correct |
5 ms |
8020 KB |
Output is correct |
24 |
Correct |
5 ms |
8404 KB |
Output is correct |
25 |
Correct |
5 ms |
8404 KB |
Output is correct |
26 |
Correct |
4 ms |
6484 KB |
Output is correct |
27 |
Correct |
6 ms |
8276 KB |
Output is correct |
28 |
Correct |
3 ms |
5844 KB |
Output is correct |
29 |
Correct |
4 ms |
6316 KB |
Output is correct |
30 |
Correct |
4 ms |
6484 KB |
Output is correct |
31 |
Correct |
5 ms |
7636 KB |
Output is correct |
32 |
Correct |
5 ms |
7892 KB |
Output is correct |
33 |
Correct |
5 ms |
8148 KB |
Output is correct |
34 |
Correct |
5 ms |
7340 KB |
Output is correct |
35 |
Correct |
5 ms |
8280 KB |
Output is correct |
36 |
Correct |
5 ms |
8788 KB |
Output is correct |
37 |
Correct |
5 ms |
8148 KB |
Output is correct |
38 |
Correct |
4 ms |
7072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 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 |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5020 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5024 KB |
Output is correct |
19 |
Correct |
97 ms |
11500 KB |
Output is correct |
20 |
Correct |
114 ms |
11520 KB |
Output is correct |
21 |
Correct |
103 ms |
11640 KB |
Output is correct |
22 |
Correct |
93 ms |
11796 KB |
Output is correct |
23 |
Correct |
64 ms |
11792 KB |
Output is correct |
24 |
Correct |
47 ms |
11596 KB |
Output is correct |
25 |
Correct |
114 ms |
15148 KB |
Output is correct |
26 |
Correct |
90 ms |
19556 KB |
Output is correct |
27 |
Correct |
145 ms |
17884 KB |
Output is correct |
28 |
Correct |
248 ms |
32148 KB |
Output is correct |
29 |
Correct |
259 ms |
30896 KB |
Output is correct |
30 |
Correct |
144 ms |
17956 KB |
Output is correct |
31 |
Correct |
144 ms |
17900 KB |
Output is correct |
32 |
Correct |
191 ms |
17900 KB |
Output is correct |
33 |
Correct |
209 ms |
19276 KB |
Output is correct |
34 |
Correct |
167 ms |
20144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5024 KB |
Output is correct |
5 |
Correct |
2 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 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 |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
4948 KB |
Output is correct |
14 |
Correct |
3 ms |
5020 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5024 KB |
Output is correct |
19 |
Correct |
3 ms |
5024 KB |
Output is correct |
20 |
Correct |
3 ms |
5020 KB |
Output is correct |
21 |
Correct |
3 ms |
5076 KB |
Output is correct |
22 |
Correct |
5 ms |
8620 KB |
Output is correct |
23 |
Correct |
5 ms |
8020 KB |
Output is correct |
24 |
Correct |
5 ms |
8404 KB |
Output is correct |
25 |
Correct |
5 ms |
8404 KB |
Output is correct |
26 |
Correct |
4 ms |
6484 KB |
Output is correct |
27 |
Correct |
6 ms |
8276 KB |
Output is correct |
28 |
Correct |
3 ms |
5844 KB |
Output is correct |
29 |
Correct |
4 ms |
6316 KB |
Output is correct |
30 |
Correct |
4 ms |
6484 KB |
Output is correct |
31 |
Correct |
5 ms |
7636 KB |
Output is correct |
32 |
Correct |
5 ms |
7892 KB |
Output is correct |
33 |
Correct |
5 ms |
8148 KB |
Output is correct |
34 |
Correct |
5 ms |
7340 KB |
Output is correct |
35 |
Correct |
5 ms |
8280 KB |
Output is correct |
36 |
Correct |
5 ms |
8788 KB |
Output is correct |
37 |
Correct |
5 ms |
8148 KB |
Output is correct |
38 |
Correct |
4 ms |
7072 KB |
Output is correct |
39 |
Correct |
97 ms |
11500 KB |
Output is correct |
40 |
Correct |
114 ms |
11520 KB |
Output is correct |
41 |
Correct |
103 ms |
11640 KB |
Output is correct |
42 |
Correct |
93 ms |
11796 KB |
Output is correct |
43 |
Correct |
64 ms |
11792 KB |
Output is correct |
44 |
Correct |
47 ms |
11596 KB |
Output is correct |
45 |
Correct |
114 ms |
15148 KB |
Output is correct |
46 |
Correct |
90 ms |
19556 KB |
Output is correct |
47 |
Correct |
145 ms |
17884 KB |
Output is correct |
48 |
Correct |
248 ms |
32148 KB |
Output is correct |
49 |
Correct |
259 ms |
30896 KB |
Output is correct |
50 |
Correct |
144 ms |
17956 KB |
Output is correct |
51 |
Correct |
144 ms |
17900 KB |
Output is correct |
52 |
Correct |
191 ms |
17900 KB |
Output is correct |
53 |
Correct |
209 ms |
19276 KB |
Output is correct |
54 |
Correct |
167 ms |
20144 KB |
Output is correct |
55 |
Correct |
9 ms |
5844 KB |
Output is correct |
56 |
Correct |
10 ms |
5804 KB |
Output is correct |
57 |
Correct |
79 ms |
13380 KB |
Output is correct |
58 |
Correct |
34 ms |
13324 KB |
Output is correct |
59 |
Correct |
95 ms |
21284 KB |
Output is correct |
60 |
Correct |
449 ms |
39260 KB |
Output is correct |
61 |
Correct |
163 ms |
20816 KB |
Output is correct |
62 |
Correct |
172 ms |
25336 KB |
Output is correct |
63 |
Correct |
248 ms |
25296 KB |
Output is correct |
64 |
Correct |
427 ms |
24036 KB |
Output is correct |
65 |
Correct |
214 ms |
21196 KB |
Output is correct |
66 |
Correct |
412 ms |
35236 KB |
Output is correct |
67 |
Correct |
122 ms |
26728 KB |
Output is correct |
68 |
Correct |
184 ms |
25136 KB |
Output is correct |
69 |
Correct |
180 ms |
25292 KB |
Output is correct |
70 |
Correct |
174 ms |
24488 KB |
Output is correct |