#include<bits/stdc++.h>
#include "race.h"
using namespace std;
const int MAXN = 200005;
vector<pair<int,int> > g[MAXN];
vector<char> used(MAXN,0);
vector<int> sz(MAXN,0);
vector<pair<int,int> > cur(MAXN);
int Ans = 1e9;
int n,k;
const int MAXK = 1e6 + 10;
vector<int> ans(MAXK,-1);
vector<int> changed(MAXK);
int cursz = 0;
int chsz = 0;
void dfs_sizes(int v,int p) {
sz[v] = 1;
for(int i=0;i<g[v].size();i++) {
int to = g[v][i].first;
if (to==p || used[to]) continue;
dfs_sizes(to,v);
sz[v]+=sz[to];
}
}
int centroid(int v,int p,int ss) {
for(int i=0;i<g[v].size();i++) {
int to = g[v][i].first;
if (to==p || used[to]) continue;
if (sz[to] > ss/2) return centroid(to,v,ss);
}
return v;
}
void dfs_func(int v,int p,pair<int,int> func) {
if (func.first>k) return;
cur[cursz++] = func;
int len = func.first, cnt = func.second;
for(int i=0;i<g[v].size();i++) {
int to = g[v][i].first;
if (to==p || used[to]) continue;
if (len+g[v][i].second > k) return;
dfs_func(to,v,{len+g[v][i].second,cnt+1});
}
}
void solve(int v) {
dfs_sizes(v,-1);
int c = centroid(v,-1,sz[v]);
used[c] = 1;
ans[0] = 0;
changed[chsz++] = 0;
for(int i=0;i<g[c].size();i++) {
int ch = g[c][i].first;
if (used[ch]) continue;
cursz = 0;
dfs_func(ch,c,{g[c][i].second,1});
for(int j=0;j<cursz;j++) {
int len = cur[j].first, cnt = cur[j].second;
if (ans[k-len]!=-1) Ans = min(Ans,ans[k-len] + cnt);
}
for(int j=0;j<cursz;j++) {
int len = cur[j].first, cnt = cur[j].second;
if (ans[len] == -1) {
ans[len] = cnt;
changed[chsz++] = len;
}
else ans[len] = min(ans[len],cnt);
}
}
for(int i=0;i<chsz;i++) {
ans[changed[i]] = -1;
}
chsz = 0;
for(int i=0;i<g[c].size();i++) {
if (!used[g[c][i].first]) solve(g[c][i].first);
}
}
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],v=h[i][1],len = l[i];
g[u].push_back({v,len});
g[v].push_back({u,len});
}
solve(0);
if (Ans==1e9) return -1; else return Ans;
}
Compilation message
race.cpp: In function 'void dfs_sizes(int, int)':
race.cpp:23:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for(int i=0;i<g[v].size();i++) {
| ~^~~~~~~~~~~~
race.cpp: In function 'int centroid(int, int, int)':
race.cpp:32:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for(int i=0;i<g[v].size();i++) {
| ~^~~~~~~~~~~~
race.cpp: In function 'void dfs_func(int, int, std::pair<int, int>)':
race.cpp:44:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for(int i=0;i<g[v].size();i++) {
| ~^~~~~~~~~~~~
race.cpp: In function 'void solve(int)':
race.cpp:61:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for(int i=0;i<g[c].size();i++) {
| ~^~~~~~~~~~~~
race.cpp:84:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
84 | for(int i=0;i<g[c].size();i++) {
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15316 KB |
Output is correct |
2 |
Correct |
8 ms |
15340 KB |
Output is correct |
3 |
Correct |
10 ms |
15316 KB |
Output is correct |
4 |
Correct |
8 ms |
15316 KB |
Output is correct |
5 |
Correct |
8 ms |
15316 KB |
Output is correct |
6 |
Correct |
8 ms |
15316 KB |
Output is correct |
7 |
Correct |
8 ms |
15372 KB |
Output is correct |
8 |
Correct |
8 ms |
15316 KB |
Output is correct |
9 |
Correct |
6 ms |
15316 KB |
Output is correct |
10 |
Correct |
7 ms |
15316 KB |
Output is correct |
11 |
Correct |
7 ms |
15316 KB |
Output is correct |
12 |
Correct |
7 ms |
15316 KB |
Output is correct |
13 |
Correct |
7 ms |
15336 KB |
Output is correct |
14 |
Correct |
7 ms |
15316 KB |
Output is correct |
15 |
Correct |
7 ms |
15316 KB |
Output is correct |
16 |
Correct |
8 ms |
15316 KB |
Output is correct |
17 |
Correct |
9 ms |
15316 KB |
Output is correct |
18 |
Correct |
8 ms |
15340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15316 KB |
Output is correct |
2 |
Correct |
8 ms |
15340 KB |
Output is correct |
3 |
Correct |
10 ms |
15316 KB |
Output is correct |
4 |
Correct |
8 ms |
15316 KB |
Output is correct |
5 |
Correct |
8 ms |
15316 KB |
Output is correct |
6 |
Correct |
8 ms |
15316 KB |
Output is correct |
7 |
Correct |
8 ms |
15372 KB |
Output is correct |
8 |
Correct |
8 ms |
15316 KB |
Output is correct |
9 |
Correct |
6 ms |
15316 KB |
Output is correct |
10 |
Correct |
7 ms |
15316 KB |
Output is correct |
11 |
Correct |
7 ms |
15316 KB |
Output is correct |
12 |
Correct |
7 ms |
15316 KB |
Output is correct |
13 |
Correct |
7 ms |
15336 KB |
Output is correct |
14 |
Correct |
7 ms |
15316 KB |
Output is correct |
15 |
Correct |
7 ms |
15316 KB |
Output is correct |
16 |
Correct |
8 ms |
15316 KB |
Output is correct |
17 |
Correct |
9 ms |
15316 KB |
Output is correct |
18 |
Correct |
8 ms |
15340 KB |
Output is correct |
19 |
Correct |
8 ms |
15328 KB |
Output is correct |
20 |
Correct |
8 ms |
15316 KB |
Output is correct |
21 |
Correct |
7 ms |
15444 KB |
Output is correct |
22 |
Correct |
8 ms |
15444 KB |
Output is correct |
23 |
Correct |
8 ms |
15356 KB |
Output is correct |
24 |
Correct |
8 ms |
15444 KB |
Output is correct |
25 |
Correct |
8 ms |
15444 KB |
Output is correct |
26 |
Correct |
8 ms |
15444 KB |
Output is correct |
27 |
Correct |
9 ms |
15384 KB |
Output is correct |
28 |
Correct |
8 ms |
15444 KB |
Output is correct |
29 |
Correct |
9 ms |
15444 KB |
Output is correct |
30 |
Correct |
9 ms |
15444 KB |
Output is correct |
31 |
Correct |
8 ms |
15448 KB |
Output is correct |
32 |
Correct |
9 ms |
15444 KB |
Output is correct |
33 |
Correct |
9 ms |
15364 KB |
Output is correct |
34 |
Correct |
8 ms |
15444 KB |
Output is correct |
35 |
Correct |
8 ms |
15444 KB |
Output is correct |
36 |
Correct |
8 ms |
15444 KB |
Output is correct |
37 |
Correct |
8 ms |
15444 KB |
Output is correct |
38 |
Correct |
8 ms |
15328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15316 KB |
Output is correct |
2 |
Correct |
8 ms |
15340 KB |
Output is correct |
3 |
Correct |
10 ms |
15316 KB |
Output is correct |
4 |
Correct |
8 ms |
15316 KB |
Output is correct |
5 |
Correct |
8 ms |
15316 KB |
Output is correct |
6 |
Correct |
8 ms |
15316 KB |
Output is correct |
7 |
Correct |
8 ms |
15372 KB |
Output is correct |
8 |
Correct |
8 ms |
15316 KB |
Output is correct |
9 |
Correct |
6 ms |
15316 KB |
Output is correct |
10 |
Correct |
7 ms |
15316 KB |
Output is correct |
11 |
Correct |
7 ms |
15316 KB |
Output is correct |
12 |
Correct |
7 ms |
15316 KB |
Output is correct |
13 |
Correct |
7 ms |
15336 KB |
Output is correct |
14 |
Correct |
7 ms |
15316 KB |
Output is correct |
15 |
Correct |
7 ms |
15316 KB |
Output is correct |
16 |
Correct |
8 ms |
15316 KB |
Output is correct |
17 |
Correct |
9 ms |
15316 KB |
Output is correct |
18 |
Correct |
8 ms |
15340 KB |
Output is correct |
19 |
Correct |
112 ms |
20116 KB |
Output is correct |
20 |
Correct |
111 ms |
20160 KB |
Output is correct |
21 |
Correct |
122 ms |
20308 KB |
Output is correct |
22 |
Correct |
123 ms |
20292 KB |
Output is correct |
23 |
Correct |
85 ms |
20400 KB |
Output is correct |
24 |
Correct |
58 ms |
20348 KB |
Output is correct |
25 |
Correct |
107 ms |
23804 KB |
Output is correct |
26 |
Correct |
87 ms |
27512 KB |
Output is correct |
27 |
Correct |
173 ms |
25292 KB |
Output is correct |
28 |
Correct |
248 ms |
39576 KB |
Output is correct |
29 |
Correct |
278 ms |
38348 KB |
Output is correct |
30 |
Correct |
157 ms |
25292 KB |
Output is correct |
31 |
Correct |
169 ms |
25368 KB |
Output is correct |
32 |
Correct |
218 ms |
25296 KB |
Output is correct |
33 |
Correct |
187 ms |
24140 KB |
Output is correct |
34 |
Correct |
179 ms |
24268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
15316 KB |
Output is correct |
2 |
Correct |
8 ms |
15340 KB |
Output is correct |
3 |
Correct |
10 ms |
15316 KB |
Output is correct |
4 |
Correct |
8 ms |
15316 KB |
Output is correct |
5 |
Correct |
8 ms |
15316 KB |
Output is correct |
6 |
Correct |
8 ms |
15316 KB |
Output is correct |
7 |
Correct |
8 ms |
15372 KB |
Output is correct |
8 |
Correct |
8 ms |
15316 KB |
Output is correct |
9 |
Correct |
6 ms |
15316 KB |
Output is correct |
10 |
Correct |
7 ms |
15316 KB |
Output is correct |
11 |
Correct |
7 ms |
15316 KB |
Output is correct |
12 |
Correct |
7 ms |
15316 KB |
Output is correct |
13 |
Correct |
7 ms |
15336 KB |
Output is correct |
14 |
Correct |
7 ms |
15316 KB |
Output is correct |
15 |
Correct |
7 ms |
15316 KB |
Output is correct |
16 |
Correct |
8 ms |
15316 KB |
Output is correct |
17 |
Correct |
9 ms |
15316 KB |
Output is correct |
18 |
Correct |
8 ms |
15340 KB |
Output is correct |
19 |
Correct |
8 ms |
15328 KB |
Output is correct |
20 |
Correct |
8 ms |
15316 KB |
Output is correct |
21 |
Correct |
7 ms |
15444 KB |
Output is correct |
22 |
Correct |
8 ms |
15444 KB |
Output is correct |
23 |
Correct |
8 ms |
15356 KB |
Output is correct |
24 |
Correct |
8 ms |
15444 KB |
Output is correct |
25 |
Correct |
8 ms |
15444 KB |
Output is correct |
26 |
Correct |
8 ms |
15444 KB |
Output is correct |
27 |
Correct |
9 ms |
15384 KB |
Output is correct |
28 |
Correct |
8 ms |
15444 KB |
Output is correct |
29 |
Correct |
9 ms |
15444 KB |
Output is correct |
30 |
Correct |
9 ms |
15444 KB |
Output is correct |
31 |
Correct |
8 ms |
15448 KB |
Output is correct |
32 |
Correct |
9 ms |
15444 KB |
Output is correct |
33 |
Correct |
9 ms |
15364 KB |
Output is correct |
34 |
Correct |
8 ms |
15444 KB |
Output is correct |
35 |
Correct |
8 ms |
15444 KB |
Output is correct |
36 |
Correct |
8 ms |
15444 KB |
Output is correct |
37 |
Correct |
8 ms |
15444 KB |
Output is correct |
38 |
Correct |
8 ms |
15328 KB |
Output is correct |
39 |
Correct |
112 ms |
20116 KB |
Output is correct |
40 |
Correct |
111 ms |
20160 KB |
Output is correct |
41 |
Correct |
122 ms |
20308 KB |
Output is correct |
42 |
Correct |
123 ms |
20292 KB |
Output is correct |
43 |
Correct |
85 ms |
20400 KB |
Output is correct |
44 |
Correct |
58 ms |
20348 KB |
Output is correct |
45 |
Correct |
107 ms |
23804 KB |
Output is correct |
46 |
Correct |
87 ms |
27512 KB |
Output is correct |
47 |
Correct |
173 ms |
25292 KB |
Output is correct |
48 |
Correct |
248 ms |
39576 KB |
Output is correct |
49 |
Correct |
278 ms |
38348 KB |
Output is correct |
50 |
Correct |
157 ms |
25292 KB |
Output is correct |
51 |
Correct |
169 ms |
25368 KB |
Output is correct |
52 |
Correct |
218 ms |
25296 KB |
Output is correct |
53 |
Correct |
187 ms |
24140 KB |
Output is correct |
54 |
Correct |
179 ms |
24268 KB |
Output is correct |
55 |
Correct |
18 ms |
15804 KB |
Output is correct |
56 |
Correct |
16 ms |
15892 KB |
Output is correct |
57 |
Correct |
79 ms |
20476 KB |
Output is correct |
58 |
Correct |
38 ms |
20408 KB |
Output is correct |
59 |
Correct |
98 ms |
28920 KB |
Output is correct |
60 |
Correct |
397 ms |
41800 KB |
Output is correct |
61 |
Correct |
168 ms |
28552 KB |
Output is correct |
62 |
Correct |
193 ms |
28236 KB |
Output is correct |
63 |
Correct |
259 ms |
28320 KB |
Output is correct |
64 |
Correct |
423 ms |
28528 KB |
Output is correct |
65 |
Correct |
241 ms |
29004 KB |
Output is correct |
66 |
Correct |
332 ms |
39088 KB |
Output is correct |
67 |
Correct |
114 ms |
28872 KB |
Output is correct |
68 |
Correct |
216 ms |
29056 KB |
Output is correct |
69 |
Correct |
247 ms |
29232 KB |
Output is correct |
70 |
Correct |
205 ms |
28416 KB |
Output is correct |