Submission #535371

# Submission time Handle Problem Language Result Execution time Memory
535371 2022-03-10T06:32:32 Z faik Pipes (BOI13_pipes) C++14
100 / 100
69 ms 13816 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);
	}
	//find leafs
	queue<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);
		}
	}
	//remove all leaves
	while(!leaf.empty()){	
		int top = leaf.front();
		leaf.pop();
		if(removed[top])
			continue;
		removed[top] = true;
		pii parent;
		for(auto i : edge[top]){
			if(!removed[i.first])
				parent = i;
		}
		// cout << top+1 << ' ' << parent.first+1 << ' ' << parent.second+1 << ' ' << net[top] << '\n';
		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);
		}
		if(sizes[parent.first] == 0){
			removed[parent.first] = true;
		}
	}
	//find the nodes in the last cycle
	int left = 0;
	vector<int> cycle;
	for(int i = 0;i < n;i++){
		if(!removed[i]){
			left++;
			cycle.push_back(i);
		}
	}
	//if there is no cycle we are finished
	if(left == 0){
		for(int i : ret)
			cout << i << ' ';
		return;
	} else if(left%2 == 0){//even cycle is bad
		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 = next;
	curr = cycle[0];
	int plus = 1;

	bool firstNode = true;
	// cout << '*';
	// cout << net[cycle[0]];
	while(curr != cycle[0] || firstNode){
		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 = -plus;
		firstNode = false;
	}
	// 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:96:13: warning: 'nextRet' may be used uninitialized in this function [-Wmaybe-uninitialized]
   96 |  ret[nextRet] += 2*net[cycle[0]];
      |             ^
pipes.cpp:102:13: warning: 'next' may be used uninitialized in this function [-Wmaybe-uninitialized]
  102 |  while(curr != cycle[0]){
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 58 ms 13256 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 372 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 45 ms 10572 KB Output is correct
14 Correct 54 ms 12572 KB Output is correct
15 Correct 57 ms 13324 KB Output is correct
16 Correct 48 ms 11340 KB Output is correct
17 Correct 59 ms 13252 KB Output is correct
18 Correct 58 ms 13372 KB Output is correct
19 Correct 58 ms 13012 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 57 ms 13260 KB Output is correct
23 Correct 44 ms 10536 KB Output is correct
24 Correct 58 ms 13344 KB Output is correct
25 Correct 46 ms 11108 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 468 KB Output is correct
3 Correct 45 ms 12592 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 324 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 320 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 1 ms 468 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 0 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 47 ms 11176 KB Output is correct
24 Correct 61 ms 13616 KB Output is correct
25 Correct 47 ms 12588 KB Output is correct
26 Correct 0 ms 340 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 0 ms 332 KB Output is correct
29 Correct 0 ms 336 KB Output is correct
30 Correct 66 ms 13112 KB Output is correct
31 Correct 69 ms 13816 KB Output is correct
32 Correct 58 ms 13352 KB Output is correct
33 Correct 47 ms 13292 KB Output is correct
34 Correct 0 ms 340 KB Output is correct
35 Correct 0 ms 340 KB Output is correct
36 Correct 1 ms 352 KB Output is correct
37 Correct 0 ms 340 KB Output is correct
38 Correct 63 ms 13468 KB Output is correct
39 Correct 64 ms 13132 KB Output is correct
40 Correct 63 ms 13580 KB Output is correct
41 Correct 42 ms 13696 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 0 ms 340 KB Output is correct
45 Correct 0 ms 340 KB Output is correct
46 Correct 65 ms 13348 KB Output is correct
47 Correct 63 ms 13616 KB Output is correct
48 Correct 64 ms 13744 KB Output is correct
49 Correct 46 ms 12068 KB Output is correct
50 Correct 0 ms 336 KB Output is correct
51 Correct 0 ms 340 KB Output is correct
52 Correct 0 ms 336 KB Output is correct
53 Correct 0 ms 340 KB Output is correct
54 Correct 63 ms 13316 KB Output is correct