Submission #84110

# Submission time Handle Problem Language Result Execution time Memory
84110 2018-11-13T02:21:50 Z shoemakerjo Pipes (BOI13_pipes) C++14
74.0741 / 100
268 ms 20412 KB
#include <bits/stdc++.h>

using namespace std;

#define maxn 100010
#define pii pair<int, int>
#define maxm 500010
#define ll long long

int N, M;
vector<pii> adj[maxn];
vector<pii> adj2[maxn];
ll nums[maxn];
ll ans[maxm];
int deg[maxn];
bool del[maxn];
bool vis[maxn];

void dfstree(int u, int p) {

	for (pii vp : adj[u]) {
		if (vp.first != p) {
			dfstree(vp.first, u);
			ans[vp.second] = 2 * nums[vp.first];
			nums[u] -= ans[vp.second]/2;
		}
	}

}

void gotree() {
	dfstree(1, -1);

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

}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> N >> M;
	for (int i = 1; i <= N; i++) {
		cin >> nums[i];
	}
	int u, v;
	for (int i = 0; i < M; i++) {
		cin >> u >> v;
		deg[u]++;
		deg[v]++;
		adj[u].push_back(pii(v, i));
		adj[v].push_back(pii(u, i));

	}

	if (M > N) {
		cout << 0 << endl;
		return 0;
	}
	if (M == N-1) {
		gotree();
		return 0;
	}

	stack<int> s;
	//do some stuff
	for (int i = 1; i <= N; i++) {
		if (deg[i] == 1) s.push(i);
	}
	while (s.size()) {
		u = s.top(); s.pop();
		del[u] = true;
		for (pii vp : adj[u]) {
			if (!del[vp.first]) {
				deg[vp.first]--;
				ans[vp.second] = 2*nums[u];
				nums[vp.second] -= ans[vp.second]/2;
				if (deg[vp.first] == 1) {
					del[vp.first] = true;
					s.push(vp.first);
				}
			}
		}
	}


	int cur;
	for (int i = 1; i <= N; i++) {
		if (!del[i]) {
			cur = i;
			for (pii vp : adj[i]) {
				if (!del[vp.first]) {
					adj2[i].push_back(vp);
				}
			}
		}
	}


	ll sum = 0;
	sum += nums[cur];
	ll mult = -1;
	int og = cur;
	int numseen = 1;
	do {
		vis[cur] = true;
		// cout << "cur: " << cur << endl;

		for (int i = 1; i >= 0; i--) {
			pii vp = adj2[cur][i];
			if (!vis[vp.first]) {
				cur = vp.first;
				numseen++;
				// cout << "contribution: " << nums[cur] << " mult: " << mult << endl;
				sum += mult*nums[cur];
				mult *= -1LL;
				break;
			}
		}
	} while (!vis[cur]);

	// cout << "numseen: " << numseen << endl;
	if (numseen%2 == 0) {
		cout << 0 << endl;
		return 0;
	}

	fill(vis, vis+maxn, false);
	ans[adj2[og][0].second] = sum; //not sure if I have to divide by 2
	// cout << adj2[og][0].second << " assigned to " << sum << endl;
	cur = adj2[og][0].first;
	while (cur != og) {
		for (int i = 0; i <= 1; i++) {
			pii vp = adj2[cur][i];
			if (!vis[vp.first]) {

				ans[vp.second] = 2 * (nums[cur] - 
					ans[adj2[cur][1-i].second]/2);
				cur = vp.first;
				vis[cur] = true;
				break;
			}
		}
	}
	for (int i = 0; i < M; i++) {
		cout << ans[i] << '\n';
	}

	cout.flush();
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:103:17: warning: 'cur' may be used uninitialized in this function [-Wmaybe-uninitialized]
  sum += nums[cur];
         ~~~~~~~~^
