#include <bits/stdc++.h>
#define pb push_back
#include "race.h"
#define endl "\n"
#define fastio cin.tie(0); ios_base::sync_with_stdio(false);
typedef long long ll;
using namespace std;
const int mod = 1e9 + 7;
const int MAXN = 2e5 + 5;
vector<pair<int, int> > adj[MAXN];
vector<int> sub(MAXN), vis(MAXN);
void subtreecalc(int node, int par){
sub[node] = 1;
for(auto itr: adj[node]){
if(itr.first != par && !vis[itr.first]){
subtreecalc(itr.first, node);
sub[node] += sub[itr.first];
}
}
}
int find_centroid(int node, int par, int sz){
for(auto itr: adj[node]){
if(vis[itr.first] || itr.first == par) continue;
if(sub[itr.first] > sz/2) return find_centroid(itr.first, node, sz);
}
return node;
}
void dfs(int node, int par, int depth, int len, map<int, int> &mpp){
if(vis[node]) return;
if(!mpp[depth]) mpp[depth] = len;
else mpp[depth] = min(mpp[depth], len);
for(auto itr: adj[node]){
if(vis[itr.first] || itr.first == par) continue;
dfs(itr.first, node, depth + itr.second, len + 1, mpp);
}
}
bool compare(pair<int, int> p1, pair<int, int> p2){
return (sub[p1.first] > sub[p2.first]);
}
int best_path(int N, int K, int H[][2], int L[]){
for(int i = 0; i < N-1; i++){
adj[H[i][0]].pb({H[i][1], L[i]});
adj[H[i][1]].pb({H[i][0], L[i]});
}
int ans = 1e9;
vector<int> roots, new_roots;
roots.pb(0);
int cnt = 0;
while(cnt < N){
for(auto i: roots) subtreecalc(i, i);
for(auto i: roots){
int ct = find_centroid(i, i, sub[i]);
sort(adj[ct].begin(), adj[ct].end(), compare);
int ind = 0;
map<int, int> pre, now;
while(ind < adj[ct].size()){
if(vis[adj[ct][ind].first]){
ind++;
continue;
}
dfs(adj[ct][ind].first, ct, adj[ct][ind].second, 1, now);
for(auto itr: now){
if(pre[K - itr.first]) ans = min(ans, pre[K - itr.first] + now[itr.first]);
if(itr.first == K) ans = min(ans, itr.second);
}
for(auto itr: now){
if(!pre[itr.first] && itr.first != 0) pre[itr.first] = itr.second;
else pre[itr.first] = min(pre[itr.first], now[itr.first]);
}
now.clear();
ind++;
}
vis[ct] = 1;
for(auto itr: adj[ct]){
if(!vis[itr.first]) new_roots.pb(itr.first);
}
cnt++;
}
roots = new_roots;
new_roots.clear();
}
if(ans == 1e9) return -1;
else return ans;
}
Compilation message
race.cpp: In function 'int best_path(int, int, int (*)[2], int*)':
race.cpp:67:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | while(ind < adj[ct].size()){
| ~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10332 KB |
Output is correct |
2 |
Correct |
3 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10332 KB |
Output is correct |
4 |
Correct |
2 ms |
10332 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10328 KB |
Output is correct |
7 |
Correct |
2 ms |
10172 KB |
Output is correct |
8 |
Correct |
2 ms |
10332 KB |
Output is correct |
9 |
Correct |
2 ms |
10332 KB |
Output is correct |
10 |
Correct |
2 ms |
10328 KB |
Output is correct |
11 |
Correct |
2 ms |
10328 KB |
Output is correct |
12 |
Correct |
2 ms |
10332 KB |
Output is correct |
13 |
Correct |
3 ms |
10332 KB |
Output is correct |
14 |
Correct |
2 ms |
10332 KB |
Output is correct |
15 |
Correct |
2 ms |
10332 KB |
Output is correct |
16 |
Correct |
2 ms |
10332 KB |
Output is correct |
17 |
Correct |
2 ms |
10332 KB |
Output is correct |
18 |
Correct |
2 ms |
10332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10332 KB |
Output is correct |
2 |
Correct |
3 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10332 KB |
Output is correct |
4 |
Correct |
2 ms |
10332 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10328 KB |
Output is correct |
7 |
Correct |
2 ms |
10172 KB |
Output is correct |
8 |
Correct |
2 ms |
10332 KB |
Output is correct |
9 |
Correct |
2 ms |
10332 KB |
Output is correct |
10 |
Correct |
2 ms |
10328 KB |
Output is correct |
11 |
Correct |
2 ms |
10328 KB |
Output is correct |
12 |
Correct |
2 ms |
10332 KB |
Output is correct |
13 |
Correct |
3 ms |
10332 KB |
Output is correct |
14 |
Correct |
2 ms |
10332 KB |
Output is correct |
15 |
Correct |
2 ms |
10332 KB |
Output is correct |
16 |
Correct |
2 ms |
10332 KB |
Output is correct |
17 |
Correct |
2 ms |
10332 KB |
Output is correct |
18 |
Correct |
2 ms |
10332 KB |
Output is correct |
19 |
Correct |
2 ms |
10328 KB |
Output is correct |
20 |
Correct |
2 ms |
10332 KB |
Output is correct |
21 |
Correct |
4 ms |
10424 KB |
Output is correct |
22 |
Correct |
4 ms |
10420 KB |
Output is correct |
23 |
Correct |
5 ms |
10332 KB |
Output is correct |
24 |
Correct |
4 ms |
10332 KB |
Output is correct |
25 |
Correct |
4 ms |
10328 KB |
Output is correct |
26 |
Correct |
4 ms |
10584 KB |
Output is correct |
27 |
Correct |
3 ms |
10332 KB |
Output is correct |
28 |
Correct |
4 ms |
10332 KB |
Output is correct |
29 |
Correct |
4 ms |
10328 KB |
Output is correct |
30 |
Correct |
4 ms |
10332 KB |
Output is correct |
31 |
Correct |
4 ms |
10332 KB |
Output is correct |
32 |
Correct |
4 ms |
10332 KB |
Output is correct |
33 |
Correct |
6 ms |
10332 KB |
Output is correct |
34 |
Correct |
4 ms |
10520 KB |
Output is correct |
35 |
Correct |
4 ms |
10480 KB |
Output is correct |
36 |
Correct |
5 ms |
10332 KB |
Output is correct |
37 |
Correct |
4 ms |
10328 KB |
Output is correct |
38 |
Correct |
4 ms |
10332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10332 KB |
Output is correct |
2 |
Correct |
3 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10332 KB |
Output is correct |
4 |
Correct |
2 ms |
10332 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10328 KB |
Output is correct |
7 |
Correct |
2 ms |
10172 KB |
Output is correct |
8 |
Correct |
2 ms |
10332 KB |
Output is correct |
9 |
Correct |
2 ms |
10332 KB |
Output is correct |
10 |
Correct |
2 ms |
10328 KB |
Output is correct |
11 |
Correct |
2 ms |
10328 KB |
Output is correct |
12 |
Correct |
2 ms |
10332 KB |
Output is correct |
13 |
Correct |
3 ms |
10332 KB |
Output is correct |
14 |
Correct |
2 ms |
10332 KB |
Output is correct |
15 |
Correct |
2 ms |
10332 KB |
Output is correct |
16 |
Correct |
2 ms |
10332 KB |
Output is correct |
17 |
Correct |
2 ms |
10332 KB |
Output is correct |
18 |
Correct |
2 ms |
10332 KB |
Output is correct |
19 |
Correct |
246 ms |
16496 KB |
Output is correct |
20 |
Correct |
219 ms |
16468 KB |
Output is correct |
21 |
Correct |
213 ms |
16464 KB |
Output is correct |
22 |
Correct |
183 ms |
16468 KB |
Output is correct |
23 |
Correct |
355 ms |
17104 KB |
Output is correct |
24 |
Correct |
148 ms |
17100 KB |
Output is correct |
25 |
Correct |
160 ms |
21132 KB |
Output is correct |
26 |
Correct |
74 ms |
22288 KB |
Output is correct |
27 |
Correct |
337 ms |
20492 KB |
Output is correct |
28 |
Correct |
1258 ms |
54608 KB |
Output is correct |
29 |
Correct |
1195 ms |
53540 KB |
Output is correct |
30 |
Correct |
399 ms |
20688 KB |
Output is correct |
31 |
Correct |
368 ms |
20700 KB |
Output is correct |
32 |
Correct |
524 ms |
20676 KB |
Output is correct |
33 |
Correct |
568 ms |
19564 KB |
Output is correct |
34 |
Correct |
1197 ms |
40208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10332 KB |
Output is correct |
2 |
Correct |
3 ms |
10332 KB |
Output is correct |
3 |
Correct |
2 ms |
10332 KB |
Output is correct |
4 |
Correct |
2 ms |
10332 KB |
Output is correct |
5 |
Correct |
2 ms |
10332 KB |
Output is correct |
6 |
Correct |
2 ms |
10328 KB |
Output is correct |
7 |
Correct |
2 ms |
10172 KB |
Output is correct |
8 |
Correct |
2 ms |
10332 KB |
Output is correct |
9 |
Correct |
2 ms |
10332 KB |
Output is correct |
10 |
Correct |
2 ms |
10328 KB |
Output is correct |
11 |
Correct |
2 ms |
10328 KB |
Output is correct |
12 |
Correct |
2 ms |
10332 KB |
Output is correct |
13 |
Correct |
3 ms |
10332 KB |
Output is correct |
14 |
Correct |
2 ms |
10332 KB |
Output is correct |
15 |
Correct |
2 ms |
10332 KB |
Output is correct |
16 |
Correct |
2 ms |
10332 KB |
Output is correct |
17 |
Correct |
2 ms |
10332 KB |
Output is correct |
18 |
Correct |
2 ms |
10332 KB |
Output is correct |
19 |
Correct |
2 ms |
10328 KB |
Output is correct |
20 |
Correct |
2 ms |
10332 KB |
Output is correct |
21 |
Correct |
4 ms |
10424 KB |
Output is correct |
22 |
Correct |
4 ms |
10420 KB |
Output is correct |
23 |
Correct |
5 ms |
10332 KB |
Output is correct |
24 |
Correct |
4 ms |
10332 KB |
Output is correct |
25 |
Correct |
4 ms |
10328 KB |
Output is correct |
26 |
Correct |
4 ms |
10584 KB |
Output is correct |
27 |
Correct |
3 ms |
10332 KB |
Output is correct |
28 |
Correct |
4 ms |
10332 KB |
Output is correct |
29 |
Correct |
4 ms |
10328 KB |
Output is correct |
30 |
Correct |
4 ms |
10332 KB |
Output is correct |
31 |
Correct |
4 ms |
10332 KB |
Output is correct |
32 |
Correct |
4 ms |
10332 KB |
Output is correct |
33 |
Correct |
6 ms |
10332 KB |
Output is correct |
34 |
Correct |
4 ms |
10520 KB |
Output is correct |
35 |
Correct |
4 ms |
10480 KB |
Output is correct |
36 |
Correct |
5 ms |
10332 KB |
Output is correct |
37 |
Correct |
4 ms |
10328 KB |
Output is correct |
38 |
Correct |
4 ms |
10332 KB |
Output is correct |
39 |
Correct |
246 ms |
16496 KB |
Output is correct |
40 |
Correct |
219 ms |
16468 KB |
Output is correct |
41 |
Correct |
213 ms |
16464 KB |
Output is correct |
42 |
Correct |
183 ms |
16468 KB |
Output is correct |
43 |
Correct |
355 ms |
17104 KB |
Output is correct |
44 |
Correct |
148 ms |
17100 KB |
Output is correct |
45 |
Correct |
160 ms |
21132 KB |
Output is correct |
46 |
Correct |
74 ms |
22288 KB |
Output is correct |
47 |
Correct |
337 ms |
20492 KB |
Output is correct |
48 |
Correct |
1258 ms |
54608 KB |
Output is correct |
49 |
Correct |
1195 ms |
53540 KB |
Output is correct |
50 |
Correct |
399 ms |
20688 KB |
Output is correct |
51 |
Correct |
368 ms |
20700 KB |
Output is correct |
52 |
Correct |
524 ms |
20676 KB |
Output is correct |
53 |
Correct |
568 ms |
19564 KB |
Output is correct |
54 |
Correct |
1197 ms |
40208 KB |
Output is correct |
55 |
Correct |
28 ms |
11356 KB |
Output is correct |
56 |
Correct |
17 ms |
11104 KB |
Output is correct |
57 |
Correct |
126 ms |
16876 KB |
Output is correct |
58 |
Correct |
43 ms |
17352 KB |
Output is correct |
59 |
Correct |
456 ms |
31064 KB |
Output is correct |
60 |
Correct |
1254 ms |
52004 KB |
Output is correct |
61 |
Correct |
427 ms |
22612 KB |
Output is correct |
62 |
Correct |
322 ms |
20564 KB |
Output is correct |
63 |
Correct |
363 ms |
20564 KB |
Output is correct |
64 |
Correct |
1313 ms |
30596 KB |
Output is correct |
65 |
Correct |
984 ms |
38628 KB |
Output is correct |
66 |
Correct |
1207 ms |
51644 KB |
Output is correct |
67 |
Correct |
129 ms |
22468 KB |
Output is correct |
68 |
Correct |
504 ms |
36184 KB |
Output is correct |
69 |
Correct |
561 ms |
36688 KB |
Output is correct |
70 |
Correct |
485 ms |
35156 KB |
Output is correct |