답안 #624866

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
624866 2022-08-08T22:49:06 Z chonka Pipes (BOI13_pipes) C++
100 / 100
87 ms 18116 KB
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
// mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

const int MAXN = 1e5 + 7 ;

int n ;
int a[ MAXN ] , cnt[ MAXN ] ;
int ans[ MAXN ] ;
vector < pair < int , int > > v[ MAXN ] ;
vector < pair < int , int > > gr[ MAXN ] ;
bool used[ MAXN ] , done[ MAXN ] ;

pair < int , int > get ( int x , int add , int coef ) {
    used[ x ] = true ;
    for ( auto [ y , id ] : gr[ x ] ) {
        if ( used[ y ] == true ) { continue ; }
        return get ( y , a[ y ] - add , -coef ) ;
    }
    return { add , coef } ;
}

void mrk ( int x , int val ) {
    used[ x ] = true ;
    for ( auto [ y , id ] : gr[ x ] ) {
        if ( used[ y ] == true ) { continue ; }
        ans[ id ] = 2 * val ;
        mrk ( y , a[ y ] - val ) ;
    }
}

void solve ( ) {
    int m ; 
    cin >> n >> m ;
    if ( m > n ) {
        cout << "0\n" ;
        return ;
    }
    for ( int i = 1 ; i <= n ; ++ i ) {
        cin >> a[ i ] ;
    }
    for ( int i = 1 , x , y ; i <= m ; ++ i ) {
        cin >> x >> y ;
        v[ x ].push_back ( { y , i } ) ;
        v[ y ].push_back ( { x , i } ) ;
    }
    queue < int > q ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        cnt[ i ] = ( int ) v[ i ].size ( ) ;
        if ( cnt[ i ] == 1 ) {
            cnt[ i ] = -1 ;
            q.push ( i ) ;
        }
    }
    while ( q.empty ( ) == false ) {
        int x = q.front ( ) ; q.pop ( ) ;
        done[ x ] = true ;
        for ( auto [ y , id ] : v[ x ] ) {
            if ( done[ y ] == true ) { continue ; }
            ans[ id ] += 2 * a[ x ] ; 
            a[ y ] -= ans[ id ] / 2 ;
            a[ x ] = 0 ;
            -- cnt[ y ] ;
            if ( cnt[ y ] == 1 ) {
                cnt[ y ] = -1 ;
                q.push ( y ) ;
            }
        }
    }
    for ( int i = 1 ; i <= n ; ++ i ) {
        if ( cnt[ i ] > 0 ) {
            for ( auto [ y , id ] : v[ i ] ) {
                if ( cnt[ y ] > 0 ) {
                    gr[ i ].push_back ( { y , id } ) ;
                }
            }
        }
    }
    int edge_val ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        if ( cnt[ i ] > 0 ) {
            auto ret = get ( i , 0 , 1 ) ;
            if ( ret.second == -1 ) {
                cout << "0\n" ;
                return ;
            }
            edge_val = ( a[ i ] - ret.first ) / 2 ;
            break ;
        }
    }
    for ( int i = 1 ; i <= n ; ++ i ) {
        used[ i ] = false ;
    }
    for ( int i = 1 ; i <= n ; ++ i ) {
        if ( cnt[ i ] > 0 ) {
            mrk ( i , edge_val ) ;
            ans[ gr[ i ][ 1 ].second ] = 2 * ( a[ i ] - edge_val ) ;
            break ;
        }
    }
    for ( int i = 1 ; i <= m ; ++ i ) {
        cout << ans[ i ] << "\n" ;
    }
}

int main ( ) {
    ios_base :: sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    int t = 1 ; // cin >> t ; 
    while ( t -- ) { solve ( ) ; }
    return 0 ;
}

Compilation message

