Submission #84257

# Submission time Handle Problem Language Result Execution time Memory
84257 2018-11-14T03:44:56 Z IOrtroiii Pipes (BOI13_pipes) C++14
100 / 100
131 ms 15552 KB
/*
input
3 3
2 2 2
1 2
2 3
3 1
*/
#include <bits/stdc++.h>
using namespace std;

const int N = 100005;

int n, m;
int deg[N];
bool ok[N];
long long a[N], ans[N];
vector<pair<int,int>> g[N];
bool was[N];
long long sum = 0, cur = 0;
bool have;

void dfs(int u,int p,int now) {
	if (now) cur += a[u];
	was[u] = 1;
	for (auto e : g[u]) {
		int v = e.first;
		int id = e.second;
		if (v == p) continue;
		if (ok[v]) continue;
		if (was[v]) {
			if (now) {
				if (have) return;
				ans[id] = cur - sum;
				a[u] -= ans[id];
				a[v] -= ans[id];
				have = true;
			} else {
				printf("0\n"); exit(0);
			}
		}
		else {
			dfs(v, u, now ^ 1);
			ans[id] = a[v];
			a[u] -= ans[id];
			a[v] -= ans[id];
		}
	}
}

int main() {
	scanf("%d %d", &n, &m);
	if (n < m) return printf("0\n"),0;
	for (int i = 1; i <= n; ++i) scanf("%lld", a + i);
	for (int i = 1; i <= m; ++i) {
		int u, v; scanf("%d %d", &u, &v);
		++deg[u], ++deg[v];
		g[u].push_back({v, i}), g[v].push_back({u, i});	
	}
	queue<int> q;
	for (int u = 1; u <= n; ++u) if (deg[u] == 1) {
		q.push(u);
	}
	while (!q.empty()) {
		int u = q.front(); q.pop();
		for (auto e : g[u]) {
			int v = e.first;
			int id = e.second;
			if (ok[v] == false) {
				ans[id] = a[u];
				a[u] -= ans[id];
				a[v] -= ans[id];
				ok[u] = true;
				if (--deg[v] == 1) {
					q.push(v);
				}
			}
		}
	}
	for (int i = 1; i <= n; ++i) if (ok[i] == false) sum += a[i];
	if (sum & 1) return printf("0\n"),0;
	sum /= 2;
	for (int i = 1; i <= n; ++i) if (ok[i] == false) {
		dfs(i, -1, 1); break;
	}
	for (int i = 1; i <= n; ++i) if (a[i]) return printf("0\n"),0;
	for (int i = 1; i <= m; ++i) if (ans[i] > (long long) 5e8 || ans[i] < (long long) -5e8) {
		return printf("0\n"),0;
	}
	for (int i = 1; i <= m; ++i) printf("%lld\n", 2 * ans[i]);
}

Compilation message

pipes.cpp: In function 'int main()':
pipes.cpp:52:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &m);
  ~~~~~^~~~~~~~~~~~~~~~~
pipes.cpp:54:36: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (int i = 1; i <= n; ++i) scanf("%lld", a + i);
                               ~~~~~^~~~~~~~~~~~~~~
pipes.cpp:56:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int u, v; scanf("%d %d", &u, &v);
             ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2680 KB Output is correct
2 Correct 4 ms 2684 KB Output is correct
3 Correct 4 ms 2856 KB Output is correct
4 Correct 131 ms 9076 KB Output is correct
5 Correct 4 ms 9076 KB Output is correct
6 Correct 4 ms 9076 KB Output is correct
7 Correct 4 ms 9076 KB Output is correct
8 Correct 4 ms 9076 KB Output is correct
9 Correct 4 ms 9076 KB Output is correct
10 Correct 4 ms 9076 KB Output is correct
11 Correct 5 ms 9076 KB Output is correct
12 Correct 5 ms 9076 KB Output is correct
13 Correct 65 ms 9076 KB Output is correct
14 Correct 89 ms 9076 KB Output is correct
15 Correct 93 ms 9356 KB Output is correct
16 Correct 74 ms 9356 KB Output is correct
17 Correct 112 ms 9356 KB Output is correct
18 Correct 90 ms 9444 KB Output is correct
19 Correct 103 ms 9444 KB Output is correct
20 Correct 4 ms 9444 KB Output is correct
21 Correct 5 ms 9444 KB Output is correct
22 Correct 88 ms 9444 KB Output is correct
23 Correct 70 ms 9444 KB Output is correct
24 Correct 95 ms 9444 KB Output is correct
25 Correct 89 ms 9444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9444 KB Output is correct
2 Correct 5 ms 9444 KB Output is correct
3 Correct 88 ms 11868 KB Output is correct
4 Correct 4 ms 11868 KB Output is correct
5 Correct 4 ms 11868 KB Output is correct
6 Correct 4 ms 11868 KB Output is correct
7 Correct 5 ms 11868 KB Output is correct
8 Correct 4 ms 11868 KB Output is correct
9 Correct 4 ms 11868 KB Output is correct
10 Correct 4 ms 11868 KB Output is correct
11 Correct 5 ms 11868 KB Output is correct
12 Correct 4 ms 11868 KB Output is correct
13 Correct 5 ms 11868 KB Output is correct
14 Correct 4 ms 11868 KB Output is correct
15 Correct 4 ms 11868 KB Output is correct
16 Correct 5 ms 11868 KB Output is correct
17 Correct 6 ms 11868 KB Output is correct
18 Correct 5 ms 11868 KB Output is correct
19 Correct 4 ms 11868 KB Output is correct
20 Correct 4 ms 11868 KB Output is correct
21 Correct 4 ms 11868 KB Output is correct
22 Correct 5 ms 11868 KB Output is correct
23 Correct 85 ms 11964 KB Output is correct
24 Correct 112 ms 13176 KB Output is correct
25 Correct 88 ms 13176 KB Output is correct
26 Correct 4 ms 13176 KB Output is correct
27 Correct 5 ms 13176 KB Output is correct
28 Correct 4 ms 13176 KB Output is correct
29 Correct 4 ms 13176 KB Output is correct
30 Correct 101 ms 13176 KB Output is correct
31 Correct 111 ms 15552 KB Output is correct
32 Correct 93 ms 15552 KB Output is correct
33 Correct 82 ms 15552 KB Output is correct
34 Correct 5 ms 15552 KB Output is correct
35 Correct 4 ms 15552 KB Output is correct
36 Correct 4 ms 15552 KB Output is correct
37 Correct 4 ms 15552 KB Output is correct
38 Correct 112 ms 15552 KB Output is correct
39 Correct 90 ms 15552 KB Output is correct
40 Correct 100 ms 15552 KB Output is correct
41 Correct 106 ms 15552 KB Output is correct
42 Correct 4 ms 15552 KB Output is correct
43 Correct 4 ms 15552 KB Output is correct
44 Correct 4 ms 15552 KB Output is correct
45 Correct 4 ms 15552 KB Output is correct
46 Correct 103 ms 15552 KB Output is correct
47 Correct 96 ms 15552 KB Output is correct
48 Correct 108 ms 15552 KB Output is correct
49 Correct 70 ms 15552 KB Output is correct
50 Correct 4 ms 15552 KB Output is correct
51 Correct 4 ms 15552 KB Output is correct
52 Correct 4 ms 15552 KB Output is correct
53 Correct 5 ms 15552 KB Output is correct
54 Correct 99 ms 15552 KB Output is correct