제출 #230478

#제출 시각아이디문제언어결과실행 시간메모리
230478alishahali1382Pipes (BOI13_pipes)C++14
100 / 100
90 ms9324 KiB
#include <bits/stdc++.h>
#pragma GCC optimize ("O2")
#pragma GCC optimize ("unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod=1000000007;
const int MAXN=100010, LOG=20;

int n, m, k, u, v, x, y, t, a, b;
ll A[MAXN], ans[MAXN];
int deg[MAXN];
bool mark[MAXN];
vector<pii> G[MAXN];
queue<int> Q;

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin>>n>>m;
	if (m>n) kill(0)
	for (int i=1; i<=n; i++) cin>>A[i], t+=(A[i]%2);
	if (t&1) kill(0)
	for (int i=1; i<=m; i++){
		cin>>u>>v;
		G[u].pb({v, i});
		G[v].pb({u, i});
		deg[u]++;
		deg[v]++;
	}
	for (int i=1; i<=n; i++) if (deg[i]==1) Q.push(i);
	while (Q.size()){
		int v=Q.front();
		Q.pop();
		if (mark[v]) continue ;
		mark[v]=1;
		for (pii p:G[v]) if (!mark[p.first]){
			ans[p.second]=A[v];
			A[v]-=ans[p.second];
			A[p.first]-=ans[p.second];
			deg[p.first]--;
			if (deg[p.first]==1) Q.push(p.first);
		}
	}
	//debug("shit")
	if (m==n-1){
		for (int v=1; v<=n; v++) if (A[v]) kill(0)
		for (int i=1; i<=m; i++) cout<<ans[i]*2<<'\n';
		return 0;
	}
	vector<int> V, E;
	for (int i=1; i<=n; i++) if (!mark[i]){
		V.pb(i);
		mark[i]=1;
		break ;
	}
	int flag=1;
	while (flag--){
		for (pii p:G[V.back()]) if (!mark[p.first]){
			V.pb(p.first);
			E.pb(p.second);
			mark[p.first]=1;
			flag=1;
			break ;
		}
	}
	for (pii p:G[V.back()]) if (p.first==V[0]) E.pb(p.second);
	//assert(V.size()==E.size());
	//debugv(V)
	//debugv(E)
	if (V.size()%2==0) kill(0)
	
	ll sum=0, k=V.size()/2;
	for (int i=0; i<=2*k; i++){
		if (i&1) sum+=A[V[i]];
		else sum-=A[V[i]];
	}
	//debug(sum)
	ans[E.back()]=-sum/2;
	A[V[0]]-=ans[E.back()];
	A[V.back()]-=ans[E.back()];
	
	//for (int i=0; i<=2*k; i++) cerr<<A[V[i]]<<" \n"[i==2*k];
	
	for (int i=0; i<2*k; i++){
		ans[E[i]]=A[V[i]];
		A[V[i]]-=ans[E[i]];
		A[V[i+1]]-=ans[E[i]];
	}
	for (int i=1; i<=m; i++) cout<<ans[i]*2<<'\n';
	
	return 0;
}
/*
5 5
-11
2
6
-4
3
2 5
1 2
1 3
2 3
3 4

5 5
10
-11
35
-31
15
1 2
2 3
3 4
4 5
5 1


*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...