# Verdict Execution time Memory Grader output
1 Correct 8 ms 4984 KB Output is correct
2 Correct 7 ms 5236 KB Output is correct
3 Correct 6 ms 5272 KB Output is correct
4 Correct 101 ms 11284 KB Output is correct
5 Correct 6 ms 11284 KB Output is correct
6 Correct 6 ms 11284 KB Output is correct
7 Correct 6 ms 11284 KB Output is correct
8 Correct 6 ms 11284 KB Output is correct
9 Correct 7 ms 11284 KB Output is correct
10 Correct 7 ms 11284 KB Output is correct
11 Correct 9 ms 11284 KB Output is correct
12 Correct 7 ms 11284 KB Output is correct
13 Correct 81 ms 11284 KB Output is correct
14 Correct 83 ms 11284 KB Output is correct
15 Correct 81 ms 11356 KB Output is correct
16 Correct 73 ms 11356 KB Output is correct
17 Correct 79 ms 11368 KB Output is correct
18 Correct 91 ms 11424 KB Output is correct
19 Correct 113 ms 14672 KB Output is correct
20 Correct 6 ms 14672 KB Output is correct
21 Correct 7 ms 14672 KB Output is correct
22 Correct 93 ms 14672 KB Output is correct
23 Correct 68 ms 14672 KB Output is correct
24 Correct 96 ms 14672 KB Output is correct
25 Correct 63 ms 14672 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 14672 KB Output isn't correct
2 Incorrect 8 ms 14672 KB Output isn't correct
3 Correct 78 ms 14672 KB Output is correct
4 Correct 66 ms 14672 KB Output is correct
5 Correct 61 ms 14672 KB Output is correct
6 Correct 257 ms 20412 KB Output is correct
7 Incorrect 6 ms 20412 KB Output isn't correct
8 Incorrect 7 ms 20412 KB Output isn't correct
9 Correct 6 ms 20412 KB Output is correct
10 Correct 6 ms 20412 KB Output is correct
11 Correct 6 ms 20412 KB Output is correct
12 Correct 6 ms 20412 KB Output is correct
13 Correct 6 ms 20412 KB Output is correct
14 Incorrect 6 ms 20412 KB Output isn't correct
15 Incorrect 7 ms 20412 KB Output isn't correct
16 Incorrect 7 ms 20412 KB Output isn't correct
17 Correct 6 ms 20412 KB Output is correct
18 Correct 6 ms 20412 KB Output is correct
19 Correct 7 ms 20412 KB Output is correct
20 Correct 6 ms 20412 KB Output is correct
21 Correct 7 ms 20412 KB Output is correct
22 Incorrect 7 ms 20412 KB Output isn't correct
23 Incorrect 69 ms 20412 KB Output isn't correct
24 Incorrect 85 ms 20412 KB Output isn't correct
25 Correct 70 ms 20412 KB Output is correct
26 Correct 57 ms 20412 KB Output is correct
27 Correct 54 ms 20412 KB Output is correct
28 Correct 58 ms 20412 KB Output is correct
29 Correct 197 ms 20412 KB Output is correct
30 Incorrect 109 ms 20412 KB Output isn't correct
31 Incorrect 94 ms 20412 KB Output isn't correct
32 Incorrect 89 ms 20412 KB Output isn't correct
33 Correct 78 ms 20412 KB Output is correct
34 Correct 61 ms 20412 KB Output is correct
35 Correct 64 ms 20412 KB Output is correct
36 Correct 85 ms 20412 KB Output is correct
37 Correct 268 ms 20412 KB Output is correct
38 Incorrect 101 ms 20412 KB Output isn't correct
39 Incorrect 88 ms 20412 KB Output isn't correct
40 Incorrect 94 ms 20412 KB Output isn't correct
41 Correct 97 ms 20412 KB Output is correct
42 Correct 64 ms 20412 KB Output is correct
43 Correct 62 ms 20412 KB Output is correct
44 Correct 57 ms 20412 KB Output is correct
45 Correct 174 ms 20412 KB Output is correct
46 Incorrect 99 ms 20412 KB Output isn't correct
47 Incorrect 112 ms 20412 KB Output isn't correct
48 Incorrect 106 ms 20412 KB Output isn't correct
49 Correct 70 ms 20412 KB Output is correct
50 Correct 60 ms 20412 KB Output is correct
51 Correct 60 ms 20412 KB Output is correct
52 Correct 62 ms 20412 KB Output is correct
53 Correct 198 ms 20412 KB Output is correct
54 Incorrect 87 ms 20412 KB Output isn't correct