답안 #84254

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
84254 2018-11-14T03:22:45 Z IOrtroiii Pipes (BOI13_pipes) C++14
74.0741 / 100
129 ms 15216 KB
#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, cur;

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) {
				ans[id] = cur - sum;
				a[u] -= ans[id];
				a[v] -= ans[id];
			} 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) 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:41: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:43: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:45: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);
             ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 2680 KB Output is correct
2 Correct 4 ms 2836 KB Output is correct
3 Correct 4 ms 2888 KB Output is correct
4 Correct 88 ms 9144 KB Output is correct
5 Correct 4 ms 9144 KB Output is correct
6 Correct 4 ms 9144 KB Output is correct
7 Correct 4 ms 9144 KB Output is correct
8 Correct 4 ms 9144 KB Output is correct
9 Correct 5 ms 9144 KB Output is correct
10 Correct 5 ms 9144 KB Output is correct
11 Correct 5 ms 9144 KB Output is correct
12 Correct 5 ms 9144 KB Output is correct
13 Correct 66 ms 9144 KB Output is correct
14 Correct 73 ms 9144 KB Output is correct
15 Correct 79 ms 9328 KB Output is correct
16 Correct 65 ms 9328 KB Output is correct
17 Correct 90 ms 9384 KB Output is correct
18 Correct 89 ms 9384 KB Output is correct
19 Correct 91 ms 9384 KB Output is correct
20 Correct 4 ms 9384 KB Output is correct
21 Correct 5 ms 9384 KB Output is correct
22 Correct 129 ms 9584 KB Output is correct
23 Correct 67 ms 9584 KB Output is correct
24 Correct 129 ms 9584 KB Output is correct
25 Correct 66 ms 9584 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 4 ms 9584 KB Output isn't correct
2 Incorrect 4 ms 9584 KB Output isn't correct
3 Correct 86 ms 12000 KB Output is correct
4 Correct 4 ms 12000 KB Output is correct
5 Correct 4 ms 12000 KB Output is correct
6 Correct 4 ms 12000 KB Output is correct
7 Incorrect 4 ms 12000 KB Output isn't correct
8 Incorrect 4 ms 12000 KB Output isn't correct
9 Correct 4 ms 12000 KB Output is correct
10 Correct 4 ms 12000 KB Output is correct
11 Correct 4 ms 12000 KB Output is correct
12 Correct 4 ms 12000 KB Output is correct
13 Correct 4 ms 12000 KB Output is correct
14 Incorrect 5 ms 12000 KB Output isn't correct
15 Incorrect 6 ms 12000 KB Output isn't correct
16 Incorrect 5 ms 12000 KB Output isn't correct
17 Correct 5 ms 12000 KB Output is correct
18 Correct 4 ms 12000 KB Output is correct
19 Correct 4 ms 12000 KB Output is correct
20 Correct 4 ms 12000 KB Output is correct
21 Correct 4 ms 12000 KB Output is correct
22 Incorrect 5 ms 12000 KB Output isn't correct
23 Incorrect 86 ms 12000 KB Output isn't correct
24 Incorrect 118 ms 12712 KB Output isn't correct
25 Correct 71 ms 12712 KB Output is correct
26 Correct 4 ms 12712 KB Output is correct
27 Correct 4 ms 12712 KB Output is correct
28 Correct 4 ms 12712 KB Output is correct
29 Correct 4 ms 12712 KB Output is correct
30 Incorrect 84 ms 12712 KB Output isn't correct
31 Incorrect 80 ms 15152 KB Output isn't correct
32 Incorrect 72 ms 15152 KB Output isn't correct
33 Correct 113 ms 15152 KB Output is correct
34 Correct 4 ms 15152 KB Output is correct
35 Correct 4 ms 15152 KB Output is correct
36 Correct 4 ms 15152 KB Output is correct
37 Correct 4 ms 15152 KB Output is correct
38 Incorrect 89 ms 15152 KB Output isn't correct
39 Incorrect 100 ms 15152 KB Output isn't correct
40 Incorrect 76 ms 15152 KB Output isn't correct
41 Correct 82 ms 15216 KB Output is correct
42 Correct 4 ms 15216 KB Output is correct
43 Correct 4 ms 15216 KB Output is correct
44 Correct 4 ms 15216 KB Output is correct
45 Correct 4 ms 15216 KB Output is correct
46 Incorrect 77 ms 15216 KB Output isn't correct
47 Incorrect 72 ms 15216 KB Output isn't correct
48 Incorrect 91 ms 15216 KB Output isn't correct
49 Correct 69 ms 15216 KB Output is correct
50 Correct 4 ms 15216 KB Output is correct
51 Correct 5 ms 15216 KB Output is correct
52 Correct 4 ms 15216 KB Output is correct
53 Correct 4 ms 15216 KB Output is correct
54 Incorrect 74 ms 15216 KB Output isn't correct