Submission #535250

# Submission time Handle Problem Language Result Execution time Memory
535250 2022-03-09T18:33:46 Z faik Pipes (BOI13_pipes) C++14
44.0741 / 100
69 ms 12280 KB
#include <bits/stdc++.h>

using namespace std;

#define MOD 998244353
#define INF 1000000001
#define LNF 1000000000000000001

#define int ll

typedef long long ll;
typedef pair<int,int> pii;



void solve(){
//#define TEST
	int n,m;cin >> n >> m;
	if(m > n){
		cout << "0";
		return;
	}
	vector<int> net(n);
	for(int i = 0;i < n;i++){
		cin >> net[i];
	}
	vector<int> ret(m);
	vector<vector<pii>> edge(n);
	for(int i = 0;i < m;i++){
		int a,b;cin >> a >> b;a--;b--;
		edge[a].emplace_back(b,i);
		edge[b].emplace_back(a,i);
	}
	stack<int> leaf;
	vector<int> sizes(n);
	vector<int> removed(n);
	for(int i = 0;i < n;i++){
		sizes[i] = edge[i].size();
		if(edge[i].size() == 1){
			leaf.push(i);
		}
	}
	while(!leaf.empty()){	
		int top = leaf.top();
		leaf.pop();
		removed[top] = true;
		pii parent;
		for(auto i : edge[top]){
			if(!removed[i.first])
				parent = i;
		}
		ret[parent.second] = net[top]*2;
		sizes[top]--;
		sizes[parent.first]--;
		net[parent.first] -= net[top];
		if(sizes[parent.first] == 1){
			leaf.push(parent.first);
		}
	}
	int left = 0;
	vector<int> cycle;
	for(int i = 0;i < n;i++){
		if(!removed[i]){
			left++;
			cycle.push_back(i);
		}
	}
	if(left == 0){
		for(int i : ret)
			cout << i << ' ';
		return;
	} else if(left%2 == 0){
		cout << '0';
		return;
	}

	int curr = cycle[0];
	int next;int nextRet;
	for(auto i : edge[curr]){
		if(!removed[i.first]){
			next = i.first;
			nextRet = i.second;
		}
	}
	ret[nextRet] += 2*net[cycle[0]];
	net[next] -= net[cycle[0]];
	net[cycle[0]] = 0;
	int last = cycle[0];
	curr = next;

	while(curr != cycle[0]){
		pii next1;
		for(auto i : edge[curr]){
			if(!removed[i.first] && i.first != last){
				next1 = i;
			}	
		}
		ret[next1.second] += 2*net[curr];
		// cout << next1.second << ' ' << 2*net[curr] << '\n';
		net[next1.first] -= net[curr];
		net[curr] = 0;
		last = curr;
		curr = next1.first;
	}

	if(net[cycle[0]] % 2 != 0){
		cout << 0;
		return;
	}

	last = cycle[0];
	curr = next;
	int bruh = next;
	int plus = 1;

	bool cum = false;
	// cout << '*';
	// cout << net[cycle[0]];
	while(curr != next || !cum){
		pii next1;
		for(auto i : edge[curr]){
			if(!removed[i.first] && i.first != last){
				next1 = i;
			}	
		}
		ret[next1.second] += net[cycle[0]]*plus;
		// cout << next1.second << ' ' << net[cycle[0]]*plus << '\n';
		last = curr;
		curr = next1.first;
		plus *= -1;
		cum = true;
	}
	// cout << "\n*";
	for(int i : ret)
		cout << i << ' ';
	// cout << '\n';
	// for(int i : net)
		// cout << i << ' ';
}

signed main(){
	cin.tie(0);
	ios_base::sync_with_stdio(0);
#ifdef DEBUG
	freopen("i.txt","r",stdin);
	freopen("o.txt","w",stdout);
#endif
	int t = 1;
#ifdef TEST
	cin >> t;
#endif
	while(t--){
		solve();
	}

	return 0;
}

Compilation message

pipes.cpp: In function 'void solve()':
pipes.cpp:113:6: warning: unused variable 'bruh' [-Wunused-variable]
  113 |  int bruh = next;
      |      ^~~~
pipes.cpp:85:13: warning: 'nextRet' may be used uninitialized in this function [-Wmaybe-uninitialized]
   85 |  ret[nextRet] += 2*net[cycle[0]];
      |             ^
pipes.cpp:119:21: warning: 'next' may be used uninitialized in this function [-Wmaybe-uninitialized]
  119 |  while(curr != next || !cum){
      |        ~~~~~~~~~~~~~^~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 0 ms 204 KB Output isn't correct
2 Incorrect 0 ms 204 KB Output isn't correct
3 Incorrect 1 ms 332 KB Output isn't correct
4 Incorrect 57 ms 11648 KB Output isn't correct
5 Incorrect 0 ms 204 KB Output isn't correct
6 Incorrect 0 ms 204 KB Output isn't correct
7 Incorrect 1 ms 204 KB Output isn't correct
8 Incorrect 1 ms 204 KB Output isn't correct
9 Incorrect 1 ms 332 KB Output isn't correct
10 Incorrect 1 ms 332 KB Output isn't correct
11 Incorrect 1 ms 332 KB Output isn't correct
12 Incorrect 1 ms 332 KB Output isn't correct
13 Incorrect 44 ms 9324 KB Output isn't correct
14 Incorrect 56 ms 11104 KB Output isn't correct
15 Incorrect 59 ms 11728 KB Output isn't correct
16 Incorrect 46 ms 10052 KB Output isn't correct
17 Incorrect 58 ms 11716 KB Output isn't correct
18 Incorrect 59 ms 11716 KB Output isn't correct
19 Incorrect 56 ms 11388 KB Output isn't correct
20 Incorrect 0 ms 204 KB Output isn't correct
21 Incorrect 1 ms 332 KB Output isn't correct
22 Incorrect 60 ms 11728 KB Output isn't correct
23 Incorrect 44 ms 9412 KB Output isn't correct
24 Incorrect 59 ms 11736 KB Output isn't correct
25 Incorrect 44 ms 9796 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 204 KB Output isn't correct
2 Incorrect 1 ms 332 KB Output isn't correct
3 Correct 44 ms 10992 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Incorrect 0 ms 284 KB Output isn't correct
8 Incorrect 0 ms 204 KB Output isn't correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 1 ms 204 KB Output is correct
11 Correct 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 0 ms 204 KB Output is correct
14 Incorrect 1 ms 204 KB Output isn't correct
15 Incorrect 1 ms 332 KB Output isn't correct
16 Incorrect 1 ms 332 KB Output isn't correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 0 ms 204 KB Output is correct
20 Correct 0 ms 204 KB Output is correct
21 Correct 0 ms 204 KB Output is correct
22 Incorrect 1 ms 332 KB Output isn't correct
23 Incorrect 56 ms 9940 KB Output isn't correct
24 Incorrect 65 ms 11972 KB Output isn't correct
25 Correct 48 ms 11112 KB Output is correct
26 Correct 0 ms 204 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 1 ms 204 KB Output is correct
29 Correct 0 ms 204 KB Output is correct
30 Incorrect 63 ms 11584 KB Output isn't correct
31 Incorrect 67 ms 12184 KB Output isn't correct
32 Incorrect 63 ms 11808 KB Output isn't correct
33 Correct 43 ms 11712 KB Output is correct
34 Correct 0 ms 204 KB Output is correct
35 Correct 0 ms 204 KB Output is correct
36 Correct 0 ms 204 KB Output is correct
37 Correct 0 ms 204 KB Output is correct
38 Incorrect 66 ms 11860 KB Output isn't correct
39 Incorrect 59 ms 11640 KB Output isn't correct
40 Incorrect 69 ms 11940 KB Output isn't correct
41 Correct 42 ms 12024 KB Output is correct
42 Correct 0 ms 204 KB Output is correct
43 Correct 0 ms 204 KB Output is correct
44 Correct 0 ms 204 KB Output is correct
45 Correct 0 ms 204 KB Output is correct
46 Incorrect 62 ms 11796 KB Output isn't correct
47 Incorrect 65 ms 11976 KB Output isn't correct
48 Incorrect 64 ms 12280 KB Output isn't correct
49 Correct 44 ms 10628 KB Output is correct
50 Correct 0 ms 204 KB Output is correct
51 Correct 0 ms 204 KB Output is correct
52 Correct 0 ms 204 KB Output is correct
53 Correct 0 ms 204 KB Output is correct
54 Incorrect 66 ms 11752 KB Output isn't correct