#include <bits/stdc++.h>
#include "race.h"
using namespace std;
#define lli long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int (i) = 0; (i) < (n); (i)++)
#pragma optimize('O1,O2,O3, Ofast');
const double EPS = 0.00001;
const int INF = 1e9+500;
const int ALPH = 26;
const int MOD = 1e9+7;
const int M = 1e6+5;
const int NMAX = 2e5+5;
int n,m,q, k;
array<int,2> cnt[M];
int ans = INF;
int vis[NMAX], subt[NMAX];
vector<vector<array<int,2> > > adj;
vector<int> centers;
int curc = 0;
inline void subtdfs(int node, int par) {
subt[node] = 1;
for(auto c : adj[node]) {
if(c[0] == par) continue;
if(vis[c[0]]) continue;
subtdfs(c[0], node);
subt[node] += subt[c[0]];
}
}
inline int cdfs(int node, int par, int sz) {
for(auto c : adj[node]) {
if(c[0] == par) continue;
if(vis[c[0]]) continue;
if(subt[c[0]]*2 >= sz) {
return cdfs(c[0], node, sz);
}
}
return node;
}
inline void find_centroids() {
subtdfs(1, 0);
int tmp = cdfs(1, 0, subt[1]);
centers.pb(tmp);
int ind = 0;
vis[tmp] = 1;
while(ind < centers.size()) {
for(auto c : adj[centers[ind]]) {
if(vis[c[0]]) continue;
subtdfs(c[0], 0);
tmp = cdfs(c[0], 0, subt[c[0]]);
vis[tmp] = 1;
centers.pb(tmp);
}
ind++;
}
}
inline void dfs(int node ,int par, int cost, int leng) {
if(leng > k) return;
if(cnt[leng][1] != curc) {
cnt[leng] = {cost, curc};
}
cnt[leng][0] = min(cnt[leng][0], cost);
for(auto c : adj[node]) {
if(c[0] == par) continue;
if(vis[c[0]]) continue;
dfs(c[0], node, cost + 1, leng + c[1]);
}
}
inline void dfs2(int node, int par, int cost, int leng) {
if(leng > k) return;
if(cnt[k - leng][1] == curc) {
ans = min(ans ,cnt[k - leng][0] + cost);
}
for(auto c : adj[node]) {
if(c[0] == par) continue;
if(vis[c[0]]) continue;
dfs2(c[0], node, cost + 1, leng + c[1]);
}
}
inline void find_paths() {
for(int j = 0; j<k+1; j++) {
cnt[j][0] = INF;
cnt[j][1] = 0;
}
for(int i = 0; i<=n; i++) vis[i] = 0;
for(auto i : centers) {
cnt[0][0] = 0; cnt[0][1] = i;
curc = i;
vis[i] = 1;
for(auto c : adj[i]) {
if(vis[c[0]]) continue;
dfs2(c[0], i, 1, c[1]);
dfs(c[0], i, 1, c[1]);
}
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N;
adj.resize(n+1);
k = K;
for(int i = 0; i<n-1; i++) {
H[i][0]++;
H[i][1]++;
adj[H[i][0]].pb({H[i][1], L[i]});
adj[H[i][1]].pb({H[i][0], L[i]});
}
find_centroids();
find_paths();
if(ans == INF) ans = -1;
return ans;
}
Compilation message
race.cpp:8: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
8 | #pragma optimize('O1,O2,O3, Ofast');
|
race.cpp: In function 'void find_centroids()':
race.cpp:50:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | while(ind < centers.size()) {
| ~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4696 KB |
Output is correct |
13 |
Correct |
1 ms |
4440 KB |
Output is correct |
14 |
Correct |
1 ms |
4440 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4696 KB |
Output is correct |
13 |
Correct |
1 ms |
4440 KB |
Output is correct |
14 |
Correct |
1 ms |
4440 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
2 ms |
4440 KB |
Output is correct |
21 |
Correct |
2 ms |
4440 KB |
Output is correct |
22 |
Correct |
4 ms |
11612 KB |
Output is correct |
23 |
Correct |
4 ms |
11608 KB |
Output is correct |
24 |
Correct |
3 ms |
11612 KB |
Output is correct |
25 |
Correct |
3 ms |
11608 KB |
Output is correct |
26 |
Correct |
2 ms |
7512 KB |
Output is correct |
27 |
Correct |
3 ms |
11608 KB |
Output is correct |
28 |
Correct |
2 ms |
7512 KB |
Output is correct |
29 |
Correct |
3 ms |
7396 KB |
Output is correct |
30 |
Correct |
2 ms |
7512 KB |
Output is correct |
31 |
Correct |
3 ms |
11608 KB |
Output is correct |
32 |
Correct |
3 ms |
11612 KB |
Output is correct |
33 |
Correct |
3 ms |
11612 KB |
Output is correct |
34 |
Correct |
2 ms |
9560 KB |
Output is correct |
35 |
Correct |
3 ms |
11608 KB |
Output is correct |
36 |
Correct |
3 ms |
11864 KB |
Output is correct |
37 |
Correct |
3 ms |
11608 KB |
Output is correct |
38 |
Correct |
2 ms |
9560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4696 KB |
Output is correct |
13 |
Correct |
1 ms |
4440 KB |
Output is correct |
14 |
Correct |
1 ms |
4440 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
92 ms |
13696 KB |
Output is correct |
20 |
Correct |
93 ms |
13796 KB |
Output is correct |
21 |
Correct |
98 ms |
13776 KB |
Output is correct |
22 |
Correct |
84 ms |
13780 KB |
Output is correct |
23 |
Correct |
56 ms |
14040 KB |
Output is correct |
24 |
Correct |
39 ms |
13776 KB |
Output is correct |
25 |
Correct |
108 ms |
14804 KB |
Output is correct |
26 |
Correct |
61 ms |
16340 KB |
Output is correct |
27 |
Correct |
124 ms |
21128 KB |
Output is correct |
28 |
Correct |
181 ms |
26032 KB |
Output is correct |
29 |
Correct |
225 ms |
25544 KB |
Output is correct |
30 |
Correct |
189 ms |
21152 KB |
Output is correct |
31 |
Correct |
144 ms |
21200 KB |
Output is correct |
32 |
Correct |
174 ms |
21172 KB |
Output is correct |
33 |
Correct |
157 ms |
19916 KB |
Output is correct |
34 |
Correct |
142 ms |
19884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4440 KB |
Output is correct |
3 |
Correct |
1 ms |
4440 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4440 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4440 KB |
Output is correct |
9 |
Correct |
1 ms |
4440 KB |
Output is correct |
10 |
Correct |
1 ms |
4440 KB |
Output is correct |
11 |
Correct |
1 ms |
4440 KB |
Output is correct |
12 |
Correct |
1 ms |
4696 KB |
Output is correct |
13 |
Correct |
1 ms |
4440 KB |
Output is correct |
14 |
Correct |
1 ms |
4440 KB |
Output is correct |
15 |
Correct |
1 ms |
4440 KB |
Output is correct |
16 |
Correct |
1 ms |
4440 KB |
Output is correct |
17 |
Correct |
1 ms |
4440 KB |
Output is correct |
18 |
Correct |
1 ms |
4440 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
2 ms |
4440 KB |
Output is correct |
21 |
Correct |
2 ms |
4440 KB |
Output is correct |
22 |
Correct |
4 ms |
11612 KB |
Output is correct |
23 |
Correct |
4 ms |
11608 KB |
Output is correct |
24 |
Correct |
3 ms |
11612 KB |
Output is correct |
25 |
Correct |
3 ms |
11608 KB |
Output is correct |
26 |
Correct |
2 ms |
7512 KB |
Output is correct |
27 |
Correct |
3 ms |
11608 KB |
Output is correct |
28 |
Correct |
2 ms |
7512 KB |
Output is correct |
29 |
Correct |
3 ms |
7396 KB |
Output is correct |
30 |
Correct |
2 ms |
7512 KB |
Output is correct |
31 |
Correct |
3 ms |
11608 KB |
Output is correct |
32 |
Correct |
3 ms |
11612 KB |
Output is correct |
33 |
Correct |
3 ms |
11612 KB |
Output is correct |
34 |
Correct |
2 ms |
9560 KB |
Output is correct |
35 |
Correct |
3 ms |
11608 KB |
Output is correct |
36 |
Correct |
3 ms |
11864 KB |
Output is correct |
37 |
Correct |
3 ms |
11608 KB |
Output is correct |
38 |
Correct |
2 ms |
9560 KB |
Output is correct |
39 |
Correct |
92 ms |
13696 KB |
Output is correct |
40 |
Correct |
93 ms |
13796 KB |
Output is correct |
41 |
Correct |
98 ms |
13776 KB |
Output is correct |
42 |
Correct |
84 ms |
13780 KB |
Output is correct |
43 |
Correct |
56 ms |
14040 KB |
Output is correct |
44 |
Correct |
39 ms |
13776 KB |
Output is correct |
45 |
Correct |
108 ms |
14804 KB |
Output is correct |
46 |
Correct |
61 ms |
16340 KB |
Output is correct |
47 |
Correct |
124 ms |
21128 KB |
Output is correct |
48 |
Correct |
181 ms |
26032 KB |
Output is correct |
49 |
Correct |
225 ms |
25544 KB |
Output is correct |
50 |
Correct |
189 ms |
21152 KB |
Output is correct |
51 |
Correct |
144 ms |
21200 KB |
Output is correct |
52 |
Correct |
174 ms |
21172 KB |
Output is correct |
53 |
Correct |
157 ms |
19916 KB |
Output is correct |
54 |
Correct |
142 ms |
19884 KB |
Output is correct |
55 |
Correct |
7 ms |
5464 KB |
Output is correct |
56 |
Correct |
8 ms |
5208 KB |
Output is correct |
57 |
Correct |
54 ms |
13776 KB |
Output is correct |
58 |
Correct |
27 ms |
14252 KB |
Output is correct |
59 |
Correct |
67 ms |
19152 KB |
Output is correct |
60 |
Correct |
354 ms |
33316 KB |
Output is correct |
61 |
Correct |
145 ms |
21112 KB |
Output is correct |
62 |
Correct |
173 ms |
28588 KB |
Output is correct |
63 |
Correct |
243 ms |
28568 KB |
Output is correct |
64 |
Correct |
340 ms |
25844 KB |
Output is correct |
65 |
Correct |
172 ms |
21196 KB |
Output is correct |
66 |
Correct |
269 ms |
32192 KB |
Output is correct |
67 |
Correct |
70 ms |
30144 KB |
Output is correct |
68 |
Correct |
271 ms |
28612 KB |
Output is correct |
69 |
Correct |
240 ms |
28872 KB |
Output is correct |
70 |
Correct |
170 ms |
28104 KB |
Output is correct |