답안 #544606

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
544606 2022-04-02T13:32:00 Z rainboy Pipes (BOI13_pipes) C
100 / 100
68 ms 10036 KB
#include <stdio.h>
#include <stdlib.h>

#define N	100000

int *oh[N], oo[N];

void append(int i, int h) {
	int o = oo[i]++;

	if (o >= 2 && (o & o - 1) == 0)
		oh[i] = (int *) realloc(oh[i], o * 2 * sizeof *oh[i]);
	oh[i][o] = h;
}

int ij[N]; long long cc[N]; char used[N];
long long aa[N]; int hh[N], dd[N];

void dfs(int i) {
	if (dd[i] == 1) {
		int h, j;

		h = hh[i], j = i ^ ij[hh[i]];
		cc[h] = aa[i] * 2, used[h] = 1;
		aa[j] -= aa[i];
		hh[i] ^= h, dd[i]--;
		hh[j] ^= h, dd[j]--;
		dfs(j);
	}
}

int main() {
	static int qu[N], qu_[N];
	int n, m, g, h, h_, i, i_, j, cnt;

	scanf("%d%d", &n, &m);
	if (m > n) {
		printf("0\n");
		return 0;
	}
	for (i = 0; i < n; i++)
		oh[i] = (int *) malloc(2 * sizeof *oh[i]);
	for (i = 0; i < n; i++)
		scanf("%lld", &aa[i]);
	for (h = 0; h < m; h++) {
		scanf("%d%d", &i, &j), i--, j--;
		ij[h] = i ^ j;
		append(i, h), append(j, h);
		hh[i] ^= h, hh[j] ^= h;
	}
	for (i = 0; i < n; i++)
		dd[i] = oo[i];
	for (i = 0; i < n; i++)
		dfs(i);
	if (m == n) {
		i = 0;
		while (dd[i] == 0)
			i++;
		i_ = i, h_ = -1, cnt = 0;
		do {
			int o;

			qu[cnt] = i_;
			for (o = 0; o < oo[i_]; o++) {
				h = oh[i_][o];
				if (h != h_ && !used[h]) {
					qu_[cnt] = h_ = h;
					i_ ^= ij[h];
					break;
				}
			}
			cnt++;
		} while (i_ != i);
		h = qu_[cnt - 1];
		for (g = 0; g < cnt; g++) {
			i = qu[g];
			cc[h] += (g % 2 == 0 ? 1 : -1) * aa[i];
		}
		for (g = 0; g < cnt - 1; g++) {
			i = qu[g], h = qu_[(g - 1 + cnt) % cnt], h_ = qu_[g];
			cc[h_] = aa[i] * 2 - cc[h];
		}
		if (cnt % 2 == 0) {
			printf("0\n");
			return 0;
		}
	}
	for (h = 0; h < m; h++)
		printf("%lld\n", cc[h]);
	return 0;
}

Compilation message

pipes.c: In function 'append':
pipes.c:11:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   11 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
pipes.c: In function 'main':
pipes.c:36:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
pipes.c:44:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   44 |   scanf("%lld", &aa[i]);
      |   ^~~~~~~~~~~~~~~~~~~~~
pipes.c:46:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   46 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 292 KB Output is correct
3 Correct 1 ms 304 KB Output is correct
4 Correct 50 ms 9688 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 288 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 304 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 304 KB Output is correct
13 Correct 44 ms 7720 KB Output is correct
14 Correct 46 ms 9152 KB Output is correct
15 Correct 54 ms 9660 KB Output is correct
16 Correct 43 ms 8200 KB Output is correct
17 Correct 51 ms 9684 KB Output is correct
18 Correct 57 ms 9676 KB Output is correct
19 Correct 50 ms 9348 KB Output is correct
20 Correct 0 ms 340 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 55 ms 9616 KB Output is correct
23 Correct 43 ms 7688 KB Output is correct
24 Correct 57 ms 9632 KB Output is correct
25 Correct 50 ms 8072 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 48 ms 8968 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 0 ms 212 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 58 ms 8256 KB Output is correct
24 Correct 59 ms 9888 KB Output is correct
25 Correct 41 ms 8968 KB Output is correct
26 Correct 0 ms 304 KB Output is correct
27 Correct 0 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 308 KB Output is correct
30 Correct 61 ms 9540 KB Output is correct
31 Correct 61 ms 10036 KB Output is correct
32 Correct 61 ms 9804 KB Output is correct
33 Correct 44 ms 9484 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 0 ms 304 KB Output is correct
36 Correct 0 ms 308 KB Output is correct
37 Correct 0 ms 308 KB Output is correct
38 Correct 54 ms 9800 KB Output is correct
39 Correct 54 ms 9724 KB Output is correct
40 Correct 57 ms 9924 KB Output is correct
41 Correct 50 ms 9680 KB Output is correct
42 Correct 1 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 0 ms 340 KB Output is correct
46 Correct 51 ms 9756 KB Output is correct
47 Correct 60 ms 9908 KB Output is correct
48 Correct 68 ms 10032 KB Output is correct
49 Correct 46 ms 8820 KB Output is correct
50 Correct 0 ms 340 KB Output is correct
51 Correct 0 ms 340 KB Output is correct
52 Correct 0 ms 304 KB Output is correct
53 Correct 0 ms 340 KB Output is correct
54 Correct 58 ms 9620 KB Output is correct