#include <bits/stdc++.h>
using namespace std;
const int mxN = 2*1e5;
set<pair<long long int, int>> all[mxN];
int p[mxN];
long long int add[mxN];
long long int a2[mxN];
long long int res;
long long int k;
void upd(int c){
while (p[c] != p[p[c]]){
p[c] = p[p[c]];
}
}
void comb(int a, int b){
if (all[a].size() > all[b].size()){
swap(a,b);
}
for (auto it = all[a].begin(); it != all[a].end(); it++){
long long int val = (*it).first + add[a] - add[b];
int nl = (*it).second + a2[a] - a2[b];
long long int needed = k - val - 2*add[b];
auto it2 = all[b].lower_bound({needed,-1 * 1e9});
if ((it2 != all[b].end()) && ((*it2).first == needed)){
res = min(res, nl + (*it2).second + 2 * a2[b]);
}
// all[b].insert({val,nl});
// cout << "HERERE " << a << " "<<b<< ": " << val+add[b] << " " << nl + a2[b] <<'\n';
}
for (auto it = all[a].begin(); it != all[a].end(); it++){
long long int val = (*it).first + add[a] - add[b];
int nl = (*it).second + a2[a] - a2[b];
all[b].insert({val,nl});
}
p[a] = b;
}
void dfs(int node, int parent, vector<vector<pair<int, long long int>>> &connect){
p[node] = node;
add[node] = 0;
a2[node] = 0;
all[node].clear();
int next;
for (int i =0; i < connect[node].size(); i++){
next = connect[node][i].first;
if (next == parent){
continue;
}
dfs(next,node,connect);
upd(node);
upd(next);
int a= p[node];
int b = p[next];
add[b] += connect[node][i].second;
a2[b] += 1;
auto it = all[b].lower_bound({k-add[b],-1*1e9});
if ((it != all[b].end()) && ((*it).first == k-add[b])){
res = min(res, (*it).second + a2[b]);
}
comb(a,b);
}
upd(node);
all[p[node]].insert({-1*add[p[node]],-1*a2[p[node]]});
/* cout<< node << ":\n";
for (auto it = all[p[node]].begin(); it != all[p[node]].end(); it++){
cout << (*it).first+add[p[node]] << "," << (*it).second+a2[p[node]] << ' ';
}
cout << '\n';*/
}
int best_path(int n,int ck, int h[][2], int l[]){
if (ck == 0){
return 0;
}
vector<vector<pair<int,long long int>>> connect(n);
k = ck;
for (int i =0; i < n-1; i++){
connect[h[i][0]].push_back({h[i][1],l[i]});
connect[h[i][1]].push_back({h[i][0],l[i]});
}
/* for (int i =0; i < n; i++){
cout << i << ": ";
for (int j = 0; j < connect[i].size();j++){
cout<<connect[i][j].first<<","<<connect[i][j].second<<" ";
}
cout<<'\n';
}*/
res = 1e9;
dfs(0,-1,connect);
if (res == 1e9){
return -1;
}
return res;
}
Compilation message
race.cpp: In function 'void dfs(int, int, std::vector<std::vector<std::pair<int, long long int> > >&)':
race.cpp:55:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for (int i =0; i < connect[node].size(); i++){
| ~~^~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9752 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9684 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
4 ms |
9752 KB |
Output is correct |
15 |
Correct |
5 ms |
9648 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
4 ms |
9684 KB |
Output is correct |
18 |
Correct |
4 ms |
9684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9752 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9684 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
4 ms |
9752 KB |
Output is correct |
15 |
Correct |
5 ms |
9648 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
4 ms |
9684 KB |
Output is correct |
18 |
Correct |
4 ms |
9684 KB |
Output is correct |
19 |
Correct |
4 ms |
9684 KB |
Output is correct |
20 |
Correct |
4 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9940 KB |
Output is correct |
22 |
Correct |
5 ms |
9940 KB |
Output is correct |
23 |
Correct |
5 ms |
9940 KB |
Output is correct |
24 |
Correct |
5 ms |
9940 KB |
Output is correct |
25 |
Correct |
6 ms |
10068 KB |
Output is correct |
26 |
Correct |
6 ms |
9940 KB |
Output is correct |
27 |
Correct |
6 ms |
9824 KB |
Output is correct |
28 |
Correct |
6 ms |
9940 KB |
Output is correct |
29 |
Correct |
5 ms |
9940 KB |
Output is correct |
30 |
Correct |
5 ms |
9940 KB |
Output is correct |
31 |
Correct |
5 ms |
9940 KB |
Output is correct |
32 |
Correct |
5 ms |
9940 KB |
Output is correct |
33 |
Correct |
5 ms |
9940 KB |
Output is correct |
34 |
Correct |
5 ms |
9940 KB |
Output is correct |
35 |
Correct |
5 ms |
9940 KB |
Output is correct |
36 |
Correct |
5 ms |
9940 KB |
Output is correct |
37 |
Correct |
5 ms |
9812 KB |
Output is correct |
38 |
Correct |
5 ms |
9900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9752 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9684 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
4 ms |
9752 KB |
Output is correct |
15 |
Correct |
5 ms |
9648 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
4 ms |
9684 KB |
Output is correct |
18 |
Correct |
4 ms |
9684 KB |
Output is correct |
19 |
Correct |
111 ms |
36428 KB |
Output is correct |
20 |
Correct |
113 ms |
36368 KB |
Output is correct |
21 |
Correct |
118 ms |
36000 KB |
Output is correct |
22 |
Correct |
112 ms |
34988 KB |
Output is correct |
23 |
Correct |
136 ms |
49856 KB |
Output is correct |
24 |
Correct |
100 ms |
38360 KB |
Output is correct |
25 |
Correct |
92 ms |
35560 KB |
Output is correct |
26 |
Correct |
73 ms |
38572 KB |
Output is correct |
27 |
Correct |
150 ms |
47288 KB |
Output is correct |
28 |
Correct |
193 ms |
67544 KB |
Output is correct |
29 |
Correct |
187 ms |
66648 KB |
Output is correct |
30 |
Correct |
178 ms |
47280 KB |
Output is correct |
31 |
Correct |
152 ms |
47180 KB |
Output is correct |
32 |
Correct |
201 ms |
47196 KB |
Output is correct |
33 |
Correct |
152 ms |
42732 KB |
Output is correct |
34 |
Correct |
247 ms |
74664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9752 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
5 ms |
9684 KB |
Output is correct |
5 |
Correct |
5 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
4 ms |
9684 KB |
Output is correct |
11 |
Correct |
6 ms |
9684 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
4 ms |
9752 KB |
Output is correct |
15 |
Correct |
5 ms |
9648 KB |
Output is correct |
16 |
Correct |
5 ms |
9684 KB |
Output is correct |
17 |
Correct |
4 ms |
9684 KB |
Output is correct |
18 |
Correct |
4 ms |
9684 KB |
Output is correct |
19 |
Correct |
4 ms |
9684 KB |
Output is correct |
20 |
Correct |
4 ms |
9684 KB |
Output is correct |
21 |
Correct |
5 ms |
9940 KB |
Output is correct |
22 |
Correct |
5 ms |
9940 KB |
Output is correct |
23 |
Correct |
5 ms |
9940 KB |
Output is correct |
24 |
Correct |
5 ms |
9940 KB |
Output is correct |
25 |
Correct |
6 ms |
10068 KB |
Output is correct |
26 |
Correct |
6 ms |
9940 KB |
Output is correct |
27 |
Correct |
6 ms |
9824 KB |
Output is correct |
28 |
Correct |
6 ms |
9940 KB |
Output is correct |
29 |
Correct |
5 ms |
9940 KB |
Output is correct |
30 |
Correct |
5 ms |
9940 KB |
Output is correct |
31 |
Correct |
5 ms |
9940 KB |
Output is correct |
32 |
Correct |
5 ms |
9940 KB |
Output is correct |
33 |
Correct |
5 ms |
9940 KB |
Output is correct |
34 |
Correct |
5 ms |
9940 KB |
Output is correct |
35 |
Correct |
5 ms |
9940 KB |
Output is correct |
36 |
Correct |
5 ms |
9940 KB |
Output is correct |
37 |
Correct |
5 ms |
9812 KB |
Output is correct |
38 |
Correct |
5 ms |
9900 KB |
Output is correct |
39 |
Correct |
111 ms |
36428 KB |
Output is correct |
40 |
Correct |
113 ms |
36368 KB |
Output is correct |
41 |
Correct |
118 ms |
36000 KB |
Output is correct |
42 |
Correct |
112 ms |
34988 KB |
Output is correct |
43 |
Correct |
136 ms |
49856 KB |
Output is correct |
44 |
Correct |
100 ms |
38360 KB |
Output is correct |
45 |
Correct |
92 ms |
35560 KB |
Output is correct |
46 |
Correct |
73 ms |
38572 KB |
Output is correct |
47 |
Correct |
150 ms |
47288 KB |
Output is correct |
48 |
Correct |
193 ms |
67544 KB |
Output is correct |
49 |
Correct |
187 ms |
66648 KB |
Output is correct |
50 |
Correct |
178 ms |
47280 KB |
Output is correct |
51 |
Correct |
152 ms |
47180 KB |
Output is correct |
52 |
Correct |
201 ms |
47196 KB |
Output is correct |
53 |
Correct |
152 ms |
42732 KB |
Output is correct |
54 |
Correct |
247 ms |
74664 KB |
Output is correct |
55 |
Correct |
15 ms |
13140 KB |
Output is correct |
56 |
Correct |
11 ms |
11856 KB |
Output is correct |
57 |
Correct |
62 ms |
31868 KB |
Output is correct |
58 |
Correct |
45 ms |
26172 KB |
Output is correct |
59 |
Correct |
69 ms |
38732 KB |
Output is correct |
60 |
Correct |
186 ms |
66912 KB |
Output is correct |
61 |
Correct |
178 ms |
52920 KB |
Output is correct |
62 |
Correct |
146 ms |
47224 KB |
Output is correct |
63 |
Correct |
201 ms |
47244 KB |
Output is correct |
64 |
Correct |
343 ms |
102864 KB |
Output is correct |
65 |
Correct |
345 ms |
99496 KB |
Output is correct |
66 |
Correct |
206 ms |
67840 KB |
Output is correct |
67 |
Correct |
141 ms |
46068 KB |
Output is correct |
68 |
Correct |
244 ms |
67052 KB |
Output is correct |
69 |
Correct |
279 ms |
72684 KB |
Output is correct |
70 |
Correct |
245 ms |
64644 KB |
Output is correct |