Submission #84107

# Submission time Handle Problem Language Result Execution time Memory
84107 2018-11-13T02:07:26 Z shoemakerjo Pipes (BOI13_pipes) C++14
65 / 100
249 ms 62100 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;
	do {

		for (pii vp : adj2[cur]) {
			if (!vis[vp.first]) {
				cur = vp.first;
				vis[cur] = true;
				sum += mult*nums[cur];
				mult *= -1LL;
				break;
			}
		}
	} while (!vis[cur]);

	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:124: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 5248 KB Output is correct
3 Correct 7 ms 5248 KB Output is correct
4 Correct 84 ms 12564 KB Output is correct
5 Correct 6 ms 12564 KB Output is correct
6 Correct 6 ms 12564 KB Output is correct
7 Correct 7 ms 12564 KB Output is correct
8 Correct 6 ms 12564 KB Output is correct
9 Correct 6 ms 12564 KB Output is correct
10 Correct 7 ms 12564 KB Output is correct
11 Correct 6 ms 12564 KB Output is correct
12 Correct 7 ms 12564 KB Output is correct
13 Correct 66 ms 12596 KB Output is correct
14 Correct 74 ms 14960 KB Output is correct
15 Correct 87 ms 16996 KB Output is correct
16 Correct 64 ms 17420 KB Output is correct
17 Correct 83 ms 19536 KB Output is correct
18 Correct 77 ms 20616 KB Output is correct
19 Correct 91 ms 23872 KB Output is correct
20 Correct 6 ms 23872 KB Output is correct
21 Correct 7 ms 23872 KB Output is correct
22 Correct 92 ms 23872 KB Output is correct
23 Correct 66 ms 23872 KB Output is correct
24 Correct 75 ms 23872 KB Output is correct
25 Correct 65 ms 23872 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 23872 KB Output isn't correct
2 Incorrect 6 ms 23872 KB Output isn't correct
3 Incorrect 89 ms 23872 KB Output isn't correct
4 Correct 65 ms 23872 KB Output is correct
5 Correct 64 ms 23872 KB Output is correct
6 Correct 246 ms 38804 KB Output is correct
7 Incorrect 6 ms 38804 KB Output isn't correct
8 Incorrect 6 ms 38804 KB Output isn't correct
9 Incorrect 6 ms 38804 KB Output isn't correct
10 Correct 6 ms 38804 KB Output is correct
11 Correct 6 ms 38804 KB Output is correct
12 Correct 6 ms 38804 KB Output is correct
13 Correct 6 ms 38804 KB Output is correct
14 Incorrect 8 ms 38804 KB Output isn't correct
15 Incorrect 7 ms 38804 KB Output isn't correct
16 Incorrect 7 ms 38804 KB Output isn't correct
17 Incorrect 8 ms 38804 KB Output isn't correct
18 Correct 8 ms 38804 KB Output is correct
19 Correct 7 ms 38804 KB Output is correct
20 Correct 6 ms 38804 KB Output is correct
21 Correct 8 ms 38804 KB Output is correct
22 Incorrect 7 ms 38804 KB Output isn't correct
23 Incorrect 97 ms 38804 KB Output isn't correct
24 Incorrect 99 ms 38804 KB Output isn't correct
25 Incorrect 84 ms 38804 KB Output isn't correct
26 Correct 67 ms 38804 KB Output is correct
27 Correct 61 ms 38804 KB Output is correct
28 Correct 79 ms 38804 KB Output is correct
29 Correct 218 ms 46120 KB Output is correct
30 Incorrect 103 ms 46120 KB Output isn't correct
31 Incorrect 90 ms 46120 KB Output isn't correct
32 Incorrect 92 ms 46120 KB Output isn't correct
33 Incorrect 104 ms 46120 KB Output isn't correct
34 Correct 67 ms 46120 KB Output is correct
35 Correct 72 ms 46120 KB Output is correct
36 Correct 62 ms 46120 KB Output is correct
37 Correct 249 ms 60128 KB Output is correct
38 Incorrect 93 ms 60128 KB Output isn't correct
39 Incorrect 85 ms 60128 KB Output isn't correct
40 Incorrect 91 ms 60128 KB Output isn't correct
41 Incorrect 103 ms 60128 KB Output isn't correct
42 Correct 72 ms 60128 KB Output is correct
43 Correct 64 ms 60128 KB Output is correct
44 Correct 65 ms 60128 KB Output is correct
45 Correct 210 ms 62100 KB Output is correct
46 Incorrect 107 ms 62100 KB Output isn't correct
47 Incorrect 96 ms 62100 KB Output isn't correct
48 Incorrect 106 ms 62100 KB Output isn't correct
49 Incorrect 77 ms 62100 KB Output isn't correct
50 Correct 57 ms 62100 KB Output is correct
51 Correct 61 ms 62100 KB Output is correct
52 Correct 60 ms 62100 KB Output is correct
53 Correct 220 ms 62100 KB Output is correct
54 Incorrect 97 ms 62100 KB Output isn't correct