Submission #1117404

# Submission time Handle Problem Language Result Execution time Memory
1117404 2024-11-23T13:56:47 Z batata1 Race (IOI11_race) C++17
21 / 100
127 ms 16540 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;
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 );
    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 ) if(minComK[e._k] == INF ) {
            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[]) {
    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;
      |         ^~~
# Verdict Execution time Memory 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 504 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 504 KB Output is correct
13 Correct 1 ms 344 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
# Verdict Execution time Memory 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 504 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 504 KB Output is correct
13 Correct 1 ms 344 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 2 ms 616 KB Output is correct
22 Correct 4 ms 4176 KB Output is correct
23 Correct 4 ms 3408 KB Output is correct
24 Correct 4 ms 3944 KB Output is correct
25 Correct 5 ms 3664 KB Output is correct
26 Correct 3 ms 1872 KB Output is correct
27 Correct 4 ms 3548 KB Output is correct
28 Correct 2 ms 1360 KB Output is correct
29 Correct 2 ms 1872 KB Output is correct
30 Correct 4 ms 1996 KB Output is correct
31 Correct 3 ms 3152 KB Output is correct
32 Correct 3 ms 3152 KB Output is correct
33 Correct 3 ms 3560 KB Output is correct
34 Correct 5 ms 2896 KB Output is correct
35 Correct 3 ms 3664 KB Output is correct
36 Correct 5 ms 4176 KB Output is correct
37 Correct 4 ms 3664 KB Output is correct
38 Correct 4 ms 2384 KB Output is correct
# Verdict Execution time Memory 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 504 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 504 KB Output is correct
13 Correct 1 ms 344 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 123 ms 16504 KB Output is correct
20 Correct 127 ms 16416 KB Output is correct
21 Incorrect 121 ms 16540 KB Output isn't correct
22 Halted 0 ms 0 KB -
# Verdict Execution time Memory 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 504 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 1 ms 504 KB Output is correct
13 Correct 1 ms 344 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 2 ms 616 KB Output is correct
22 Correct 4 ms 4176 KB Output is correct
23 Correct 4 ms 3408 KB Output is correct
24 Correct 4 ms 3944 KB Output is correct
25 Correct 5 ms 3664 KB Output is correct
26 Correct 3 ms 1872 KB Output is correct
27 Correct 4 ms 3548 KB Output is correct
28 Correct 2 ms 1360 KB Output is correct
29 Correct 2 ms 1872 KB Output is correct
30 Correct 4 ms 1996 KB Output is correct
31 Correct 3 ms 3152 KB Output is correct
32 Correct 3 ms 3152 KB Output is correct
33 Correct 3 ms 3560 KB Output is correct
34 Correct 5 ms 2896 KB Output is correct
35 Correct 3 ms 3664 KB Output is correct
36 Correct 5 ms 4176 KB Output is correct
37 Correct 4 ms 3664 KB Output is correct
38 Correct 4 ms 2384 KB Output is correct
39 Correct 123 ms 16504 KB Output is correct
40 Correct 127 ms 16416 KB Output is correct
41 Incorrect 121 ms 16540 KB Output isn't correct
42 Halted 0 ms 0 KB -