Submission #535248

# Submission time Handle Problem Language Result Execution time Memory
535248 2022-03-09T18:24:38 Z faik Pipes (BOI13_pipes) C++14
44.0741 / 100
89 ms 13940 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;
	}

	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:108:6: warning: unused variable 'bruh' [-Wunused-variable]
  108 |  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:114:21: warning: 'next' may be used uninitialized in this function [-Wmaybe-uninitialized]
  114 |  while(curr != next || !cum){
      |        ~~~~~~~~~~~~~^~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 312 KB Output isn't correct
2 Incorrect 1 ms 332 KB Output isn't correct
3 Incorrect 1 ms 324 KB Output isn't correct
4 Incorrect 84 ms 13228 KB Output isn't correct
5 Incorrect 1 ms 204 KB Output isn't correct
6 Incorrect 1 ms 204 KB Output isn't correct
7 Incorrect 1 ms 308 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 45 ms 10564 KB Output isn't correct
14 Incorrect 64 ms 12616 KB Output isn't correct
15 Incorrect 63 ms 13360 KB Output isn't correct
16 Incorrect 52 ms 11308 KB Output isn't correct
17 Incorrect 61 ms 13232 KB Output isn't correct
18 Incorrect 61 ms 13356 KB Output isn't correct
19 Incorrect 61 ms 12996 KB Output isn't correct
20 Incorrect 1 ms 308 KB Output isn't correct
21 Incorrect 1 ms 308 KB Output isn't correct
22 Incorrect 62 ms 13360 KB Output isn't correct
23 Incorrect 51 ms 10628 KB Output isn't correct
24 Incorrect 64 ms 13364 KB Output isn't correct
25 Incorrect 46 ms 11044 KB Output isn't correct
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 304 KB Output isn't correct
2 Incorrect 1 ms 452 KB Output isn't correct
3 Correct 62 ms 12532 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 0 ms 328 KB Output is correct
7 Incorrect 1 ms 204 KB Output isn't correct
8 Incorrect 1 ms 204 KB Output isn't correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Correct 1 ms 312 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Incorrect 1 ms 312 KB Output isn't correct
15 Incorrect 1 ms 444 KB Output isn't correct
16 Incorrect 1 ms 332 KB Output isn't correct
17 Correct 1 ms 444 KB Output is correct
18 Correct 1 ms 204 KB Output is correct
19 Correct 1 ms 204 KB Output is correct
20 Correct 1 ms 204 KB Output is correct
21 Correct 1 ms 328 KB Output is correct
22 Incorrect 1 ms 332 KB Output isn't correct
23 Incorrect 64 ms 11172 KB Output isn't correct
24 Incorrect 66 ms 13500 KB Output isn't correct
25 Correct 44 ms 12512 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 0 ms 332 KB Output is correct
28 Correct 1 ms 332 KB Output is correct
29 Correct 1 ms 332 KB Output is correct
30 Incorrect 61 ms 13124 KB Output isn't correct
31 Incorrect 75 ms 13940 KB Output isn't correct
32 Incorrect 89 ms 13376 KB Output isn't correct
33 Correct 61 ms 13240 KB Output is correct
34 Correct 1 ms 332 KB Output is correct
35 Correct 1 ms 332 KB Output is correct
36 Correct 1 ms 324 KB Output is correct
37 Correct 1 ms 332 KB Output is correct
38 Incorrect 63 ms 13476 KB Output isn't correct
39 Incorrect 66 ms 13144 KB Output isn't correct
40 Incorrect 63 ms 13548 KB Output isn't correct
41 Correct 47 ms 13736 KB Output is correct
42 Correct 1 ms 328 KB Output is correct
43 Correct 1 ms 332 KB Output is correct
44 Correct 0 ms 332 KB Output is correct
45 Correct 1 ms 324 KB Output is correct
46 Incorrect 66 ms 13340 KB Output isn't correct
47 Incorrect 63 ms 13496 KB Output isn't correct
48 Incorrect 70 ms 13808 KB Output isn't correct
49 Correct 47 ms 12080 KB Output is correct
50 Correct 1 ms 332 KB Output is correct
51 Correct 1 ms 332 KB Output is correct
52 Correct 1 ms 324 KB Output is correct
53 Correct 1 ms 332 KB Output is correct
54 Incorrect 67 ms 13292 KB Output isn't correct