#include<bits/stdc++.h>
#include "race.h"
using namespace std;
typedef long long ll;
#define pii pair<ll,int>
#define _caminho second
#define _k first
#define INF 1e9
vector<bool> previousCentroid;
void find_sub( int v , int paiV ,vector<int>& sub , vector<vector<int>>& A ) {
sub[v] = 1;
for( int viz : A[v] ) if( viz != paiV && !previousCentroid[viz] )
{ find_sub( viz , v , sub , A ); sub[v] += sub[viz]; }
}
int find_centroid( int v, int paiV , int n , vector<int>& sub , vector<vector<int>>& A) {
for( int viz : A[v] ) if( viz != paiV && !previousCentroid[viz] )
if( 2*sub[viz] > n ) return find_centroid( viz , v , n , sub , A );
return v;
}
void dfs( int v , int paiV , ll caminhoPai , int nDeAresta ,vector<pii>& kValidos ,vector<vector<ll>>& peso , vector<vector<int>>& A) {
//if (caminhoPai > k) return;
kValidos.push_back( make_pair( caminhoPai , nDeAresta) );
if( !A[v].empty() ) for( int i = 0 , viz = A[v][0] ; i < A[v].size() ; viz = A[v][++i] ) if( viz != paiV && !previousCentroid[viz] )
dfs( viz , v , caminhoPai + peso[v][i] , nDeAresta+1 , kValidos , peso , A );
}
ll divide_and_conquer( int v , vector<int>& sub, vector<int>& minComK , vector<vector<ll>>& peso , vector<vector<int>>& A) {
int k = minComK.size()-1;
find_sub( v , v , sub , A );
v = find_centroid( v , v , sub[v] , sub , A );
ll caminhoMin = INF;
vector<int> kValidos;
if( !A[v].empty() ) for( int i = 0 , viz = A[v][0] ; i < A[v].size() ; viz = A[v][++i] ) if( !previousCentroid[viz] ) {
vector<pii> kValidosParaSub;
dfs( viz , v , peso[v][i] , 1 , kValidosParaSub , peso , A );
for( pii& e : kValidosParaSub ) if( e._k <= k ) caminhoMin = min( caminhoMin , e._caminho + ( long long ) minComK[ k - e._k ] );
for( pii& e : kValidosParaSub ) if( e._k <= k ) {
kValidos.push_back(e._k);
minComK[e._k] = min( minComK[e._k] , e._caminho );
}
}
for( int e : kValidos ) minComK[e] = INF;
minComK[0] = 0;
previousCentroid[v] = true;
for( int viz : A[v] ) if(!previousCentroid[viz])
caminhoMin = min( caminhoMin , divide_and_conquer( viz , sub , minComK , peso , A ) );
return caminhoMin;
}
int best_path(int N, int K, int H[][2], int L[]) {
//k = K;
vector<int> minComK( K+1 , INF );
minComK[0] = 0;
previousCentroid.resize(N+1 , false );
vector<vector<int>> A(N+1);
vector<vector<ll>> peso(N+1);
for(int i = 0; i < N-1 ; i++ ) {
peso[++H[i][1]].push_back(L[i]);
peso[++H[i][0]].push_back(L[i]);
A[H[i][1]].push_back(H[i][0]);
A[H[i][0]].push_back(H[i][1]);
}
vector<int> sub( N+1 , 0 );
int resp = ( int ) divide_and_conquer( 1 , sub , minComK , peso , A );
previousCentroid.clear();
return ( resp == INF ) ? -1 : resp;
}
Compilation message
race.cpp: In function 'void dfs(int, int, ll, int, std::vector<std::pair<long long int, int> >&, std::vector<std::vector<long long int> >&, std::vector<std::vector<int> >&)':
race.cpp:26:60: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
26 | if( !A[v].empty() ) for( int i = 0 , viz = A[v][0] ; i < A[v].size() ; viz = A[v][++i] ) if( viz != paiV && !previousCentroid[viz] )
| ~~^~~~~~~~~~~~~
race.cpp: In function 'll divide_and_conquer(int, std::vector<int>&, std::vector<int>&, std::vector<std::vector<long long int> >&, std::vector<std::vector<int> >&)':
race.cpp:37:60: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | if( !A[v].empty() ) for( int i = 0 , viz = A[v][0] ; i < A[v].size() ; viz = A[v][++i] ) if( !previousCentroid[viz] ) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
592 KB |
Output is correct |
22 |
Correct |
5 ms |
4176 KB |
Output is correct |
23 |
Correct |
3 ms |
3408 KB |
Output is correct |
24 |
Correct |
4 ms |
3920 KB |
Output is correct |
25 |
Correct |
4 ms |
3664 KB |
Output is correct |
26 |
Correct |
2 ms |
1872 KB |
Output is correct |
27 |
Correct |
3 ms |
3676 KB |
Output is correct |
28 |
Correct |
2 ms |
1360 KB |
Output is correct |
29 |
Correct |
2 ms |
1616 KB |
Output is correct |
30 |
Correct |
3 ms |
1872 KB |
Output is correct |
31 |
Correct |
3 ms |
3152 KB |
Output is correct |
32 |
Correct |
3 ms |
3152 KB |
Output is correct |
33 |
Correct |
5 ms |
3408 KB |
Output is correct |
34 |
Correct |
3 ms |
2896 KB |
Output is correct |
35 |
Correct |
3 ms |
3664 KB |
Output is correct |
36 |
Correct |
4 ms |
4176 KB |
Output is correct |
37 |
Correct |
3 ms |
3664 KB |
Output is correct |
38 |
Correct |
3 ms |
2384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
127 ms |
14980 KB |
Output is correct |
20 |
Correct |
136 ms |
14912 KB |
Output is correct |
21 |
Correct |
145 ms |
15264 KB |
Output is correct |
22 |
Correct |
111 ms |
15144 KB |
Output is correct |
23 |
Correct |
125 ms |
14784 KB |
Output is correct |
24 |
Correct |
60 ms |
14628 KB |
Output is correct |
25 |
Correct |
156 ms |
21676 KB |
Output is correct |
26 |
Correct |
114 ms |
22700 KB |
Output is correct |
27 |
Correct |
192 ms |
27208 KB |
Output is correct |
28 |
Correct |
358 ms |
44640 KB |
Output is correct |
29 |
Correct |
351 ms |
43428 KB |
Output is correct |
30 |
Correct |
195 ms |
27252 KB |
Output is correct |
31 |
Correct |
166 ms |
27208 KB |
Output is correct |
32 |
Correct |
221 ms |
27208 KB |
Output is correct |
33 |
Correct |
249 ms |
28704 KB |
Output is correct |
34 |
Correct |
222 ms |
27588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
504 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
592 KB |
Output is correct |
22 |
Correct |
5 ms |
4176 KB |
Output is correct |
23 |
Correct |
3 ms |
3408 KB |
Output is correct |
24 |
Correct |
4 ms |
3920 KB |
Output is correct |
25 |
Correct |
4 ms |
3664 KB |
Output is correct |
26 |
Correct |
2 ms |
1872 KB |
Output is correct |
27 |
Correct |
3 ms |
3676 KB |
Output is correct |
28 |
Correct |
2 ms |
1360 KB |
Output is correct |
29 |
Correct |
2 ms |
1616 KB |
Output is correct |
30 |
Correct |
3 ms |
1872 KB |
Output is correct |
31 |
Correct |
3 ms |
3152 KB |
Output is correct |
32 |
Correct |
3 ms |
3152 KB |
Output is correct |
33 |
Correct |
5 ms |
3408 KB |
Output is correct |
34 |
Correct |
3 ms |
2896 KB |
Output is correct |
35 |
Correct |
3 ms |
3664 KB |
Output is correct |
36 |
Correct |
4 ms |
4176 KB |
Output is correct |
37 |
Correct |
3 ms |
3664 KB |
Output is correct |
38 |
Correct |
3 ms |
2384 KB |
Output is correct |
39 |
Correct |
127 ms |
14980 KB |
Output is correct |
40 |
Correct |
136 ms |
14912 KB |
Output is correct |
41 |
Correct |
145 ms |
15264 KB |
Output is correct |
42 |
Correct |
111 ms |
15144 KB |
Output is correct |
43 |
Correct |
125 ms |
14784 KB |
Output is correct |
44 |
Correct |
60 ms |
14628 KB |
Output is correct |
45 |
Correct |
156 ms |
21676 KB |
Output is correct |
46 |
Correct |
114 ms |
22700 KB |
Output is correct |
47 |
Correct |
192 ms |
27208 KB |
Output is correct |
48 |
Correct |
358 ms |
44640 KB |
Output is correct |
49 |
Correct |
351 ms |
43428 KB |
Output is correct |
50 |
Correct |
195 ms |
27252 KB |
Output is correct |
51 |
Correct |
166 ms |
27208 KB |
Output is correct |
52 |
Correct |
221 ms |
27208 KB |
Output is correct |
53 |
Correct |
249 ms |
28704 KB |
Output is correct |
54 |
Correct |
222 ms |
27588 KB |
Output is correct |
55 |
Correct |
12 ms |
1872 KB |
Output is correct |
56 |
Correct |
9 ms |
1872 KB |
Output is correct |
57 |
Correct |
80 ms |
15268 KB |
Output is correct |
58 |
Correct |
34 ms |
15044 KB |
Output is correct |
59 |
Correct |
91 ms |
23476 KB |
Output is correct |
60 |
Correct |
337 ms |
48460 KB |
Output is correct |
61 |
Correct |
171 ms |
27696 KB |
Output is correct |
62 |
Correct |
178 ms |
32176 KB |
Output is correct |
63 |
Correct |
229 ms |
32176 KB |
Output is correct |
64 |
Correct |
350 ms |
32252 KB |
Output is correct |
65 |
Correct |
288 ms |
29120 KB |
Output is correct |
66 |
Correct |
386 ms |
47012 KB |
Output is correct |
67 |
Correct |
122 ms |
33456 KB |
Output is correct |
68 |
Correct |
246 ms |
33468 KB |
Output is correct |
69 |
Correct |
239 ms |
33776 KB |
Output is correct |
70 |
Correct |
203 ms |
32472 KB |
Output is correct |