Submission #230478

# Submission time Handle Problem Language Result Execution time Memory
230478 2020-05-10T08:00:43 Z alishahali1382 Pipes (BOI13_pipes) C++14
100 / 100
90 ms 9324 KB
#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 time Memory Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 6 ms 2688 KB Output is correct
3 Correct 6 ms 2816 KB Output is correct
4 Correct 85 ms 9048 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 6 ms 2816 KB Output is correct
10 Correct 6 ms 2816 KB Output is correct
11 Correct 6 ms 2816 KB Output is correct
12 Correct 6 ms 2816 KB Output is correct
13 Correct 67 ms 7800 KB Output is correct
14 Correct 71 ms 8824 KB Output is correct
15 Correct 87 ms 9048 KB Output is correct
16 Correct 69 ms 8184 KB Output is correct
17 Correct 74 ms 9048 KB Output is correct
18 Correct 80 ms 9176 KB Output is correct
19 Correct 79 ms 8440 KB Output is correct
20 Correct 6 ms 2688 KB Output is correct
21 Correct 6 ms 2816 KB Output is correct
22 Correct 82 ms 9176 KB Output is correct
23 Correct 62 ms 7800 KB Output is correct
24 Correct 87 ms 9172 KB Output is correct
25 Correct 70 ms 8056 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2688 KB Output is correct
2 Correct 6 ms 2816 KB Output is correct
3 Correct 64 ms 8568 KB Output is correct
4 Correct 6 ms 2688 KB Output is correct
5 Correct 6 ms 2688 KB Output is correct
6 Correct 7 ms 2816 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 6 ms 2816 KB Output is correct
16 Correct 6 ms 2816 KB Output is correct
17 Correct 6 ms 2816 KB Output is correct
18 Correct 6 ms 2688 KB Output is correct
19 Correct 6 ms 2688 KB Output is correct
20 Correct 6 ms 2688 KB Output is correct
21 Correct 6 ms 2688 KB Output is correct
22 Correct 6 ms 2816 KB Output is correct
23 Correct 63 ms 8016 KB Output is correct
24 Correct 79 ms 9264 KB Output is correct
25 Correct 64 ms 8568 KB Output is correct
26 Correct 6 ms 2688 KB Output is correct
27 Correct 6 ms 2688 KB Output is correct
28 Correct 6 ms 2688 KB Output is correct
29 Correct 6 ms 2688 KB Output is correct
30 Correct 74 ms 8776 KB Output is correct
31 Correct 86 ms 9324 KB Output is correct
32 Correct 84 ms 9080 KB Output is correct
33 Correct 85 ms 8788 KB Output is correct
34 Correct 6 ms 2688 KB Output is correct
35 Correct 6 ms 2688 KB Output is correct
36 Correct 6 ms 2688 KB Output is correct
37 Correct 6 ms 2688 KB Output is correct
38 Correct 90 ms 9076 KB Output is correct
39 Correct 83 ms 8952 KB Output is correct
40 Correct 78 ms 9160 KB Output is correct
41 Correct 72 ms 8948 KB Output is correct
42 Correct 6 ms 2688 KB Output is correct
43 Correct 6 ms 2688 KB Output is correct
44 Correct 6 ms 2688 KB Output is correct
45 Correct 6 ms 2688 KB Output is correct
46 Correct 76 ms 8820 KB Output is correct
47 Correct 79 ms 9292 KB Output is correct
48 Correct 74 ms 9248 KB Output is correct
49 Correct 59 ms 8312 KB Output is correct
50 Correct 6 ms 2688 KB Output is correct
51 Correct 6 ms 2688 KB Output is correct
52 Correct 6 ms 2688 KB Output is correct
53 Correct 6 ms 2688 KB Output is correct
54 Correct 81 ms 8912 KB Output is correct