답안 #535249

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
535249 2022-03-09T18:31:10 Z faik Pipes (BOI13_pipes) C++14
44.0741 / 100
90 ms 12216 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){
      |        ~~~~~~~~~~~~~^~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 204 KB Output isn't correct
2 Incorrect 1 ms 204 KB Output isn't correct
3 Incorrect 1 ms 332 KB Output isn't correct
4 Incorrect 79 ms 11716 KB Output isn't correct
5 Incorrect 1 ms 204 KB Output isn't correct
6 Incorrect 0 ms 204 KB Output isn't correct
7 Incorrect 0 ms 204 KB Output isn't correct
8 Incorrect 0 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 53 ms 9308 KB Output isn't correct
14 Incorrect 52 ms 11084 KB Output isn't correct
15 Incorrect 78 ms 11668 KB Output isn't correct
16 Incorrect 62 ms 10000 KB Output isn't correct
17 Incorrect 57 ms 11716 KB Output isn't correct
18 Incorrect 58 ms 11752 KB Output isn't correct
19 Incorrect 57 ms 11380 KB Output isn't correct
20 Incorrect 1 ms 204 KB Output isn't correct
21 Incorrect 1 ms 332 KB Output isn't correct
22 Incorrect 83 ms 11784 KB Output isn't correct
23 Incorrect 70 ms 9320 KB Output isn't correct
24 Incorrect 61 ms 11740 KB Output isn't correct
25 Incorrect 49 ms 9768 KB Output isn't correct
# 결과 실행 시간 메모리 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 11028 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 1 ms 204 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 0 ms 204 KB Output is correct
12 Correct 0 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Incorrect 0 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 1 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 75 ms 9908 KB Output isn't correct
24 Incorrect 90 ms 11964 KB Output isn't correct
25 Correct 42 ms 11100 KB Output is correct
26 Correct 0 ms 204 KB Output is correct
27 Correct 0 ms 204 KB Output is correct
28 Correct 0 ms 204 KB Output is correct
29 Correct 1 ms 204 KB Output is correct
30 Incorrect 62 ms 11568 KB Output isn't correct
31 Incorrect 71 ms 12216 KB Output isn't correct
32 Incorrect 65 ms 11756 KB Output isn't correct
33 Correct 50 ms 11696 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 64 ms 11908 KB Output isn't correct
39 Incorrect 58 ms 11604 KB Output isn't correct
40 Incorrect 75 ms 11964 KB Output isn't correct
41 Correct 46 ms 12120 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 60 ms 11740 KB Output isn't correct
47 Incorrect 60 ms 12024 KB Output isn't correct
48 Incorrect 69 ms 12200 KB Output isn't correct
49 Correct 64 ms 10576 KB Output is correct
50 Correct 1 ms 204 KB Output is correct
51 Correct 1 ms 204 KB Output is correct
52 Correct 1 ms 204 KB Output is correct
53 Correct 0 ms 204 KB Output is correct
54 Incorrect 61 ms 11776 KB Output isn't correct