Submission #84108

# Submission time Handle Problem Language Result Execution time Memory
84108 2018-11-13T02:08:51 Z shoemakerjo Pipes (BOI13_pipes) C++14
74.0741 / 100
244 ms 20396 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;
	vis[cur] = true;
	int og = cur;
	int numseen = 1;
	do {

		for (pii vp : adj2[cur]) {
			if (!vis[vp.first]) {
				cur = vp.first;
				vis[cur] = true;
				numseen++;
				sum += mult*nums[cur];
				mult *= -1LL;
				break;
			}
		}
	} while (!vis[cur]);
	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
	cur = adj2[og][0].first;
	while (cur != og) {
		for (int i = 0; i< adj2[cur].size(); 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:130:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int i = 0; i< adj2[cur].size(); i++) {
                   ~^~~~~~~~~~~~~~~~~~
pipes.cpp:89:6: warning: 'cur' may be used uninitialized in this function [-Wmaybe-uninitialized]
  int cur;
      ^~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 5112 KB Output is correct
2 Correct 6 ms 5116 KB Output is correct
3 Correct 6 ms 5372 KB Output is correct
4 Correct 72 ms 11376 KB Output is correct
5 Correct 6 ms 11376 KB Output is correct
6 Correct 6 ms 11376 KB Output is correct
7 Correct 6 ms 11376 KB Output is correct
8 Correct 6 ms 11376 KB Output is correct
9 Correct 6 ms 11376 KB Output is correct
10 Correct 8 ms 11376 KB Output is correct
11 Correct 6 ms 11376 KB Output is correct
12 Correct 6 ms 11376 KB Output is correct
13 Correct 67 ms 11376 KB Output is correct
14 Correct 82 ms 11376 KB Output is correct
15 Correct 81 ms 11548 KB Output is correct
16 Correct 67 ms 11548 KB Output is correct
17 Correct 75 ms 11548 KB Output is correct
18 Correct 71 ms 11548 KB Output is correct
19 Correct 79 ms 14784 KB Output is correct
20 Correct 6 ms 14784 KB Output is correct
21 Correct 6 ms 14784 KB Output is correct
22 Correct 75 ms 14784 KB Output is correct
23 Correct 59 ms 14784 KB Output is correct
24 Correct 88 ms 14784 KB Output is correct
25 Correct 103 ms 14784 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 14784 KB Output isn't correct
2 Incorrect 6 ms 14784 KB Output isn't correct
3 Correct 87 ms 14784 KB Output is correct
4 Correct 58 ms 14784 KB Output is correct
5 Correct 57 ms 14784 KB Output is correct
6 Correct 244 ms 20340 KB Output is correct
7 Incorrect 6 ms 20340 KB Output isn't correct
8 Incorrect 6 ms 20340 KB Output isn't correct
9 Correct 6 ms 20340 KB Output is correct
10 Correct 6 ms 20340 KB Output is correct
11 Correct 6 ms 20340 KB Output is correct
12 Correct 6 ms 20340 KB Output is correct
13 Correct 6 ms 20340 KB Output is correct
14 Incorrect 6 ms 20340 KB Output isn't correct
15 Incorrect 8 ms 20340 KB Output isn't correct
16 Incorrect 6 ms 20340 KB Output isn't correct
17 Correct 6 ms 20340 KB Output is correct
18 Correct 6 ms 20340 KB Output is correct
19 Correct 7 ms 20340 KB Output is correct
20 Correct 7 ms 20340 KB Output is correct
21 Correct 7 ms 20340 KB Output is correct
22 Incorrect 6 ms 20340 KB Output isn't correct
23 Incorrect 67 ms 20340 KB Output isn't correct
24 Incorrect 77 ms 20340 KB Output isn't correct
25 Correct 71 ms 20340 KB Output is correct
26 Correct 103 ms 20340 KB Output is correct
27 Correct 59 ms 20340 KB Output is correct
28 Correct 64 ms 20340 KB Output is correct
29 Correct 201 ms 20340 KB Output is correct
30 Incorrect 83 ms 20340 KB Output isn't correct
31 Incorrect 70 ms 20340 KB Output isn't correct
32 Incorrect 87 ms 20340 KB Output isn't correct
33 Correct 88 ms 20340 KB Output is correct
34 Correct 57 ms 20340 KB Output is correct
35 Correct 64 ms 20340 KB Output is correct
36 Correct 58 ms 20340 KB Output is correct
37 Correct 243 ms 20396 KB Output is correct
38 Incorrect 82 ms 20396 KB Output isn't correct
39 Incorrect 87 ms 20396 KB Output isn't correct
40 Incorrect 95 ms 20396 KB Output isn't correct
41 Correct 77 ms 20396 KB Output is correct
42 Correct 63 ms 20396 KB Output is correct
43 Correct 60 ms 20396 KB Output is correct
44 Correct 67 ms 20396 KB Output is correct
45 Correct 193 ms 20396 KB Output is correct
46 Incorrect 79 ms 20396 KB Output isn't correct
47 Incorrect 76 ms 20396 KB Output isn't correct
48 Incorrect 110 ms 20396 KB Output isn't correct
49 Correct 74 ms 20396 KB Output is correct
50 Correct 57 ms 20396 KB Output is correct
51 Correct 58 ms 20396 KB Output is correct
52 Correct 58 ms 20396 KB Output is correct
53 Correct 198 ms 20396 KB Output is correct
54 Incorrect 76 ms 20396 KB Output isn't correct