#include <bits/stdc++.h>
#define MAX 200010
#define MAXK 1000010
#define INF 1000000010
using namespace std;
typedef long long int lli;
typedef pair<int,int> pii;
int n, k;
int ans;
int sub[MAX];
int minEdge[MAXK];
bool isCentroid[MAX];
vector< int > peso[MAX];
vector< int > grafo[MAX];
vector< pii > updates;
void DFSInit(int cur, int p)
{
sub[ cur ] = 1;
for(int i = 0 ; i < grafo[ cur ].size() ; i++)
{
int prox = grafo[ cur ][ i ];
if( prox == p ) continue;
if( isCentroid[ prox ] ) continue;
DFSInit( prox , cur );
sub[ cur ] += sub[ prox ];
}
}
int findCentroid(int cur, int p, int s)
{
for(int i = 0 ; i < grafo[ cur ].size() ; i++)
{
int prox = grafo[ cur ][ i ];
if( prox == p ) continue;
if( isCentroid[ prox ] ) continue;
if( sub[ prox ] > s/2 ) return findCentroid( prox , cur , s );
}
return cur;
}
void DFSCalculate(int cur, int p, lli distWeighted, int distUnweighted)
{
if(distWeighted <= k)
{
updates.push_back( { distWeighted , distUnweighted } );
ans = min( ans , minEdge[ k - distWeighted ] + distUnweighted );
}
for(int i = 0 ; i < grafo[ cur ].size() ; i++)
{
int prox = grafo[ cur ][ i ];
int w = peso[ cur ][ i ];
if( prox == p ) continue;
if( isCentroid[ prox ] ) continue;
DFSCalculate( prox , cur , distWeighted + w , distUnweighted + 1 );
}
}
void decompose(int cur, int p)
{
DFSInit( cur , cur );
int curCentroid = findCentroid( cur , cur , sub[ cur ] );
isCentroid[ curCentroid ] = true;
vector< int > ind;
for(int i = 0 ; i < grafo[ curCentroid ].size() ; i++)
{
int prox = grafo[ curCentroid ][ i ];
int w = peso[ curCentroid ][ i ];
if( isCentroid[ prox ] ) continue;
DFSCalculate(prox , curCentroid , w , 1);
while( !updates.empty() )
{
int wW = updates.back().first;
int wU = updates.back().second;
updates.pop_back();
ind.push_back( wW );
minEdge[ wW ] = min( minEdge[ wW ] , wU );
}
}
for(int i = 0 ; i < ind.size() ; i++)
minEdge[ ind[i] ] = INF;
minEdge[ 0 ] = 0;
for(int i = 0 ; i < grafo[ curCentroid ].size() ; i++)
{
int prox = grafo[ curCentroid ][ i ];
if( !isCentroid[ prox ] )
decompose( prox , curCentroid );
}
}
int best_path(int N, int K, int H[][2], int L[])
{
n = N; k = K;
for(int i = 0 ; i <= k ; i++)
minEdge[ i ] = INF;
minEdge[ 0 ] = 0;
for(int i = 0 ; i < n - 1 ; i++)
{
int U = H[ i ][ 0 ];
int V = H[ i ][ 1 ];
int W = L[ i ];
peso[ U ].push_back( W );
peso[ V ].push_back( W );
grafo[ U ].push_back( V );
grafo[ V ].push_back( U );
}
ans = INF;
decompose( 0 , 0 );
if(ans == INF) return -1;
return ans;
}
Compilation message
race.cpp: In function 'void DFSInit(int, int)':
race.cpp:28:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0 ; i < grafo[ cur ].size() ; i++)
~~^~~~~~~~~~~~~~~~~~~~~
race.cpp: In function 'int findCentroid(int, int, int)':
race.cpp:43:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0 ; i < grafo[ cur ].size() ; i++)
~~^~~~~~~~~~~~~~~~~~~~~
race.cpp: In function 'void DFSCalculate(int, int, lli, int)':
race.cpp:64:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0 ; i < grafo[ cur ].size() ; i++)
~~^~~~~~~~~~~~~~~~~~~~~
race.cpp: In function 'void decompose(int, int)':
race.cpp:86:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0 ; i < grafo[ curCentroid ].size() ; i++)
~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
race.cpp:107:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0 ; i < ind.size() ; i++)
~~^~~~~~~~~~~~
race.cpp:112:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0 ; i < grafo[ curCentroid ].size() ; i++)
~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9720 KB |
Output is correct |
2 |
Correct |
10 ms |
9720 KB |
Output is correct |
3 |
Correct |
10 ms |
9720 KB |
Output is correct |
4 |
Correct |
10 ms |
9720 KB |
Output is correct |
5 |
Correct |
10 ms |
9692 KB |
Output is correct |
6 |
Correct |
10 ms |
9720 KB |
Output is correct |
7 |
Correct |
10 ms |
9720 KB |
Output is correct |
8 |
Correct |
10 ms |
9720 KB |
Output is correct |
9 |
Correct |
10 ms |
9720 KB |
Output is correct |
10 |
Correct |
10 ms |
9724 KB |
Output is correct |
11 |
Correct |
10 ms |
9720 KB |
Output is correct |
12 |
Correct |
10 ms |
9880 KB |
Output is correct |
13 |
Correct |
11 ms |
9720 KB |
Output is correct |
14 |
Correct |
11 ms |
9852 KB |
Output is correct |
15 |
Correct |
10 ms |
9720 KB |
Output is correct |
16 |
Correct |
10 ms |
9720 KB |
Output is correct |
17 |
Correct |
10 ms |
9720 KB |
Output is correct |
18 |
Correct |
10 ms |
9720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9720 KB |
Output is correct |
2 |
Correct |
10 ms |
9720 KB |
Output is correct |
3 |
Correct |
10 ms |
9720 KB |
Output is correct |
4 |
Correct |
10 ms |
9720 KB |
Output is correct |
5 |
Correct |
10 ms |
9692 KB |
Output is correct |
6 |
Correct |
10 ms |
9720 KB |
Output is correct |
7 |
Correct |
10 ms |
9720 KB |
Output is correct |
8 |
Correct |
10 ms |
9720 KB |
Output is correct |
9 |
Correct |
10 ms |
9720 KB |
Output is correct |
10 |
Correct |
10 ms |
9724 KB |
Output is correct |
11 |
Correct |
10 ms |
9720 KB |
Output is correct |
12 |
Correct |
10 ms |
9880 KB |
Output is correct |
13 |
Correct |
11 ms |
9720 KB |
Output is correct |
14 |
Correct |
11 ms |
9852 KB |
Output is correct |
15 |
Correct |
10 ms |
9720 KB |
Output is correct |
16 |
Correct |
10 ms |
9720 KB |
Output is correct |
17 |
Correct |
10 ms |
9720 KB |
Output is correct |
18 |
Correct |
10 ms |
9720 KB |
Output is correct |
19 |
Correct |
10 ms |
9720 KB |
Output is correct |
20 |
Correct |
10 ms |
9720 KB |
Output is correct |
21 |
Correct |
11 ms |
9924 KB |
Output is correct |
22 |
Correct |
14 ms |
13432 KB |
Output is correct |
23 |
Correct |
14 ms |
12764 KB |
Output is correct |
24 |
Correct |
15 ms |
13176 KB |
Output is correct |
25 |
Correct |
14 ms |
13176 KB |
Output is correct |
26 |
Correct |
15 ms |
11132 KB |
Output is correct |
27 |
Correct |
14 ms |
13048 KB |
Output is correct |
28 |
Correct |
12 ms |
10616 KB |
Output is correct |
29 |
Correct |
12 ms |
11128 KB |
Output is correct |
30 |
Correct |
13 ms |
11256 KB |
Output is correct |
31 |
Correct |
13 ms |
12408 KB |
Output is correct |
32 |
Correct |
14 ms |
12664 KB |
Output is correct |
33 |
Correct |
16 ms |
12920 KB |
Output is correct |
34 |
Correct |
13 ms |
12152 KB |
Output is correct |
35 |
Correct |
14 ms |
13052 KB |
Output is correct |
36 |
Correct |
15 ms |
13560 KB |
Output is correct |
37 |
Correct |
14 ms |
12920 KB |
Output is correct |
38 |
Correct |
14 ms |
11896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9720 KB |
Output is correct |
2 |
Correct |
10 ms |
9720 KB |
Output is correct |
3 |
Correct |
10 ms |
9720 KB |
Output is correct |
4 |
Correct |
10 ms |
9720 KB |
Output is correct |
5 |
Correct |
10 ms |
9692 KB |
Output is correct |
6 |
Correct |
10 ms |
9720 KB |
Output is correct |
7 |
Correct |
10 ms |
9720 KB |
Output is correct |
8 |
Correct |
10 ms |
9720 KB |
Output is correct |
9 |
Correct |
10 ms |
9720 KB |
Output is correct |
10 |
Correct |
10 ms |
9724 KB |
Output is correct |
11 |
Correct |
10 ms |
9720 KB |
Output is correct |
12 |
Correct |
10 ms |
9880 KB |
Output is correct |
13 |
Correct |
11 ms |
9720 KB |
Output is correct |
14 |
Correct |
11 ms |
9852 KB |
Output is correct |
15 |
Correct |
10 ms |
9720 KB |
Output is correct |
16 |
Correct |
10 ms |
9720 KB |
Output is correct |
17 |
Correct |
10 ms |
9720 KB |
Output is correct |
18 |
Correct |
10 ms |
9720 KB |
Output is correct |
19 |
Correct |
199 ms |
19320 KB |
Output is correct |
20 |
Correct |
208 ms |
19320 KB |
Output is correct |
21 |
Correct |
213 ms |
19736 KB |
Output is correct |
22 |
Correct |
198 ms |
20388 KB |
Output is correct |
23 |
Correct |
171 ms |
19064 KB |
Output is correct |
24 |
Correct |
114 ms |
19576 KB |
Output is correct |
25 |
Correct |
236 ms |
23812 KB |
Output is correct |
26 |
Correct |
157 ms |
26540 KB |
Output is correct |
27 |
Correct |
336 ms |
29872 KB |
Output is correct |
28 |
Correct |
559 ms |
41176 KB |
Output is correct |
29 |
Correct |
583 ms |
40184 KB |
Output is correct |
30 |
Correct |
335 ms |
29816 KB |
Output is correct |
31 |
Correct |
352 ms |
29816 KB |
Output is correct |
32 |
Correct |
414 ms |
29944 KB |
Output is correct |
33 |
Correct |
498 ms |
28800 KB |
Output is correct |
34 |
Correct |
473 ms |
29688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
9720 KB |
Output is correct |
2 |
Correct |
10 ms |
9720 KB |
Output is correct |
3 |
Correct |
10 ms |
9720 KB |
Output is correct |
4 |
Correct |
10 ms |
9720 KB |
Output is correct |
5 |
Correct |
10 ms |
9692 KB |
Output is correct |
6 |
Correct |
10 ms |
9720 KB |
Output is correct |
7 |
Correct |
10 ms |
9720 KB |
Output is correct |
8 |
Correct |
10 ms |
9720 KB |
Output is correct |
9 |
Correct |
10 ms |
9720 KB |
Output is correct |
10 |
Correct |
10 ms |
9724 KB |
Output is correct |
11 |
Correct |
10 ms |
9720 KB |
Output is correct |
12 |
Correct |
10 ms |
9880 KB |
Output is correct |
13 |
Correct |
11 ms |
9720 KB |
Output is correct |
14 |
Correct |
11 ms |
9852 KB |
Output is correct |
15 |
Correct |
10 ms |
9720 KB |
Output is correct |
16 |
Correct |
10 ms |
9720 KB |
Output is correct |
17 |
Correct |
10 ms |
9720 KB |
Output is correct |
18 |
Correct |
10 ms |
9720 KB |
Output is correct |
19 |
Correct |
10 ms |
9720 KB |
Output is correct |
20 |
Correct |
10 ms |
9720 KB |
Output is correct |
21 |
Correct |
11 ms |
9924 KB |
Output is correct |
22 |
Correct |
14 ms |
13432 KB |
Output is correct |
23 |
Correct |
14 ms |
12764 KB |
Output is correct |
24 |
Correct |
15 ms |
13176 KB |
Output is correct |
25 |
Correct |
14 ms |
13176 KB |
Output is correct |
26 |
Correct |
15 ms |
11132 KB |
Output is correct |
27 |
Correct |
14 ms |
13048 KB |
Output is correct |
28 |
Correct |
12 ms |
10616 KB |
Output is correct |
29 |
Correct |
12 ms |
11128 KB |
Output is correct |
30 |
Correct |
13 ms |
11256 KB |
Output is correct |
31 |
Correct |
13 ms |
12408 KB |
Output is correct |
32 |
Correct |
14 ms |
12664 KB |
Output is correct |
33 |
Correct |
16 ms |
12920 KB |
Output is correct |
34 |
Correct |
13 ms |
12152 KB |
Output is correct |
35 |
Correct |
14 ms |
13052 KB |
Output is correct |
36 |
Correct |
15 ms |
13560 KB |
Output is correct |
37 |
Correct |
14 ms |
12920 KB |
Output is correct |
38 |
Correct |
14 ms |
11896 KB |
Output is correct |
39 |
Correct |
199 ms |
19320 KB |
Output is correct |
40 |
Correct |
208 ms |
19320 KB |
Output is correct |
41 |
Correct |
213 ms |
19736 KB |
Output is correct |
42 |
Correct |
198 ms |
20388 KB |
Output is correct |
43 |
Correct |
171 ms |
19064 KB |
Output is correct |
44 |
Correct |
114 ms |
19576 KB |
Output is correct |
45 |
Correct |
236 ms |
23812 KB |
Output is correct |
46 |
Correct |
157 ms |
26540 KB |
Output is correct |
47 |
Correct |
336 ms |
29872 KB |
Output is correct |
48 |
Correct |
559 ms |
41176 KB |
Output is correct |
49 |
Correct |
583 ms |
40184 KB |
Output is correct |
50 |
Correct |
335 ms |
29816 KB |
Output is correct |
51 |
Correct |
352 ms |
29816 KB |
Output is correct |
52 |
Correct |
414 ms |
29944 KB |
Output is correct |
53 |
Correct |
498 ms |
28800 KB |
Output is correct |
54 |
Correct |
473 ms |
29688 KB |
Output is correct |
55 |
Correct |
27 ms |
10824 KB |
Output is correct |
56 |
Correct |
26 ms |
10872 KB |
Output is correct |
57 |
Correct |
140 ms |
20140 KB |
Output is correct |
58 |
Correct |
64 ms |
20196 KB |
Output is correct |
59 |
Correct |
164 ms |
27312 KB |
Output is correct |
60 |
Correct |
856 ms |
46944 KB |
Output is correct |
61 |
Correct |
363 ms |
30332 KB |
Output is correct |
62 |
Correct |
360 ms |
34700 KB |
Output is correct |
63 |
Correct |
485 ms |
34684 KB |
Output is correct |
64 |
Correct |
655 ms |
33768 KB |
Output is correct |
65 |
Correct |
477 ms |
30000 KB |
Output is correct |
66 |
Correct |
700 ms |
42492 KB |
Output is correct |
67 |
Correct |
214 ms |
35808 KB |
Output is correct |
68 |
Correct |
416 ms |
35624 KB |
Output is correct |
69 |
Correct |
522 ms |
35724 KB |
Output is correct |
70 |
Correct |
358 ms |
34672 KB |
Output is correct |