#include<cstdio>
#include<algorithm>
#include<vector>
#include<map>
#define S 200007
using namespace std;
vector < pair < int , int > > V[S];
int roz[S];
bool deleted[S];
void DFS(int x, int r){
roz[x] = 1;
for(int i = 0 ; i < V[x].size();i++){
if(V[x][i].first != r && !deleted[V[x][i].first]){
DFS(V[x][i].first,x);
roz[x] += roz[V[x][i].first];
}
}
}
int Find(int x){
for(int i = 0 ; i < V[x].size();i++){
if(!deleted[V[x][i].first]){
if(roz[V[x][i].first] > roz[x]/2){
roz[x] -= roz[V[x][i].first];
roz[V[x][i].first] += roz[x];
return Find(V[x][i].first);
}
}
}
return x;
}
long long dp[S];
int l = 0;
int L[S];
int R[S];
int ID[S];
int poz[S];
void DFS2(int x, int r){
L[x] = ++l;
ID[l] = x;
for(int i = 0 ; i < V[x].size();i++){
int v = V[x][i].first;
int c = V[x][i].second;
if(v != r && !deleted[v]){
poz[v] = poz[x] + 1;
dp[v] = dp[x] + c;
DFS2(v,x);
}
}
R[x] = l;
}
long long k;
int odp = 1e9;
map < long long , int > mapa;
void decomposition(int x){
DFS(x,x);
int centroid = Find(x);
poz[centroid] = 0;
dp[centroid] = 0;
DFS2(centroid,centroid);
deleted[centroid] = 1;
for(int i = 0 ; i < V[centroid].size();i++){
int v = V[centroid][i].first;
if(!deleted[v]){
//printf("%d %d %d %d*\n",centroid,v,L[v],R[v]);
for(int j = L[v]; j <= R[v];j++){
int v2 = ID[j];
if(dp[v2] == k){
odp = min(odp,poz[v2]);
//printf("%d %d$\n",centroid,v2);
}
if(mapa[k-dp[v2]]){
odp = min(odp, poz[v2] + mapa[k-dp[v2]]);
//printf("%d %d$$\n",centroid,v2);
}
}
for(int j = L[v]; j <= R[v];j++){
int v2 = ID[j];
if(!mapa[dp[v2]]){
mapa[dp[v2]] = poz[v2];
}else{
mapa[dp[v2]] = min(mapa[dp[v2]],poz[v2]);
}
}
}
}
l = 0;
mapa.clear();
for(int i = 0 ; i < V[centroid].size();i++){
int v = V[centroid][i].first;
if(!deleted[v])
decomposition(v);
}
}
int best_path(int n, int _k, int H[][2], int L[]){
k = _k;
for(int i = 0 ; i < n-1;i++){
V[H[i][0]+1].push_back({H[i][1]+1,L[i]});
V[H[i][1]+1].push_back({H[i][0]+1,L[i]});
}
decomposition(1);
if(odp < 1e8)
return odp;
return -1;
}
/*
int main(void){
int A[10][2] = { {0,1}, {0,2}, {2,3}, {3,4}, {4,5}, {0,6}, {6,7}, {6,8}, {8,9}, {8,10} };
int B[10] = {3,4,5,4,6,3,2,5,6,7};
printf("%d",best_path(11,12,A,B));
return 0;
}
*/
Compilation message
race.cpp: In function 'void DFS(int, int)':
race.cpp:15: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]
15 | for(int i = 0 ; i < V[x].size();i++){
| ~~^~~~~~~~~~~~~
race.cpp: In function 'int Find(int)':
race.cpp:24: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]
24 | for(int i = 0 ; i < V[x].size();i++){
| ~~^~~~~~~~~~~~~
race.cpp: In function 'void DFS2(int, int)':
race.cpp:46: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]
46 | for(int i = 0 ; i < V[x].size();i++){
| ~~^~~~~~~~~~~~~
race.cpp: In function 'void decomposition(int)':
race.cpp:68: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]
68 | for(int i = 0 ; i < V[centroid].size();i++){
| ~~^~~~~~~~~~~~~~~~~~~~
race.cpp:95: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]
95 | for(int i = 0 ; i < V[centroid].size();i++){
| ~~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4988 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4952 KB |
Output is correct |
11 |
Correct |
3 ms |
4964 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
5056 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4988 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4988 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4952 KB |
Output is correct |
11 |
Correct |
3 ms |
4964 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
5056 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4988 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4988 KB |
Output is correct |
21 |
Correct |
5 ms |
5040 KB |
Output is correct |
22 |
Correct |
6 ms |
5196 KB |
Output is correct |
23 |
Correct |
6 ms |
5196 KB |
Output is correct |
24 |
Correct |
5 ms |
5196 KB |
Output is correct |
25 |
Correct |
5 ms |
5196 KB |
Output is correct |
26 |
Correct |
5 ms |
5196 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
6 ms |
5196 KB |
Output is correct |
29 |
Correct |
5 ms |
5196 KB |
Output is correct |
30 |
Correct |
6 ms |
5136 KB |
Output is correct |
31 |
Correct |
5 ms |
5132 KB |
Output is correct |
32 |
Correct |
6 ms |
5196 KB |
Output is correct |
33 |
Correct |
6 ms |
5196 KB |
Output is correct |
34 |
Correct |
6 ms |
5196 KB |
Output is correct |
35 |
Correct |
6 ms |
5232 KB |
Output is correct |
36 |
Correct |
5 ms |
5196 KB |
Output is correct |
37 |
Correct |
5 ms |
5196 KB |
Output is correct |
38 |
Correct |
6 ms |
5196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4988 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4952 KB |
Output is correct |
11 |
Correct |
3 ms |
4964 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
5056 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4988 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
409 ms |
14000 KB |
Output is correct |
20 |
Correct |
380 ms |
14128 KB |
Output is correct |
21 |
Correct |
358 ms |
14052 KB |
Output is correct |
22 |
Correct |
308 ms |
14180 KB |
Output is correct |
23 |
Correct |
503 ms |
14472 KB |
Output is correct |
24 |
Correct |
227 ms |
14300 KB |
Output is correct |
25 |
Correct |
336 ms |
17456 KB |
Output is correct |
26 |
Correct |
148 ms |
19676 KB |
Output is correct |
27 |
Correct |
446 ms |
23816 KB |
Output is correct |
28 |
Correct |
1739 ms |
59616 KB |
Output is correct |
29 |
Correct |
1695 ms |
58712 KB |
Output is correct |
30 |
Correct |
441 ms |
23872 KB |
Output is correct |
31 |
Correct |
439 ms |
23796 KB |
Output is correct |
32 |
Correct |
573 ms |
23876 KB |
Output is correct |
33 |
Correct |
865 ms |
22556 KB |
Output is correct |
34 |
Correct |
1777 ms |
48000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
4 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4940 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4988 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4952 KB |
Output is correct |
11 |
Correct |
3 ms |
4964 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
5056 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4988 KB |
Output is correct |
16 |
Correct |
4 ms |
4940 KB |
Output is correct |
17 |
Correct |
3 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
3 ms |
4988 KB |
Output is correct |
21 |
Correct |
5 ms |
5040 KB |
Output is correct |
22 |
Correct |
6 ms |
5196 KB |
Output is correct |
23 |
Correct |
6 ms |
5196 KB |
Output is correct |
24 |
Correct |
5 ms |
5196 KB |
Output is correct |
25 |
Correct |
5 ms |
5196 KB |
Output is correct |
26 |
Correct |
5 ms |
5196 KB |
Output is correct |
27 |
Correct |
4 ms |
5068 KB |
Output is correct |
28 |
Correct |
6 ms |
5196 KB |
Output is correct |
29 |
Correct |
5 ms |
5196 KB |
Output is correct |
30 |
Correct |
6 ms |
5136 KB |
Output is correct |
31 |
Correct |
5 ms |
5132 KB |
Output is correct |
32 |
Correct |
6 ms |
5196 KB |
Output is correct |
33 |
Correct |
6 ms |
5196 KB |
Output is correct |
34 |
Correct |
6 ms |
5196 KB |
Output is correct |
35 |
Correct |
6 ms |
5232 KB |
Output is correct |
36 |
Correct |
5 ms |
5196 KB |
Output is correct |
37 |
Correct |
5 ms |
5196 KB |
Output is correct |
38 |
Correct |
6 ms |
5196 KB |
Output is correct |
39 |
Correct |
409 ms |
14000 KB |
Output is correct |
40 |
Correct |
380 ms |
14128 KB |
Output is correct |
41 |
Correct |
358 ms |
14052 KB |
Output is correct |
42 |
Correct |
308 ms |
14180 KB |
Output is correct |
43 |
Correct |
503 ms |
14472 KB |
Output is correct |
44 |
Correct |
227 ms |
14300 KB |
Output is correct |
45 |
Correct |
336 ms |
17456 KB |
Output is correct |
46 |
Correct |
148 ms |
19676 KB |
Output is correct |
47 |
Correct |
446 ms |
23816 KB |
Output is correct |
48 |
Correct |
1739 ms |
59616 KB |
Output is correct |
49 |
Correct |
1695 ms |
58712 KB |
Output is correct |
50 |
Correct |
441 ms |
23872 KB |
Output is correct |
51 |
Correct |
439 ms |
23796 KB |
Output is correct |
52 |
Correct |
573 ms |
23876 KB |
Output is correct |
53 |
Correct |
865 ms |
22556 KB |
Output is correct |
54 |
Correct |
1777 ms |
48000 KB |
Output is correct |
55 |
Correct |
36 ms |
6472 KB |
Output is correct |
56 |
Correct |
22 ms |
5912 KB |
Output is correct |
57 |
Correct |
214 ms |
14256 KB |
Output is correct |
58 |
Correct |
57 ms |
13988 KB |
Output is correct |
59 |
Correct |
501 ms |
28764 KB |
Output is correct |
60 |
Correct |
1743 ms |
55848 KB |
Output is correct |
61 |
Correct |
519 ms |
27204 KB |
Output is correct |
62 |
Correct |
455 ms |
23932 KB |
Output is correct |
63 |
Correct |
593 ms |
23876 KB |
Output is correct |
64 |
Correct |
1890 ms |
36096 KB |
Output is correct |
65 |
Correct |
1723 ms |
49060 KB |
Output is correct |
66 |
Correct |
1765 ms |
56900 KB |
Output is correct |
67 |
Correct |
203 ms |
24368 KB |
Output is correct |
68 |
Correct |
905 ms |
45432 KB |
Output is correct |
69 |
Correct |
916 ms |
45796 KB |
Output is correct |
70 |
Correct |
854 ms |
43616 KB |
Output is correct |