답안 #853288

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
853288 2023-09-23T23:15:13 Z NK_ Pipes (BOI13_pipes) C++17
100 / 100
58 ms 16844 KB
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
 
using namespace std;
 
#define nl '\n'
#define pb push_back
#define pf push_front
 
#define mp make_pair
#define f first
#define s second
#define sz(x) int(x.size())
 
template<class T> using V = vector<T>;
using pi = pair<int, int>;
using vi = V<int>;
using vpi = V<pi>;
 
using ll = long long;
using pl = pair<ll, ll>;
using vpl = V<pl>;
using vl = V<ll>;
 
using db = long double;
 
template<class T> using pq = priority_queue<T, V<T>, greater<T>>;
 
const int MOD = 1e9 + 7;
const ll INFL = ll(1e17);
const int LG = 18;

int main() {
	cin.tie(0)->sync_with_stdio(0);

	int N, M; cin >> N >> M;

	if (M > N) {
		cout << 0 << nl;
		exit(0-0);
	}

	// TREE OR 1 CYCLE

	vl A(N); for(auto& x : A) cin >> x;

	V<vpi> adj(N); for(int i = 0; i < M; i++) {
		int u, v; cin >> u >> v; --u, --v;
		adj[u].pb(mp(v, i));
		adj[v].pb(mp(u, i));
	}

	vl ans(M); vi vis(N);
	function<int(int)> fix = [&](int u) { // remove odd
		vis[u] = 1;
		for(auto v : adj[u]) if (!vis[v.f]) {
			ans[v.s] = fix(v.f);
			A[u] -= ans[v.s];
		}

		int need = (A[u] % 2 == 0 ? 0 : 1); A[u] -= need;
		return need;
	};

	if (fix(0)) {
		cout << 0 << nl;
		exit(0-0);
	}	



	vi deg(N); vi seen(N);
	for(int u = 0; u < N; u++) for(auto v : adj[u]) {
		if (u > v.f) deg[u]++, deg[v.f]++;
	}

	queue<int> q; 
	for(int u = 0; u < N; u++) if (deg[u] == 1) q.push(u);

	while(sz(q)) {
		int u = q.front(); q.pop();

		seen[u] = 1;
		for(auto& v : adj[u]) {
			if (!seen[v.f]) {
				deg[v.f]--; 
				ans[v.s] += A[u]; A[v.f] -= A[u]; A[u] = 0;
				if (deg[v.f] == 1) q.push(v.f);
			}
		}
	}

	// for(int i = 0; i < M; i++) cout << ans[i] << nl;
	// cout << nl;
	// for(int i = 0; i < N; i++) cout << A[i] << nl;
	// cout << nl;


	int len = count(begin(seen), end(seen), 0);

	if (len == 0) {
		for(int i = 0; i < M; i++) cout << ans[i] * 2LL << nl;
		exit(0-0);
	}

	if (len % 2 == 0) {
		cout << 0 << nl;
		exit(0-0);
	}	

	
	vi P;
	function<void(int)> dfs = [&](int u) {
		if (sz(P) == 2*len) return;
		P.pb(u);
		for(auto& v : adj[u]) {
			if (seen[v.f]) continue;
			if (sz(P) > 1 && v.s == P[sz(P)-2]) continue;
			P.pb(v.s); 
			dfs(v.f);
			if (sz(P) == 2*len) return;
		}
	};

	int r = find(begin(seen), end(seen), 0) - begin(seen);
	dfs(r);

	int amt = 0, coef = 1;
	for(int i = 0; i < 2 * len; i += 2) {
		amt += coef * A[P[i]]; coef = -coef;
	}

	amt /= 2;

	for(int i = 0; i < 2 * len; i += 2) {
		ans[P[i+1]] += A[P[i]] - amt;
		amt = A[P[i]] - amt ;
	}

	for(int i = 0; i < M; i++) cout << ans[i] * 2LL << nl;
	exit(0-0);
} 	

# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 49 ms 9748 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 0 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 34 ms 7760 KB Output is correct
14 Correct 42 ms 9180 KB Output is correct
15 Correct 44 ms 9808 KB Output is correct
16 Correct 39 ms 8272 KB Output is correct
17 Correct 58 ms 9840 KB Output is correct
18 Correct 46 ms 9808 KB Output is correct
19 Correct 47 ms 12880 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 50 ms 9808 KB Output is correct
23 Correct 34 ms 7760 KB Output is correct
24 Correct 47 ms 9868 KB Output is correct
25 Correct 37 ms 8276 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 544 KB Output is correct
2 Correct 1 ms 600 KB Output is correct
3 Correct 38 ms 12112 KB Output is correct
4 Correct 0 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 0 ms 344 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 344 KB Output is correct
10 Correct 0 ms 344 KB Output is correct
11 Correct 0 ms 344 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 600 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 0 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 600 KB Output is correct
23 Correct 38 ms 12268 KB Output is correct
24 Correct 49 ms 14060 KB Output is correct
25 Correct 41 ms 12136 KB Output is correct
26 Correct 0 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 548 KB Output is correct
29 Correct 0 ms 344 KB Output is correct
30 Correct 58 ms 16192 KB Output is correct
31 Correct 56 ms 16812 KB Output is correct
32 Correct 50 ms 11212 KB Output is correct
33 Correct 45 ms 13136 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
36 Correct 0 ms 344 KB Output is correct
37 Correct 0 ms 344 KB Output is correct
38 Correct 47 ms 15512 KB Output is correct
39 Correct 53 ms 10524 KB Output is correct
40 Correct 46 ms 13704 KB Output is correct
41 Correct 46 ms 15440 KB Output is correct
42 Correct 0 ms 344 KB Output is correct
43 Correct 1 ms 344 KB Output is correct
44 Correct 0 ms 344 KB Output is correct
45 Correct 0 ms 344 KB Output is correct
46 Correct 51 ms 16844 KB Output is correct
47 Correct 50 ms 13692 KB Output is correct
48 Correct 55 ms 16444 KB Output is correct
49 Correct 43 ms 9812 KB Output is correct
50 Correct 0 ms 344 KB Output is correct
51 Correct 1 ms 344 KB Output is correct
52 Correct 0 ms 344 KB Output is correct
53 Correct 0 ms 344 KB Output is correct
54 Correct 51 ms 14728 KB Output is correct