# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
624866 | chonka | Pipes (BOI13_pipes) | C++98 | 87 ms | 18116 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |