#include <bits/stdc++.h>
#include "race.h"
#define X first
#define Y second
#define pb push_back
#define pii pair<int, int>
typedef long long ll;
using namespace std;
const int MOD = 1e9 + 7;
const ll INF = 1e18;
const int OFF = (1 << 20);
int n, k, sol = 1e9;
vector< pair<int, int> > v[200005];
int kol[200005];
ll d[200005];
int bio[200005];
int w[1000005];
void dfs(int x, int p, ll dist) {
d[x] = dist;
kol[x]++;
for (int i = 0; i < v[x].size(); i++) {
if (v[x][i].X != p) {
dfs(v[x][i].X, x, dist+v[x][i].Y);
kol[x] += kol[v[x][i].X];
}
}
}
void postavi(int x, int p, ll l1, int l2) {
if (l1 <= k) w[l1] = min(w[l1], l2);
for (int i = 0; i < v[x].size(); i++) {
if (v[x][i].X != p && bio[v[x][i].X] == 0) postavi(v[x][i].X, x, l1+v[x][i].Y, l2+1);
}
}
void odstavi(int x, int p, ll l1, int l2) {
if (l1 <= k) w[l1] = 1e9;
for (int i = 0; i < v[x].size(); i++) {
if (v[x][i].X != p && bio[v[x][i].X] == 0) odstavi(v[x][i].X, x, l1+v[x][i].Y, l2+1);
}
}
void racunaj(int x, int p, ll l1, int l2) {
if (l1 <= k && w[k-l1] != 1e9) sol = min(sol, w[k-l1]+l2);
for (int i = 0; i < v[x].size(); i++) {
if (v[x][i].X != p && bio[v[x][i].X] == 0) racunaj(v[x][i].X, x, l1+v[x][i].Y, l2+1);
}
}
void centroidna(int x, int siz) {
while (1) {
int maxi = -1, gdje = -1;
for (int i = 0; i < v[x].size(); i++) {
if (bio[v[x][i].X] == 0 && kol[v[x][i].X] > maxi) {
maxi = kol[v[x][i].X];
gdje = v[x][i].X;
}
}
if (maxi > siz/2) {
int pri = kol[gdje];
kol[gdje] = kol[x];
kol[x] -= pri;
x = gdje;
}
else break;
}
bio[x] = 1;
w[0] = 0;
for (int i = 0; i < v[x].size(); i++) {
if (bio[v[x][i].X] == 0) {
racunaj(v[x][i].X, x, v[x][i].Y, 1);
postavi(v[x][i].X, x, v[x][i].Y, 1);
}
}
for (int i = 0; i < v[x].size(); i++) {
if (bio[v[x][i].X] == 0) odstavi(v[x][i].X, x, v[x][i].Y, 1);
}
w[0] = 1e9;
for (int i = 0; i < v[x].size(); i++) {
if (bio[v[x][i].X] == 0) centroidna(v[x][i].X, kol[v[x][i].X]);
}
}
int best_path(int N, int K, int h[][2], int l[]) {
n = N;
k = K;
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]});
}
for (int i = 0; i <= k; i++) w[i] = 1e9;
dfs(0, -1, 0);
centroidna(0, n);
if (sol == 1e9) return -1;
else return sol;
}
Compilation message
race.cpp: In function 'void dfs(int, int, ll)':
race.cpp:25:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for (int i = 0; i < v[x].size(); i++) {
| ~~^~~~~~~~~~~~~
race.cpp: In function 'void postavi(int, int, ll, int)':
race.cpp:36:20: 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 'void odstavi(int, int, ll, int)':
race.cpp:44:20: 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 < v[x].size(); i++) {
| ~~^~~~~~~~~~~~~
race.cpp: In function 'void racunaj(int, int, ll, int)':
race.cpp:52:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int i = 0; i < v[x].size(); i++) {
| ~~^~~~~~~~~~~~~
race.cpp: In function 'void centroidna(int, int)':
race.cpp:60:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for (int i = 0; i < v[x].size(); i++) {
| ~~^~~~~~~~~~~~~
race.cpp:78:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
78 | for (int i = 0; i < v[x].size(); i++) {
| ~~^~~~~~~~~~~~~
race.cpp:84:20: 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 < v[x].size(); i++) {
| ~~^~~~~~~~~~~~~
race.cpp:89:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
89 | for (int i = 0; i < v[x].size(); i++) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5004 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
4968 KB |
Output is correct |
7 |
Correct |
3 ms |
5000 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5004 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5004 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
4968 KB |
Output is correct |
7 |
Correct |
3 ms |
5000 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5004 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4996 KB |
Output is correct |
21 |
Correct |
4 ms |
5120 KB |
Output is correct |
22 |
Correct |
5 ms |
8660 KB |
Output is correct |
23 |
Correct |
7 ms |
7960 KB |
Output is correct |
24 |
Correct |
5 ms |
8468 KB |
Output is correct |
25 |
Correct |
5 ms |
8404 KB |
Output is correct |
26 |
Correct |
4 ms |
6424 KB |
Output is correct |
27 |
Correct |
5 ms |
8148 KB |
Output is correct |
28 |
Correct |
3 ms |
5844 KB |
Output is correct |
29 |
Correct |
4 ms |
6300 KB |
Output is correct |
30 |
Correct |
5 ms |
6484 KB |
Output is correct |
31 |
Correct |
5 ms |
7636 KB |
Output is correct |
32 |
Correct |
14 ms |
7892 KB |
Output is correct |
33 |
Correct |
5 ms |
8148 KB |
Output is correct |
34 |
Correct |
5 ms |
7432 KB |
Output is correct |
35 |
Correct |
7 ms |
8276 KB |
Output is correct |
36 |
Correct |
7 ms |
8660 KB |
Output is correct |
37 |
Correct |
5 ms |
8148 KB |
Output is correct |
38 |
Correct |
4 ms |
6988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5004 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
4968 KB |
Output is correct |
7 |
Correct |
3 ms |
5000 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5004 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
19 |
Correct |
164 ms |
12764 KB |
Output is correct |
20 |
Correct |
148 ms |
12768 KB |
Output is correct |
21 |
Correct |
133 ms |
12792 KB |
Output is correct |
22 |
Correct |
119 ms |
12880 KB |
Output is correct |
23 |
Correct |
100 ms |
13020 KB |
Output is correct |
24 |
Correct |
70 ms |
12896 KB |
Output is correct |
25 |
Correct |
168 ms |
16452 KB |
Output is correct |
26 |
Correct |
90 ms |
19972 KB |
Output is correct |
27 |
Correct |
191 ms |
20628 KB |
Output is correct |
28 |
Correct |
391 ms |
35412 KB |
Output is correct |
29 |
Correct |
574 ms |
34280 KB |
Output is correct |
30 |
Correct |
194 ms |
20832 KB |
Output is correct |
31 |
Correct |
190 ms |
20920 KB |
Output is correct |
32 |
Correct |
326 ms |
21044 KB |
Output is correct |
33 |
Correct |
279 ms |
19820 KB |
Output is correct |
34 |
Correct |
285 ms |
20684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
5004 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
4 ms |
4968 KB |
Output is correct |
7 |
Correct |
3 ms |
5000 KB |
Output is correct |
8 |
Correct |
3 ms |
4948 KB |
Output is correct |
9 |
Correct |
3 ms |
4948 KB |
Output is correct |
10 |
Correct |
4 ms |
4948 KB |
Output is correct |
11 |
Correct |
3 ms |
4948 KB |
Output is correct |
12 |
Correct |
3 ms |
4948 KB |
Output is correct |
13 |
Correct |
3 ms |
5004 KB |
Output is correct |
14 |
Correct |
3 ms |
4948 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
3 ms |
4948 KB |
Output is correct |
18 |
Correct |
4 ms |
4948 KB |
Output is correct |
19 |
Correct |
3 ms |
4948 KB |
Output is correct |
20 |
Correct |
3 ms |
4996 KB |
Output is correct |
21 |
Correct |
4 ms |
5120 KB |
Output is correct |
22 |
Correct |
5 ms |
8660 KB |
Output is correct |
23 |
Correct |
7 ms |
7960 KB |
Output is correct |
24 |
Correct |
5 ms |
8468 KB |
Output is correct |
25 |
Correct |
5 ms |
8404 KB |
Output is correct |
26 |
Correct |
4 ms |
6424 KB |
Output is correct |
27 |
Correct |
5 ms |
8148 KB |
Output is correct |
28 |
Correct |
3 ms |
5844 KB |
Output is correct |
29 |
Correct |
4 ms |
6300 KB |
Output is correct |
30 |
Correct |
5 ms |
6484 KB |
Output is correct |
31 |
Correct |
5 ms |
7636 KB |
Output is correct |
32 |
Correct |
14 ms |
7892 KB |
Output is correct |
33 |
Correct |
5 ms |
8148 KB |
Output is correct |
34 |
Correct |
5 ms |
7432 KB |
Output is correct |
35 |
Correct |
7 ms |
8276 KB |
Output is correct |
36 |
Correct |
7 ms |
8660 KB |
Output is correct |
37 |
Correct |
5 ms |
8148 KB |
Output is correct |
38 |
Correct |
4 ms |
6988 KB |
Output is correct |
39 |
Correct |
164 ms |
12764 KB |
Output is correct |
40 |
Correct |
148 ms |
12768 KB |
Output is correct |
41 |
Correct |
133 ms |
12792 KB |
Output is correct |
42 |
Correct |
119 ms |
12880 KB |
Output is correct |
43 |
Correct |
100 ms |
13020 KB |
Output is correct |
44 |
Correct |
70 ms |
12896 KB |
Output is correct |
45 |
Correct |
168 ms |
16452 KB |
Output is correct |
46 |
Correct |
90 ms |
19972 KB |
Output is correct |
47 |
Correct |
191 ms |
20628 KB |
Output is correct |
48 |
Correct |
391 ms |
35412 KB |
Output is correct |
49 |
Correct |
574 ms |
34280 KB |
Output is correct |
50 |
Correct |
194 ms |
20832 KB |
Output is correct |
51 |
Correct |
190 ms |
20920 KB |
Output is correct |
52 |
Correct |
326 ms |
21044 KB |
Output is correct |
53 |
Correct |
279 ms |
19820 KB |
Output is correct |
54 |
Correct |
285 ms |
20684 KB |
Output is correct |
55 |
Correct |
58 ms |
5748 KB |
Output is correct |
56 |
Correct |
11 ms |
5784 KB |
Output is correct |
57 |
Correct |
75 ms |
12968 KB |
Output is correct |
58 |
Correct |
33 ms |
12752 KB |
Output is correct |
59 |
Correct |
86 ms |
20708 KB |
Output is correct |
60 |
Correct |
455 ms |
38440 KB |
Output is correct |
61 |
Correct |
184 ms |
21096 KB |
Output is correct |
62 |
Correct |
183 ms |
24812 KB |
Output is correct |
63 |
Correct |
403 ms |
25036 KB |
Output is correct |
64 |
Correct |
448 ms |
23156 KB |
Output is correct |
65 |
Correct |
365 ms |
21828 KB |
Output is correct |
66 |
Correct |
437 ms |
35864 KB |
Output is correct |
67 |
Correct |
116 ms |
25584 KB |
Output is correct |
68 |
Correct |
266 ms |
25612 KB |
Output is correct |
69 |
Correct |
261 ms |
25804 KB |
Output is correct |
70 |
Correct |
247 ms |
24948 KB |
Output is correct |