pipes.cpp: In function 'std::pair<int, int> get(int, int, int)':
pipes.cpp:17:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   17 |     for ( auto [ y , id ] : gr[ x ] ) {
      |                ^
pipes.cpp: In function 'void mrk(int, int)':
pipes.cpp:26:16: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   26 |     for ( auto [ y , id ] : gr[ x ] ) {
      |                ^
pipes.cpp: In function 'void solve()':
pipes.cpp:59:20: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   59 |         for ( auto [ y , id ] : v[ x ] ) {
      |                    ^
pipes.cpp:73:24: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   73 |             for ( auto [ y , id ] : v[ i ] ) {
      |                        ^
pipes.cpp:97:17: warning: 'edge_val' may be used uninitialized in this function [-Wmaybe-uninitialized]
   97 |             mrk ( i , edge_val ) ;
      |             ~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 4 ms 4948 KB Output is correct
3 Correct 4 ms 5076 KB Output is correct
4 Correct 57 ms 10696 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 3 ms 4948 KB Output is correct
7 Correct 2 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 3 ms 5076 KB Output is correct
10 Correct 3 ms 5076 KB Output is correct
11 Correct 3 ms 5076 KB Output is correct
12 Correct 3 ms 5076 KB Output is correct
13 Correct 41 ms 9584 KB Output is correct
14 Correct 58 ms 10448 KB Output is correct
15 Correct 63 ms 10784 KB Output is correct
16 Correct 53 ms 9920 KB Output is correct
17 Correct 58 ms 10760 KB Output is correct
18 Correct 63 ms 10744 KB Output is correct
19 Correct 57 ms 10032 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 5076 KB Output is correct
22 Correct 59 ms 10716 KB Output is correct
23 Correct 45 ms 9512 KB Output is correct
24 Correct 61 ms 10836 KB Output is correct
25 Correct 43 ms 9724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 4948 KB Output is correct
2 Correct 3 ms 5076 KB Output is correct
3 Correct 55 ms 10920 KB Output is correct
4 Correct 2 ms 4948 KB Output is correct
5 Correct 2 ms 4948 KB Output is correct
6 Correct 3 ms 4916 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 2 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 3 ms 4948 KB Output is correct
14 Correct 2 ms 4948 KB Output is correct
15 Correct 3 ms 5076 KB Output is correct
16 Correct 3 ms 5076 KB Output is correct
17 Correct 3 ms 5076 KB Output is correct
18 Correct 2 ms 4948 KB Output is correct
19 Correct 3 ms 4948 KB Output is correct
20 Correct 3 ms 4948 KB Output is correct
21 Correct 3 ms 4948 KB Output is correct
22 Correct 3 ms 5076 KB Output is correct
23 Correct 67 ms 14032 KB Output is correct
24 Correct 70 ms 15168 KB Output is correct
25 Correct 48 ms 11012 KB Output is correct
26 Correct 2 ms 4948 KB Output is correct
27 Correct 3 ms 4948 KB Output is correct
28 Correct 3 ms 4948 KB Output is correct
29 Correct 2 ms 4948 KB Output is correct
30 Correct 86 ms 14072 KB Output is correct
31 Correct 81 ms 18116 KB Output is correct
32 Correct 59 ms 12028 KB Output is correct
33 Correct 59 ms 11596 KB Output is correct
34 Correct 3 ms 4948 KB Output is correct
35 Correct 3 ms 4948 KB Output is correct
36 Correct 4 ms 4948 KB Output is correct
37 Correct 3 ms 4948 KB Output is correct
38 Correct 71 ms 14424 KB Output is correct
39 Correct 54 ms 11404 KB Output is correct
40 Correct 65 ms 14812 KB Output is correct
41 Correct 59 ms 12232 KB Output is correct
42 Correct 2 ms 4948 KB Output is correct
43 Correct 3 ms 4948 KB Output is correct
44 Correct 2 ms 4948 KB Output is correct
45 Correct 2 ms 4948 KB Output is correct
46 Correct 87 ms 13776 KB Output is correct
47 Correct 64 ms 14828 KB Output is correct
48 Correct 74 ms 17932 KB Output is correct
49 Correct 53 ms 10152 KB Output is correct
50 Correct 3 ms 4948 KB Output is correct
51 Correct 3 ms 4948 KB Output is correct
52 Correct 3 ms 4948 KB Output is correct
53 Correct 3 ms 4948 KB Output is correct
54 Correct 60 ms 13452 KB Output is correct