#include "race.h"
#include <bits/stdc++.h>
#define fi first
#define se second
#define all(v) v.begin(), v.end()
#define eb emplace_back
#define INF 2000000000
using namespace std;
typedef long long ll;
typedef pair <int, ll> pii;
bool chk[202020];
int c[202020], dep[202020];
vector <pii> g[202020];
vector <int> ct[202020];
int dfs(int x, int pa) {
c[x] = 1;
for(auto i : g[x]) {
if(i.fi == pa) continue;
c[x] += dfs(i.fi, x);
}
return c[x];
}
int find_cen(int x, int pa){
int sz = 1, mx = 0, mxi;
for(auto i : g[x]){
if(chk[i.fi]) continue;
sz += c[i.fi];
if(c[i.fi] > mx){
mx = c[i.fi];
mxi = i.fi;
}
}
if(mx <= sz/2) return x;
c[x]= sz - mx;
return find_cen(mxi, x);
}
int make_ct(int cur, int pa){
int x = find_cen(cur, pa);
chk[x] = true;
for(auto i : g[x]){
if(chk[i.fi]) continue;
ct[x].eb(make_ct(i.fi, x));
}
return x;
}
int ans = INF;
ll k;
void get_ans(int x) {
chk[x] = true;
map <int, int> mp;
mp.insert({0, 0});
for(auto i : g[x]) {
if(chk[i.fi]) continue;
map <int, int> tmp;
queue <pair <pii, int> > q;
q.push({i, x});
int cnt = 1;
while(!q.empty()) {
int sz = q.size();
while(sz--) {
int v = q.front().fi.fi, dis = q.front().fi.se, pa = q.front().se;
q.pop();
if(dis > k) continue;
if(tmp.find(dis) == tmp.end()) tmp.insert({dis, cnt});
if(mp.find(k-dis) != mp.end()) ans = min(ans, mp[k-dis] + cnt);
for(auto i : g[v]) {
if(i.fi == pa || chk[i.fi]) continue;
q.push({{i.fi, dis+i.se}, v});
}
}
cnt++;
}
for(auto it = tmp.begin(); it != tmp.end(); it++) {
if(mp.find(it->fi) == mp.end()) mp.insert(*it);
else mp[it->fi] = min(mp[it->fi], it->se);
}
}
for(auto i : ct[x]) {
if(chk[i]) continue;
get_ans(i);
}
}
int best_path(int n, int K, int H[][2], int L[]) {
k = (ll)K;
for(int i = 1; i < n; i++) {
int u, v, w;
u = H[i-1][0] + 1, v = H[i-1][1] + 1, w = L[i-1];
g[u].eb(v, w), g[v].eb(u, w);
}
dfs(1, 0);
int ctr = make_ct(1, 0);
for(int i = 1; i <= n; i++) {
chk[i] = false;
}
get_ans(ctr);
if(ans == INF) return -1;
else return ans;
}
Compilation message
race.cpp: In function 'int find_cen(int, int)':
race.cpp:28:25: warning: 'mxi' may be used uninitialized in this function [-Wmaybe-uninitialized]
int sz = 1, mx = 0, mxi;
^~~
race.cpp: In function 'int make_ct(int, int)':
race.cpp:49:12: warning: 'mxi' may be used uninitialized in this function [-Wmaybe-uninitialized]
return x;
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9848 KB |
Output is correct |
2 |
Correct |
11 ms |
9848 KB |
Output is correct |
3 |
Correct |
11 ms |
9848 KB |
Output is correct |
4 |
Correct |
11 ms |
9848 KB |
Output is correct |
5 |
Correct |
11 ms |
9848 KB |
Output is correct |
6 |
Correct |
18 ms |
9848 KB |
Output is correct |
7 |
Correct |
11 ms |
9848 KB |
Output is correct |
8 |
Correct |
12 ms |
9848 KB |
Output is correct |
9 |
Correct |
11 ms |
9848 KB |
Output is correct |
10 |
Correct |
11 ms |
9848 KB |
Output is correct |
11 |
Correct |
11 ms |
9848 KB |
Output is correct |
12 |
Correct |
11 ms |
9848 KB |
Output is correct |
13 |
Correct |
11 ms |
9848 KB |
Output is correct |
14 |
Correct |
11 ms |
9848 KB |
Output is correct |
15 |
Correct |
11 ms |
9848 KB |
Output is correct |
16 |
Correct |
11 ms |
9848 KB |
Output is correct |
17 |
Correct |
11 ms |
9848 KB |
Output is correct |
18 |
Correct |
11 ms |
9848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9848 KB |
Output is correct |
2 |
Correct |
11 ms |
9848 KB |
Output is correct |
3 |
Correct |
11 ms |
9848 KB |
Output is correct |
4 |
Correct |
11 ms |
9848 KB |
Output is correct |
5 |
Correct |
11 ms |
9848 KB |
Output is correct |
6 |
Correct |
18 ms |
9848 KB |
Output is correct |
7 |
Correct |
11 ms |
9848 KB |
Output is correct |
8 |
Correct |
12 ms |
9848 KB |
Output is correct |
9 |
Correct |
11 ms |
9848 KB |
Output is correct |
10 |
Correct |
11 ms |
9848 KB |
Output is correct |
11 |
Correct |
11 ms |
9848 KB |
Output is correct |
12 |
Correct |
11 ms |
9848 KB |
Output is correct |
13 |
Correct |
11 ms |
9848 KB |
Output is correct |
14 |
Correct |
11 ms |
9848 KB |
Output is correct |
15 |
Correct |
11 ms |
9848 KB |
Output is correct |
16 |
Correct |
11 ms |
9848 KB |
Output is correct |
17 |
Correct |
11 ms |
9848 KB |
Output is correct |
18 |
Correct |
11 ms |
9848 KB |
Output is correct |
19 |
Correct |
11 ms |
9848 KB |
Output is correct |
20 |
Correct |
11 ms |
9848 KB |
Output is correct |
21 |
Correct |
14 ms |
9976 KB |
Output is correct |
22 |
Correct |
12 ms |
9848 KB |
Output is correct |
23 |
Correct |
12 ms |
9976 KB |
Output is correct |
24 |
Correct |
12 ms |
9976 KB |
Output is correct |
25 |
Correct |
13 ms |
9976 KB |
Output is correct |
26 |
Correct |
12 ms |
9976 KB |
Output is correct |
27 |
Correct |
12 ms |
9976 KB |
Output is correct |
28 |
Correct |
13 ms |
9976 KB |
Output is correct |
29 |
Correct |
13 ms |
9976 KB |
Output is correct |
30 |
Correct |
13 ms |
9976 KB |
Output is correct |
31 |
Correct |
16 ms |
10104 KB |
Output is correct |
32 |
Correct |
17 ms |
9980 KB |
Output is correct |
33 |
Correct |
16 ms |
9976 KB |
Output is correct |
34 |
Correct |
15 ms |
9976 KB |
Output is correct |
35 |
Correct |
14 ms |
9976 KB |
Output is correct |
36 |
Correct |
15 ms |
9976 KB |
Output is correct |
37 |
Correct |
13 ms |
9976 KB |
Output is correct |
38 |
Correct |
13 ms |
9976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9848 KB |
Output is correct |
2 |
Correct |
11 ms |
9848 KB |
Output is correct |
3 |
Correct |
11 ms |
9848 KB |
Output is correct |
4 |
Correct |
11 ms |
9848 KB |
Output is correct |
5 |
Correct |
11 ms |
9848 KB |
Output is correct |
6 |
Correct |
18 ms |
9848 KB |
Output is correct |
7 |
Correct |
11 ms |
9848 KB |
Output is correct |
8 |
Correct |
12 ms |
9848 KB |
Output is correct |
9 |
Correct |
11 ms |
9848 KB |
Output is correct |
10 |
Correct |
11 ms |
9848 KB |
Output is correct |
11 |
Correct |
11 ms |
9848 KB |
Output is correct |
12 |
Correct |
11 ms |
9848 KB |
Output is correct |
13 |
Correct |
11 ms |
9848 KB |
Output is correct |
14 |
Correct |
11 ms |
9848 KB |
Output is correct |
15 |
Correct |
11 ms |
9848 KB |
Output is correct |
16 |
Correct |
11 ms |
9848 KB |
Output is correct |
17 |
Correct |
11 ms |
9848 KB |
Output is correct |
18 |
Correct |
11 ms |
9848 KB |
Output is correct |
19 |
Correct |
297 ms |
19452 KB |
Output is correct |
20 |
Correct |
305 ms |
19460 KB |
Output is correct |
21 |
Correct |
303 ms |
19576 KB |
Output is correct |
22 |
Correct |
306 ms |
19704 KB |
Output is correct |
23 |
Correct |
194 ms |
20220 KB |
Output is correct |
24 |
Correct |
153 ms |
18372 KB |
Output is correct |
25 |
Correct |
236 ms |
22136 KB |
Output is correct |
26 |
Correct |
136 ms |
24440 KB |
Output is correct |
27 |
Correct |
420 ms |
28968 KB |
Output is correct |
28 |
Correct |
504 ms |
39196 KB |
Output is correct |
29 |
Correct |
470 ms |
38392 KB |
Output is correct |
30 |
Correct |
365 ms |
28920 KB |
Output is correct |
31 |
Correct |
369 ms |
28920 KB |
Output is correct |
32 |
Correct |
405 ms |
29048 KB |
Output is correct |
33 |
Correct |
590 ms |
29064 KB |
Output is correct |
34 |
Correct |
342 ms |
30072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9848 KB |
Output is correct |
2 |
Correct |
11 ms |
9848 KB |
Output is correct |
3 |
Correct |
11 ms |
9848 KB |
Output is correct |
4 |
Correct |
11 ms |
9848 KB |
Output is correct |
5 |
Correct |
11 ms |
9848 KB |
Output is correct |
6 |
Correct |
18 ms |
9848 KB |
Output is correct |
7 |
Correct |
11 ms |
9848 KB |
Output is correct |
8 |
Correct |
12 ms |
9848 KB |
Output is correct |
9 |
Correct |
11 ms |
9848 KB |
Output is correct |
10 |
Correct |
11 ms |
9848 KB |
Output is correct |
11 |
Correct |
11 ms |
9848 KB |
Output is correct |
12 |
Correct |
11 ms |
9848 KB |
Output is correct |
13 |
Correct |
11 ms |
9848 KB |
Output is correct |
14 |
Correct |
11 ms |
9848 KB |
Output is correct |
15 |
Correct |
11 ms |
9848 KB |
Output is correct |
16 |
Correct |
11 ms |
9848 KB |
Output is correct |
17 |
Correct |
11 ms |
9848 KB |
Output is correct |
18 |
Correct |
11 ms |
9848 KB |
Output is correct |
19 |
Correct |
11 ms |
9848 KB |
Output is correct |
20 |
Correct |
11 ms |
9848 KB |
Output is correct |
21 |
Correct |
14 ms |
9976 KB |
Output is correct |
22 |
Correct |
12 ms |
9848 KB |
Output is correct |
23 |
Correct |
12 ms |
9976 KB |
Output is correct |
24 |
Correct |
12 ms |
9976 KB |
Output is correct |
25 |
Correct |
13 ms |
9976 KB |
Output is correct |
26 |
Correct |
12 ms |
9976 KB |
Output is correct |
27 |
Correct |
12 ms |
9976 KB |
Output is correct |
28 |
Correct |
13 ms |
9976 KB |
Output is correct |
29 |
Correct |
13 ms |
9976 KB |
Output is correct |
30 |
Correct |
13 ms |
9976 KB |
Output is correct |
31 |
Correct |
16 ms |
10104 KB |
Output is correct |
32 |
Correct |
17 ms |
9980 KB |
Output is correct |
33 |
Correct |
16 ms |
9976 KB |
Output is correct |
34 |
Correct |
15 ms |
9976 KB |
Output is correct |
35 |
Correct |
14 ms |
9976 KB |
Output is correct |
36 |
Correct |
15 ms |
9976 KB |
Output is correct |
37 |
Correct |
13 ms |
9976 KB |
Output is correct |
38 |
Correct |
13 ms |
9976 KB |
Output is correct |
39 |
Correct |
297 ms |
19452 KB |
Output is correct |
40 |
Correct |
305 ms |
19460 KB |
Output is correct |
41 |
Correct |
303 ms |
19576 KB |
Output is correct |
42 |
Correct |
306 ms |
19704 KB |
Output is correct |
43 |
Correct |
194 ms |
20220 KB |
Output is correct |
44 |
Correct |
153 ms |
18372 KB |
Output is correct |
45 |
Correct |
236 ms |
22136 KB |
Output is correct |
46 |
Correct |
136 ms |
24440 KB |
Output is correct |
47 |
Correct |
420 ms |
28968 KB |
Output is correct |
48 |
Correct |
504 ms |
39196 KB |
Output is correct |
49 |
Correct |
470 ms |
38392 KB |
Output is correct |
50 |
Correct |
365 ms |
28920 KB |
Output is correct |
51 |
Correct |
369 ms |
28920 KB |
Output is correct |
52 |
Correct |
405 ms |
29048 KB |
Output is correct |
53 |
Correct |
590 ms |
29064 KB |
Output is correct |
54 |
Correct |
342 ms |
30072 KB |
Output is correct |
55 |
Correct |
42 ms |
11152 KB |
Output is correct |
56 |
Correct |
29 ms |
10792 KB |
Output is correct |
57 |
Correct |
190 ms |
20316 KB |
Output is correct |
58 |
Correct |
81 ms |
18148 KB |
Output is correct |
59 |
Correct |
570 ms |
31608 KB |
Output is correct |
60 |
Correct |
1854 ms |
55812 KB |
Output is correct |
61 |
Correct |
511 ms |
29336 KB |
Output is correct |
62 |
Correct |
529 ms |
29304 KB |
Output is correct |
63 |
Correct |
639 ms |
29288 KB |
Output is correct |
64 |
Correct |
1446 ms |
41256 KB |
Output is correct |
65 |
Correct |
351 ms |
30228 KB |
Output is correct |
66 |
Correct |
1109 ms |
38520 KB |
Output is correct |
67 |
Correct |
248 ms |
27740 KB |
Output is correct |
68 |
Correct |
690 ms |
35764 KB |
Output is correct |
69 |
Correct |
789 ms |
36060 KB |
Output is correct |
70 |
Correct |
660 ms |
34808 KB |
Output is correct |