#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);
int n , m , start ;
vpi v[100009];
ll a[100009] , deg[100009] , ans[100009] , done[100009];
ll sum ( int node ) {
done [ node ] = 1 ;
ll ret = 0 ;
for ( auto u : v[node] ) {
if ( done [u.fi] ) C ;
ret = sum ( u.fi ) ;
break ;
}
done [ node ] = 0 ;
return ret + a[node] ;
}
ll go ( int node , ll p ) {
done [ node ] = 1 ;
ll ret = 0 ;
for ( auto u : v[node] ) {
if ( done [u.fi] ) C ;
ret = go ( u.fi , a[ u.fi ] - p ) ;
break ;
}
done [ node ] = 0 ;
return ret + p ;
}
void solve ( int node ) {
done [node] = 1 ;
ll out = 0 ;
for ( auto u : v[node] ) {
if ( done [u.fi] ) C ;
a [ u.fi ] -= a[node] ;
ans [ u.se ] = a[node];
solve( u.fi ) ;
out = 1 ;
break;
}
if ( !out ) {
for ( auto u :v[node] ) {
if ( u.fi == start ) {
ans [ u.se ] = a[ node ];
break ;
}
}
}
done [ node ] = 0 ;
}
int main () {
cin >> n >> m ;
for ( int i = 0 ; i < n ; i ++ ) cin >> a[i] ;
for ( int i = 0 ; i < m ; i ++ ) {
int a , b ;
cin >> a >> b ;
a -- , b -- ;
v[a] .pb ( { b , i } ) ;
v[b] .pb ( { a , i } ) ;
}
if ( m > n ) cout << 0 << endl , exit ( 0 ) ;
queue <int> q ;
for ( int i = 0 ; i < n ; i ++ ) {
deg[i] = v[i].size() ;
if ( deg[i] == 1 ) q.push ( i ) ;
}
int N = n ;
while ( q.size() ) {
N -- ;
int node = q.front () ;
q.pop () ;
done [ node ] = 1 ;
for ( auto U : v [node] ) {
int u = U.fi ;
int id= U.se ;
if ( done [u] ) C ;
ans [id] = a [node] ;
a [u] -= ans [id] ;
deg[u] -- ;
if ( deg [u] == 1 ) q.push ( u ) ;
}
}
if ( N && N%2==0 ) cout << 0 << endl , exit ( 0 ) ;
for ( int i = 0 ; i < n ; i ++ ) {
if ( done [i] ) C ;
start = i ;
ll all = sum ( i ) / 2 ;
ll ret = go ( i , 0 ) ;
a [ start ] = all - ret ; ;
solve ( i ) ;
break ;
}
for ( int i = 0 ; i < m ; i ++ ) cout << ans [i] *2ll << endl ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
6 ms |
2688 KB |
Output is correct |
3 |
Correct |
9 ms |
2688 KB |
Output is correct |
4 |
Correct |
360 ms |
11736 KB |
Output is correct |
5 |
Correct |
6 ms |
2688 KB |
Output is correct |
6 |
Correct |
6 ms |
2688 KB |
Output is correct |
7 |
Correct |
6 ms |
2688 KB |
Output is correct |
8 |
Correct |
6 ms |
2688 KB |
Output is correct |
9 |
Correct |
9 ms |
2688 KB |
Output is correct |
10 |
Correct |
10 ms |
2816 KB |
Output is correct |
11 |
Correct |
9 ms |
2688 KB |
Output is correct |
12 |
Correct |
9 ms |
2816 KB |
Output is correct |
13 |
Correct |
272 ms |
9848 KB |
Output is correct |
14 |
Correct |
334 ms |
11384 KB |
Output is correct |
15 |
Correct |
362 ms |
11736 KB |
Output is correct |
16 |
Correct |
299 ms |
10616 KB |
Output is correct |
17 |
Correct |
353 ms |
11736 KB |
Output is correct |
18 |
Correct |
360 ms |
11864 KB |
Output is correct |
19 |
Correct |
345 ms |
11000 KB |
Output is correct |
20 |
Correct |
7 ms |
2688 KB |
Output is correct |
21 |
Correct |
9 ms |
2816 KB |
Output is correct |
22 |
Correct |
372 ms |
11992 KB |
Output is correct |
23 |
Correct |
268 ms |
9848 KB |
Output is correct |
24 |
Correct |
349 ms |
12092 KB |
Output is correct |
25 |
Correct |
294 ms |
10284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2688 KB |
Output is correct |
2 |
Correct |
9 ms |
2816 KB |
Output is correct |
3 |
Correct |
151 ms |
10628 KB |
Output is correct |
4 |
Correct |
139 ms |
8312 KB |
Output is correct |
5 |
Correct |
136 ms |
8440 KB |
Output is correct |
6 |
Correct |
558 ms |
23544 KB |
Output is correct |
7 |
Correct |
6 ms |
2688 KB |
Output is correct |
8 |
Correct |
6 ms |
2688 KB |
Output is correct |
9 |
Correct |
6 ms |
2688 KB |
Output is correct |
10 |
Correct |
6 ms |
2688 KB |
Output is correct |
11 |
Correct |
6 ms |
2688 KB |
Output is correct |
12 |
Correct |
6 ms |
2688 KB |
Output is correct |
13 |
Correct |
6 ms |
2688 KB |
Output is correct |
14 |
Correct |
6 ms |
2688 KB |
Output is correct |
15 |
Correct |
9 ms |
2816 KB |
Output is correct |
16 |
Correct |
9 ms |
2816 KB |
Output is correct |
17 |
Correct |
7 ms |
2816 KB |
Output is correct |
18 |
Correct |
8 ms |
2688 KB |
Output is correct |
19 |
Correct |
7 ms |
2688 KB |
Output is correct |
20 |
Correct |
7 ms |
2688 KB |
Output is correct |
21 |
Correct |
8 ms |
2816 KB |
Output is correct |
22 |
Correct |
9 ms |
2816 KB |
Output is correct |
23 |
Correct |
304 ms |
11532 KB |
Output is correct |
24 |
Correct |
362 ms |
12920 KB |
Output is correct |
25 |
Correct |
141 ms |
10488 KB |
Output is correct |
26 |
Correct |
137 ms |
8316 KB |
Output is correct |
27 |
Correct |
139 ms |
8184 KB |
Output is correct |
28 |
Correct |
146 ms |
8696 KB |
Output is correct |
29 |
Correct |
448 ms |
19704 KB |
Output is correct |
30 |
Correct |
354 ms |
12280 KB |
Output is correct |
31 |
Correct |
382 ms |
13944 KB |
Output is correct |
32 |
Correct |
339 ms |
12152 KB |
Output is correct |
33 |
Correct |
151 ms |
10872 KB |
Output is correct |
34 |
Correct |
138 ms |
8312 KB |
Output is correct |
35 |
Correct |
138 ms |
8312 KB |
Output is correct |
36 |
Correct |
141 ms |
8568 KB |
Output is correct |
37 |
Correct |
551 ms |
23588 KB |
Output is correct |
38 |
Correct |
367 ms |
12536 KB |
Output is correct |
39 |
Correct |
356 ms |
12024 KB |
Output is correct |
40 |
Correct |
360 ms |
12920 KB |
Output is correct |
41 |
Correct |
143 ms |
10616 KB |
Output is correct |
42 |
Correct |
142 ms |
8184 KB |
Output is correct |
43 |
Correct |
140 ms |
8184 KB |
Output is correct |
44 |
Correct |
142 ms |
8572 KB |
Output is correct |
45 |
Correct |
448 ms |
20576 KB |
Output is correct |
46 |
Correct |
367 ms |
12212 KB |
Output is correct |
47 |
Correct |
358 ms |
12920 KB |
Output is correct |
48 |
Correct |
373 ms |
13816 KB |
Output is correct |
49 |
Correct |
146 ms |
10748 KB |
Output is correct |
50 |
Correct |
139 ms |
8316 KB |
Output is correct |
51 |
Correct |
140 ms |
8440 KB |
Output is correct |
52 |
Correct |
141 ms |
8312 KB |
Output is correct |
53 |
Correct |
472 ms |
20728 KB |
Output is correct |
54 |
Correct |
360 ms |
12280 KB |
Output is correct |