답안 #1117420

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1117420 2024-11-23T14:29:49 Z batata1 경주 (Race) (IOI11_race) C++17
100 / 100
382 ms 51364 KB
#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
int t, k;
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) {
    //printf(" dfs %d , p%d , camp %lld , na%d \n" , v , paiV , caminhoPai , nDeAresta );
    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) {
    ++t;
    int aux = v;
    int k = minComK.size()-1;
    find_sub( v , v , sub , A );
    v = find_centroid( v , v , sub[v] , sub , A );
    ll caminhoMin = INF;
    // for( int i = 1 ; i < A.size() ; i++) {
    //    printf("%d :: " , i);
    //     for( int viz : A[i] ) printf(" %d" , viz );
    //     printf("\n");
    // }


    //printf(" divide and conquer : %d.start %d.centroid\n" ,aux ,  v );
    //assert( t < 30 );
    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 );
         //printf("   validos para sub :%d\n" , viz);
         //for( pii e : kValidosParaSub ) printf("%lld %d\n" , e._k , e._caminho);

        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]);
    }

    // for( int i = 1 ; i < A.size() ; i++) {
    //    printf("%d :: " , i);
    //     for( int viz : A[i] ) printf(" %d" , viz );
    //     printf("\n");
    // }

    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:28:60: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |     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:49:60: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |     if( !A[v].empty() ) for( int i = 0 , viz = A[v][0] ; i < A[v].size() ; viz = A[v][++i] ) if( !previousCentroid[viz] ) {
      |                                                          ~~^~~~~~~~~~~~~
race.cpp:34:9: warning: unused variable 'aux' [-Wunused-variable]
   34 |     int aux = v;
      |         ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 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 400 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 504 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 336 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 400 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 504 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 2 ms 592 KB Output is correct
22 Correct 4 ms 4176 KB Output is correct
23 Correct 3 ms 3408 KB Output is correct
24 Correct 3 ms 3920 KB Output is correct
25 Correct 3 ms 3664 KB Output is correct
26 Correct 2 ms 1872 KB Output is correct
27 Correct 5 ms 3664 KB Output is correct
28 Correct 2 ms 1536 KB Output is correct
29 Correct 2 ms 1872 KB Output is correct
30 Correct 2 ms 1872 KB Output is correct
31 Correct 4 ms 3152 KB Output is correct
32 Correct 4 ms 3196 KB Output is correct
33 Correct 3 ms 3592 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 4172 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 336 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 400 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 504 KB Output is correct
17 Correct 1 ms 336 KB Output is correct
18 Correct 1 ms 336 KB Output is correct
19 Correct 106 ms 14808 KB Output is correct
20 Correct 102 ms 13844 KB Output is correct
21 Correct 112 ms 15820 KB Output is correct
22 Correct 109 ms 16360 KB Output is correct
23 Correct 71 ms 15176 KB Output is correct
24 Correct 58 ms 15188 KB Output is correct
25 Correct 122 ms 18512 KB Output is correct
26 Correct 94 ms 23984 KB Output is correct
27 Correct 167 ms 30280 KB Output is correct
28 Correct 266 ms 44080 KB Output is correct
29 Correct 236 ms 43056 KB Output is correct
30 Correct 158 ms 30312 KB Output is correct
31 Correct 171 ms 30316 KB Output is correct
32 Correct 177 ms 30188 KB Output is correct
33 Correct 198 ms 28432 KB Output is correct
34 Correct 155 ms 29124 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 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 400 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 504 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 2 ms 592 KB Output is correct
22 Correct 4 ms 4176 KB Output is correct
23 Correct 3 ms 3408 KB Output is correct
24 Correct 3 ms 3920 KB Output is correct
25 Correct 3 ms 3664 KB Output is correct
26 Correct 2 ms 1872 KB Output is correct
27 Correct 5 ms 3664 KB Output is correct
28 Correct 2 ms 1536 KB Output is correct
29 Correct 2 ms 1872 KB Output is correct
30 Correct 2 ms 1872 KB Output is correct
31 Correct 4 ms 3152 KB Output is correct
32 Correct 4 ms 3196 KB Output is correct
33 Correct 3 ms 3592 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 4172 KB Output is correct
37 Correct 3 ms 3664 KB Output is correct
38 Correct 3 ms 2384 KB Output is correct
39 Correct 106 ms 14808 KB Output is correct
40 Correct 102 ms 13844 KB Output is correct
41 Correct 112 ms 15820 KB Output is correct
42 Correct 109 ms 16360 KB Output is correct
43 Correct 71 ms 15176 KB Output is correct
44 Correct 58 ms 15188 KB Output is correct
45 Correct 122 ms 18512 KB Output is correct
46 Correct 94 ms 23984 KB Output is correct
47 Correct 167 ms 30280 KB Output is correct
48 Correct 266 ms 44080 KB Output is correct
49 Correct 236 ms 43056 KB Output is correct
50 Correct 158 ms 30312 KB Output is correct
51 Correct 171 ms 30316 KB Output is correct
52 Correct 177 ms 30188 KB Output is correct
53 Correct 198 ms 28432 KB Output is correct
54 Correct 155 ms 29124 KB Output is correct
55 Correct 9 ms 1856 KB Output is correct
56 Correct 9 ms 2048 KB Output is correct
57 Correct 74 ms 16728 KB Output is correct
58 Correct 36 ms 16068 KB Output is correct
59 Correct 102 ms 24912 KB Output is correct
60 Correct 346 ms 51364 KB Output is correct
61 Correct 173 ms 30792 KB Output is correct
62 Correct 176 ms 35248 KB Output is correct
63 Correct 213 ms 35336 KB Output is correct
64 Correct 382 ms 35332 KB Output is correct
65 Correct 211 ms 30808 KB Output is correct
66 Correct 314 ms 44636 KB Output is correct
67 Correct 113 ms 36792 KB Output is correct
68 Correct 213 ms 35944 KB Output is correct
69 Correct 195 ms 36224 KB Output is correct
70 Correct 199 ms 34760 KB Output is correct