Submission #443806

# Submission time Handle Problem Language Result Execution time Memory
443806 2021-07-12T06:41:32 Z wind_reaper Pipes (BOI13_pipes) C++17
100 / 100
557 ms 68452 KB
#include <bits/stdc++.h>

using namespace std;
#define int int64_t

const int MXN = 100000;

set<array<int, 2>> g[MXN];
int change[MXN], suf[MXN];
bitset<MXN> vis;
int a[MXN], b[MXN], ans[MXN + 1];

void solve(){
	int N, M;
	cin >> N >> M;

	for(int i = 0; i < N; i++)
		cin >> change[i];

	for(int i = 0; i < M; i++){
		int u, v;
		cin >> u >> v;
		--u, --v;
		g[u].insert({v, i});
		g[v].insert({u, i});
	}

	if(M > N){
		cout << 0 << '\n';
		return;
	}

	queue<array<int, 3>> q;
	for(int i = 0; i < N; i++)
		if(g[i].size() < 2){
			q.push({i, (*g[i].begin())[0], (*g[i].begin())[1]});
			vis[i] = 1;
		}

	int cnt = 0;

	while(!q.empty()){
		auto [u, par, idx] = q.front();
		q.pop();

		++cnt;

		int t = change[u];
		ans[idx] += 2*t;
		change[u] -= t;
		change[par] -= t;

		g[par].erase({u, idx});

		if(g[par].size() < 2 && !vis[par]){
			q.push({par, (*g[par].begin())[0], (*g[par].begin())[1]});
			vis[par] = 1;
		}
	}

	if(cnt < N){
		if(!((N - cnt) & 1)){
			cout << 0 << '\n';
			return;
		}

		int x;
		for(int i = 0; i < N; i++) 
			if(!vis[i]) 
				x = i;

		int u = x;
		N = 0;

		while(u != x || N == 0){
			auto [v, idx] = (N == 0 || (*g[u].begin())[0] != a[N-1] ? *g[u].begin() : *g[u].rbegin());
			a[N] = u;
			b[N++] = idx;
			u = v;
		}

		suf[N-1] = change[a[N-1]];
		for(int i = N - 2; i >= 0; --i)
			suf[i] = change[a[i]] - suf[i+1];
		int pre = 0;

		for(int i = 0; i < N; i++){
			pre = change[a[i]] - pre;
			ans[b[i]] = (pre + (i + 1 < N ? suf[i+1] : 0));
		}
	}

	for(int i = 0; i < M; i++)
		cout << ans[i] << '\n';
}

int32_t main(){
	ios_base::sync_with_stdio(false); 
	cin.tie(NULL); 

	solve();

	return 0; 
}

Compilation message

pipes.cpp: In function 'void solve()':
pipes.cpp:75:16: warning: 'x' may be used uninitialized in this function [-Wmaybe-uninitialized]
   75 |   while(u != x || N == 0){
      |         ~~~~~~~^~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 3 ms 4940 KB Output is correct
3 Correct 5 ms 5068 KB Output is correct
4 Correct 146 ms 20476 KB Output is correct
5 Correct 3 ms 4940 KB Output is correct
6 Correct 3 ms 4940 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 4 ms 4940 KB Output is correct
9 Correct 5 ms 5068 KB Output is correct
10 Correct 5 ms 5068 KB Output is correct
11 Correct 4 ms 5152 KB Output is correct
12 Correct 5 ms 5124 KB Output is correct
13 Correct 106 ms 17552 KB Output is correct
14 Correct 131 ms 19712 KB Output is correct
15 Correct 159 ms 20612 KB Output is correct
16 Correct 119 ms 18332 KB Output is correct
17 Correct 140 ms 20492 KB Output is correct
18 Correct 137 ms 20556 KB Output is correct
19 Correct 139 ms 19680 KB Output is correct
20 Correct 3 ms 5012 KB Output is correct
21 Correct 4 ms 5068 KB Output is correct
22 Correct 136 ms 20592 KB Output is correct
23 Correct 100 ms 17516 KB Output is correct
24 Correct 136 ms 20588 KB Output is correct
25 Correct 116 ms 18108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4940 KB Output is correct
2 Correct 4 ms 5196 KB Output is correct
3 Correct 98 ms 18980 KB Output is correct
4 Correct 74 ms 18292 KB Output is correct
5 Correct 76 ms 17908 KB Output is correct
6 Correct 557 ms 68452 KB Output is correct
7 Correct 3 ms 4940 KB Output is correct
8 Correct 3 ms 4940 KB Output is correct
9 Correct 3 ms 4940 KB Output is correct
10 Correct 3 ms 4940 KB Output is correct
11 Correct 3 ms 4940 KB Output is correct
12 Correct 3 ms 4940 KB Output is correct
13 Correct 3 ms 4940 KB Output is correct
14 Correct 3 ms 4940 KB Output is correct
15 Correct 4 ms 5196 KB Output is correct
16 Correct 4 ms 5212 KB Output is correct
17 Correct 4 ms 5100 KB Output is correct
18 Correct 4 ms 5068 KB Output is correct
19 Correct 4 ms 5068 KB Output is correct
20 Correct 4 ms 5068 KB Output is correct
21 Correct 5 ms 5396 KB Output is correct
22 Correct 4 ms 5196 KB Output is correct
23 Correct 96 ms 18328 KB Output is correct
24 Correct 116 ms 21316 KB Output is correct
25 Correct 100 ms 18936 KB Output is correct
26 Correct 89 ms 18304 KB Output is correct
27 Correct 75 ms 18120 KB Output is correct
28 Correct 80 ms 18856 KB Output is correct
29 Correct 400 ms 55988 KB Output is correct
30 Correct 105 ms 20412 KB Output is correct
31 Correct 108 ms 21732 KB Output is correct
32 Correct 131 ms 20932 KB Output is correct
33 Correct 98 ms 19652 KB Output is correct
34 Correct 77 ms 18396 KB Output is correct
35 Correct 76 ms 18220 KB Output is correct
36 Correct 79 ms 18436 KB Output is correct
37 Correct 556 ms 68428 KB Output is correct
38 Correct 114 ms 20920 KB Output is correct
39 Correct 153 ms 20792 KB Output is correct
40 Correct 119 ms 21240 KB Output is correct
41 Correct 81 ms 19244 KB Output is correct
42 Correct 73 ms 18272 KB Output is correct
43 Correct 74 ms 18308 KB Output is correct
44 Correct 74 ms 17920 KB Output is correct
45 Correct 509 ms 60976 KB Output is correct
46 Correct 110 ms 20408 KB Output is correct
47 Correct 118 ms 21228 KB Output is correct
48 Correct 112 ms 21680 KB Output is correct
49 Correct 106 ms 19252 KB Output is correct
50 Correct 81 ms 18256 KB Output is correct
51 Correct 81 ms 18248 KB Output is correct
52 Correct 77 ms 17988 KB Output is correct
53 Correct 512 ms 62288 KB Output is correct
54 Correct 114 ms 20548 KB Output is correct