This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#include <bits/stdc++.h>
using namespace std;
#define sqr 340
#define mid (l+r)/2
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define ins insert
#define era erase
#define C continue
#define mem(dp,i) memset(dp,i,sizeof(dp))
#define mset multiset
typedef long long ll;
typedef short int si;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pi> vpi;
typedef vector<pll> vpll;
const ll inf=1e18;
const ld pai=acos(-1);
ll n , m , all ;
set<pll>v[500009];
ll val[500009] , ans[500009] , ret[500009] , VAL[500009] ,done[500009];;
ll start , sum , xxx ;
void dfs ( int node , int p ) {
done [node] = 1;
for ( auto u : v[node] ) {
if ( u.fi == p ) C ;
ll XXX = xxx ;
if ( node != start ) XXX = -VAL[node];
VAL[node] += XXX;
VAL[u.fi] += XXX ;
sum += XXX ;
ret [ u.se ] = XXX ;
if ( done[u.fi] ) C ;
dfs ( u.fi , node ) ;
break;
}
}
int main () {
mem ( ret , -1 ) ;
cin >> n >> m ;
for ( int i = 0 ; i < n ; i ++ ) cin >> val[i] , all += val[i];
all /= 2 ;
for ( int i = 0 ; i < m ; i ++ ) {
int a , b ;
cin >> a >> b ;
a -- , b -- ;
v[a].ins( { b , i } ) ;
v[b].ins( { a , i } ) ;
}
if ( m > n ) {
cout << 0 << endl ;
exit ( 0 ) ;
}
set<pll>s;
for ( int i = 0 ; i < n ; i ++ ) {
s.ins ( { v[i].size() , i } ) ;
}
while ( ( s.size() && s.begin()->fi==1 ) ) {
int node = s.begin() -> se ;
int to = v[node].begin() -> fi ;
int id = v[node].begin() -> se ;
s.era(s.begin());
v[node].era( v[node].begin() );
s.era ( { v[to].size() , to } ) ;
v[to].era( { node , id } ) ;
if ( v[to].size() ) s.ins ( { v[to].size() , to } ) ;
ans [ id ] = - val [ node ] ;
val [ to ] += ans [id] ;
val [ node ] = 0 ;
}
if ( s.size() && s.size() % 2 == 0 ) cout << 0 << endl , exit ( 0 ) ;
if ( !s.size() ) {
for ( int i = 0 ; i < m ; i ++ ) {
cout << -ans [i] *2 << endl ;
}
exit ( 0 ) ;
}
for ( auto u : s ) VAL[u.se] = val[u.se] ;
start = s.begin()->se;
xxx = 0 ;
dfs(start,start);
for ( auto u : s ) VAL[u.se] = val[u.se] ;
xxx = all + sum ;
xxx *= -1 ;
mem(done,0);
dfs(start,start);
for ( int i = 0 ; i < m ;i ++ ) {
if ( ret[i]!=-1 ) cout << ret[i]*2<<endl;
else cout << -ans [i]*2 << endl;
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |