#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<int, int> pp;
const int MAXN = 2 * 1e5 + 100;
const int INF = 2 * 1e9;
struct mapa{
int val = INF;
};
int n; int k;
vector<pp> v[MAXN];
int p[MAXN] = { 0 };
int vel[MAXN] = { 0 };
map<int, mapa> m;
vector<int> s;
int sol = INF;
int cnt = 0;
int H[MAXN][2];
int L[MAXN];
int dfs(int x, int depth, ll sum, int val, int k){
p[x] = 1; vel[x] = 0;
if (val == 1 && sum <= k){
int zb = depth + m[k - sum].val - 1;
sol = min(sol, zb);
}
else if (val == 2 && sum <= k){
s.push_back(sum);
m[sum].val = min(m[sum].val, depth);
}
for (int i = 0; i < v[x].size(); ++i){
int y = v[x][i].first;
if (!p[y]){
int rt = dfs(y, depth + 1, sum + v[x][i].second, val, k);
if (val == 0)
vel[x] += rt;
}
}
if (val == 0)
++vel[x];
p[x] = 0;
if (val == 0)
return vel[x];
return 0;
}
int centr(int x){
p[x] = 1; pp maks = {-1, -1};
for (int i = 0; i < v[x].size(); ++i){
int y = v[x][i].first;
if (!p[y] && vel[y] > maks.second)
maks = {y, vel[y]};
}
if (maks.second <= cnt / 2){
p[x] = 0; return x;
}
else{
int rjes = centr(maks.first);
p[x] = 0; return rjes;
}
}
void solve(int x, int K){
s.clear(); m[0].val = 1;
for (int i = 0; i < v[x].size(); ++i){
int y = v[x][i].first;
if (!p[y]){
dfs(y, 2, v[x][i].second, 1, K); dfs(y, 2, v[x][i].second, 2, K);
}
}
while (!s.empty()){
m[s.back()].val = INF; s.pop_back();
}
return;
}
void decompose(int x, int K){
cnt = 0; dfs(x, 1, 0, 0, K); cnt = vel[x];
int root = centr(x); p[root] = 1;
solve(root, K);
for (int i = 0; i < v[root].size(); ++i){
int y = v[root][i].first;
if (!p[y]) decompose(y, K);
}
}
int best_path(int N, int K, int H[MAXN][2], int L[MAXN]){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
for (int i = 0; i < N - 1; ++i){
v[H[i][0]].push_back({H[i][1], L[i]});
v[H[i][1]].push_back({H[i][0], L[i]});
}
decompose(0, K);
if (sol == INF)
return -1;
else return (sol - 1);
}
Compilation message
race.cpp: In function 'int dfs(int, int, ll, int, int)':
race.cpp:36:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for (int i = 0; i < v[x].size(); ++i){
| ~~^~~~~~~~~~~~~
race.cpp: In function 'int centr(int)':
race.cpp:54:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for (int i = 0; i < v[x].size(); ++i){
| ~~^~~~~~~~~~~~~
race.cpp: In function 'void solve(int, int)':
race.cpp:70:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for (int i = 0; i < v[x].size(); ++i){
| ~~^~~~~~~~~~~~~
race.cpp: In function 'void decompose(int, int)':
race.cpp:86:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for (int i = 0; i < v[root].size(); ++i){
| ~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
3 ms |
5068 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
3 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
5068 KB |
Output is correct |
11 |
Correct |
2 ms |
5068 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
4 ms |
5068 KB |
Output is correct |
16 |
Correct |
4 ms |
5068 KB |
Output is correct |
17 |
Correct |
3 ms |
5068 KB |
Output is correct |
18 |
Correct |
3 ms |
5068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
3 ms |
5068 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
3 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
5068 KB |
Output is correct |
11 |
Correct |
2 ms |
5068 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
4 ms |
5068 KB |
Output is correct |
16 |
Correct |
4 ms |
5068 KB |
Output is correct |
17 |
Correct |
3 ms |
5068 KB |
Output is correct |
18 |
Correct |
3 ms |
5068 KB |
Output is correct |
19 |
Correct |
4 ms |
4940 KB |
Output is correct |
20 |
Correct |
4 ms |
4940 KB |
Output is correct |
21 |
Correct |
5 ms |
5068 KB |
Output is correct |
22 |
Correct |
4 ms |
5196 KB |
Output is correct |
23 |
Correct |
4 ms |
5196 KB |
Output is correct |
24 |
Correct |
4 ms |
5148 KB |
Output is correct |
25 |
Correct |
6 ms |
5452 KB |
Output is correct |
26 |
Correct |
4 ms |
5196 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
5 ms |
5452 KB |
Output is correct |
29 |
Correct |
6 ms |
5452 KB |
Output is correct |
30 |
Correct |
6 ms |
5496 KB |
Output is correct |
31 |
Correct |
7 ms |
5580 KB |
Output is correct |
32 |
Correct |
6 ms |
5580 KB |
Output is correct |
33 |
Correct |
6 ms |
5580 KB |
Output is correct |
34 |
Correct |
6 ms |
5452 KB |
Output is correct |
35 |
Correct |
6 ms |
5452 KB |
Output is correct |
36 |
Correct |
5 ms |
5452 KB |
Output is correct |
37 |
Correct |
6 ms |
5580 KB |
Output is correct |
38 |
Correct |
6 ms |
5580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
3 ms |
5068 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
3 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
5068 KB |
Output is correct |
11 |
Correct |
2 ms |
5068 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
4 ms |
5068 KB |
Output is correct |
16 |
Correct |
4 ms |
5068 KB |
Output is correct |
17 |
Correct |
3 ms |
5068 KB |
Output is correct |
18 |
Correct |
3 ms |
5068 KB |
Output is correct |
19 |
Correct |
275 ms |
10772 KB |
Output is correct |
20 |
Correct |
240 ms |
10608 KB |
Output is correct |
21 |
Correct |
249 ms |
11096 KB |
Output is correct |
22 |
Correct |
224 ms |
11328 KB |
Output is correct |
23 |
Correct |
153 ms |
10876 KB |
Output is correct |
24 |
Correct |
94 ms |
10792 KB |
Output is correct |
25 |
Correct |
220 ms |
16660 KB |
Output is correct |
26 |
Correct |
173 ms |
23232 KB |
Output is correct |
27 |
Correct |
252 ms |
16580 KB |
Output is correct |
28 |
Correct |
558 ms |
40244 KB |
Output is correct |
29 |
Correct |
555 ms |
38340 KB |
Output is correct |
30 |
Correct |
273 ms |
16540 KB |
Output is correct |
31 |
Correct |
254 ms |
16700 KB |
Output is correct |
32 |
Correct |
326 ms |
16612 KB |
Output is correct |
33 |
Correct |
450 ms |
15540 KB |
Output is correct |
34 |
Correct |
338 ms |
15504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
3 ms |
5068 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
4 ms |
5068 KB |
Output is correct |
7 |
Correct |
3 ms |
5068 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
3 ms |
5068 KB |
Output is correct |
10 |
Correct |
3 ms |
5068 KB |
Output is correct |
11 |
Correct |
2 ms |
5068 KB |
Output is correct |
12 |
Correct |
2 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
5068 KB |
Output is correct |
14 |
Correct |
3 ms |
5068 KB |
Output is correct |
15 |
Correct |
4 ms |
5068 KB |
Output is correct |
16 |
Correct |
4 ms |
5068 KB |
Output is correct |
17 |
Correct |
3 ms |
5068 KB |
Output is correct |
18 |
Correct |
3 ms |
5068 KB |
Output is correct |
19 |
Correct |
4 ms |
4940 KB |
Output is correct |
20 |
Correct |
4 ms |
4940 KB |
Output is correct |
21 |
Correct |
5 ms |
5068 KB |
Output is correct |
22 |
Correct |
4 ms |
5196 KB |
Output is correct |
23 |
Correct |
4 ms |
5196 KB |
Output is correct |
24 |
Correct |
4 ms |
5148 KB |
Output is correct |
25 |
Correct |
6 ms |
5452 KB |
Output is correct |
26 |
Correct |
4 ms |
5196 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
5 ms |
5452 KB |
Output is correct |
29 |
Correct |
6 ms |
5452 KB |
Output is correct |
30 |
Correct |
6 ms |
5496 KB |
Output is correct |
31 |
Correct |
7 ms |
5580 KB |
Output is correct |
32 |
Correct |
6 ms |
5580 KB |
Output is correct |
33 |
Correct |
6 ms |
5580 KB |
Output is correct |
34 |
Correct |
6 ms |
5452 KB |
Output is correct |
35 |
Correct |
6 ms |
5452 KB |
Output is correct |
36 |
Correct |
5 ms |
5452 KB |
Output is correct |
37 |
Correct |
6 ms |
5580 KB |
Output is correct |
38 |
Correct |
6 ms |
5580 KB |
Output is correct |
39 |
Correct |
275 ms |
10772 KB |
Output is correct |
40 |
Correct |
240 ms |
10608 KB |
Output is correct |
41 |
Correct |
249 ms |
11096 KB |
Output is correct |
42 |
Correct |
224 ms |
11328 KB |
Output is correct |
43 |
Correct |
153 ms |
10876 KB |
Output is correct |
44 |
Correct |
94 ms |
10792 KB |
Output is correct |
45 |
Correct |
220 ms |
16660 KB |
Output is correct |
46 |
Correct |
173 ms |
23232 KB |
Output is correct |
47 |
Correct |
252 ms |
16580 KB |
Output is correct |
48 |
Correct |
558 ms |
40244 KB |
Output is correct |
49 |
Correct |
555 ms |
38340 KB |
Output is correct |
50 |
Correct |
273 ms |
16540 KB |
Output is correct |
51 |
Correct |
254 ms |
16700 KB |
Output is correct |
52 |
Correct |
326 ms |
16612 KB |
Output is correct |
53 |
Correct |
450 ms |
15540 KB |
Output is correct |
54 |
Correct |
338 ms |
15504 KB |
Output is correct |
55 |
Correct |
35 ms |
5836 KB |
Output is correct |
56 |
Correct |
18 ms |
5728 KB |
Output is correct |
57 |
Correct |
191 ms |
11504 KB |
Output is correct |
58 |
Correct |
59 ms |
11820 KB |
Output is correct |
59 |
Correct |
491 ms |
30776 KB |
Output is correct |
60 |
Correct |
1697 ms |
70440 KB |
Output is correct |
61 |
Correct |
428 ms |
17092 KB |
Output is correct |
62 |
Correct |
405 ms |
17768 KB |
Output is correct |
63 |
Correct |
541 ms |
17892 KB |
Output is correct |
64 |
Correct |
2875 ms |
36232 KB |
Output is correct |
65 |
Correct |
389 ms |
16436 KB |
Output is correct |
66 |
Correct |
2106 ms |
46976 KB |
Output is correct |
67 |
Correct |
154 ms |
18484 KB |
Output is correct |
68 |
Correct |
1149 ms |
36864 KB |
Output is correct |
69 |
Correct |
1142 ms |
37064 KB |
Output is correct |
70 |
Correct |
1148 ms |
35844 KB |
Output is correct |