답안 #544752

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
544752 2022-04-02T16:27:10 Z rainboy Graph (BOI20_graph) C
100 / 100
172 ms 26416 KB
#include <stdio.h>
#include <stdlib.h>

#define N	100000
#define M	200000

unsigned int X = 12345;

int rand_() {
	return (X *= 3) >> 1;
}

int ii[M], jj[M], ww[M];
int *eh[N], eo[N];

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

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

int ff[N], cc[N], aa[N], bb[N], cnt;

void dfs2(int i, int a);

int dfs1(int f, int i, int c, int a) {
	int o;

	if (cc[i]) {
		if (cc[i] != c) {
			int j;

			j = i ^ ii[f] ^ jj[f], a = 0;
			while (j != i)
				a = ww[ff[j]] - a, j ^= ii[ff[j]] ^ jj[ff[j]];
			a = ww[f] - a, j ^= ii[f] ^ jj[f];
			dfs2(j, a / 2);
			return 1;
		}
		return 0;
	}
	ff[i] = f, cc[i] = c, aa[i] = a;
	bb[cnt++] = c == 1 ? -a : a;
	for (o = eo[i]; o--; ) {
		int h = eh[i][o], j = i ^ ii[h] ^ jj[h];

		if (dfs1(h, j, c ^ 3, ww[h] - a))
			return 1;
	}
	return 0;
}

void dfs2(int i, int a) {
	int o;

	if (cc[i] == -1)
		return;
	cc[i] = -1, aa[i] = a;
	for (o = eo[i]; o--; ) {
		int h = eh[i][o], j = i ^ ii[h] ^ jj[h];

		dfs2(j, ww[h] - a);
	}
}

int median(int *aa, int n) {
	int l = 0, r = n, m = (n - 1) / 2;

	while (1) {
		int i = l, j = l, k = r, a = aa[l + rand_() % (r - l)], tmp;

		while (j < k)
			if (aa[j] == a)
				j++;
			else if (aa[j] < a) {
				tmp = aa[i], aa[i] = aa[j], aa[j] = tmp;
				i++, j++;
			} else {
				k--;
				tmp = aa[j], aa[j] = aa[k], aa[k] = tmp;
			}
		if (m < i)
			r = i;
		else if (m >= k)
			l = k;
		else
			return a;
	}
}

int main() {
	int n, m, h, i, j;

	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++)
		eh[i] = (int *) malloc(2 * sizeof *eh[i]);
	for (h = 0; h < m; h++) {
		scanf("%d%d%d", &i, &j, &ww[h]), i--, j--, ww[h] *= 2;
		ii[h] = i, jj[h] = j;
		append(i, h), append(j, h);
	}
	for (i = 0; i < n; i++)
		if (!cc[i]) {
			cnt = 0;
			if (!dfs1(-1, i, 1, 0))
				dfs2(i, median(bb, cnt));
		}
	for (h = 0; h < m; h++)
		if (aa[ii[h]] + aa[jj[h]] != ww[h]) {
			printf("NO\n");
			return 0;
		}
	printf("YES\n");
	for (i = 0; i < n; i++)
		if (aa[i] > 0)
			printf("%d.%d ", aa[i] / 2, aa[i] % 2 == 0 ? 0 : 5);
		else if (aa[i] < 0)
			printf("-%d.%d ", -aa[i] / 2, -aa[i] % 2 == 0 ? 0 : 5);
		else
			printf("0.0 ");
	printf("\n");
	return 0;
}

Compilation message

Graph.c: In function 'append':
Graph.c:19:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   19 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
Graph.c: In function 'main':
Graph.c:96:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |  scanf("%d%d", &n, &m);
      |  ^~~~~~~~~~~~~~~~~~~~~
Graph.c:100:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
  100 |   scanf("%d%d%d", &i, &j, &ww[h]), i--, j--, ww[h] *= 2;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 412 KB answer = YES
2 Correct 1 ms 300 KB answer = YES
3 Correct 1 ms 340 KB answer = YES
4 Correct 1 ms 304 KB answer = NO
5 Correct 1 ms 212 KB answer = YES
6 Correct 1 ms 304 KB answer = YES
7 Correct 0 ms 340 KB answer = YES
8 Correct 0 ms 212 KB answer = YES
9 Correct 1 ms 296 KB answer = NO
10 Correct 0 ms 340 KB answer = YES
11 Correct 1 ms 300 KB answer = YES
12 Correct 1 ms 360 KB answer = NO
13 Correct 0 ms 340 KB answer = YES
14 Correct 1 ms 304 KB answer = YES
15 Correct 1 ms 340 KB answer = YES
16 Correct 0 ms 340 KB answer = YES
17 Correct 0 ms 212 KB answer = YES
18 Correct 1 ms 296 KB answer = YES
19 Correct 1 ms 212 KB answer = YES
20 Correct 1 ms 340 KB answer = YES
21 Correct 0 ms 340 KB answer = YES
22 Correct 0 ms 296 KB answer = NO
23 Correct 0 ms 340 KB answer = NO
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 412 KB answer = YES
2 Correct 1 ms 300 KB answer = YES
3 Correct 1 ms 340 KB answer = YES
4 Correct 1 ms 304 KB answer = NO
5 Correct 1 ms 212 KB answer = YES
6 Correct 1 ms 304 KB answer = YES
7 Correct 0 ms 340 KB answer = YES
8 Correct 0 ms 212 KB answer = YES
9 Correct 1 ms 296 KB answer = NO
10 Correct 0 ms 340 KB answer = YES
11 Correct 1 ms 300 KB answer = YES
12 Correct 1 ms 360 KB answer = NO
13 Correct 0 ms 340 KB answer = YES
14 Correct 1 ms 304 KB answer = YES
15 Correct 1 ms 340 KB answer = YES
16 Correct 0 ms 340 KB answer = YES
17 Correct 0 ms 212 KB answer = YES
18 Correct 1 ms 296 KB answer = YES
19 Correct 1 ms 212 KB answer = YES
20 Correct 1 ms 340 KB answer = YES
21 Correct 0 ms 340 KB answer = YES
22 Correct 0 ms 296 KB answer = NO
23 Correct 0 ms 340 KB answer = NO
24 Correct 1 ms 340 KB answer = YES
25 Correct 1 ms 340 KB answer = YES
26 Correct 1 ms 296 KB answer = YES
27 Correct 0 ms 340 KB answer = YES
28 Correct 1 ms 340 KB answer = YES
29 Correct 1 ms 340 KB answer = YES
30 Correct 1 ms 340 KB answer = NO
31 Correct 1 ms 340 KB answer = YES
32 Correct 1 ms 340 KB answer = YES
33 Correct 1 ms 300 KB answer = YES
34 Correct 1 ms 340 KB answer = YES
35 Correct 1 ms 340 KB answer = YES
36 Correct 1 ms 340 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 412 KB answer = YES
2 Correct 1 ms 300 KB answer = YES
3 Correct 1 ms 340 KB answer = YES
4 Correct 1 ms 304 KB answer = NO
5 Correct 1 ms 212 KB answer = YES
6 Correct 1 ms 304 KB answer = YES
7 Correct 0 ms 340 KB answer = YES
8 Correct 0 ms 212 KB answer = YES
9 Correct 1 ms 296 KB answer = NO
10 Correct 0 ms 340 KB answer = YES
11 Correct 1 ms 300 KB answer = YES
12 Correct 1 ms 360 KB answer = NO
13 Correct 0 ms 340 KB answer = YES
14 Correct 1 ms 304 KB answer = YES
15 Correct 1 ms 340 KB answer = YES
16 Correct 0 ms 340 KB answer = YES
17 Correct 0 ms 212 KB answer = YES
18 Correct 1 ms 296 KB answer = YES
19 Correct 1 ms 212 KB answer = YES
20 Correct 1 ms 340 KB answer = YES
21 Correct 0 ms 340 KB answer = YES
22 Correct 0 ms 296 KB answer = NO
23 Correct 0 ms 340 KB answer = NO
24 Correct 1 ms 340 KB answer = YES
25 Correct 1 ms 340 KB answer = YES
26 Correct 1 ms 296 KB answer = YES
27 Correct 0 ms 340 KB answer = YES
28 Correct 1 ms 340 KB answer = YES
29 Correct 1 ms 340 KB answer = YES
30 Correct 1 ms 340 KB answer = NO
31 Correct 1 ms 340 KB answer = YES
32 Correct 1 ms 340 KB answer = YES
33 Correct 1 ms 300 KB answer = YES
34 Correct 1 ms 340 KB answer = YES
35 Correct 1 ms 340 KB answer = YES
36 Correct 1 ms 340 KB answer = YES
37 Correct 1 ms 300 KB answer = YES
38 Correct 1 ms 296 KB answer = YES
39 Correct 1 ms 300 KB answer = YES
40 Correct 1 ms 340 KB answer = YES
41 Correct 1 ms 340 KB answer = NO
42 Correct 1 ms 340 KB answer = YES
43 Correct 1 ms 340 KB answer = YES
44 Correct 1 ms 308 KB answer = YES
45 Correct 1 ms 436 KB answer = YES
46 Correct 1 ms 340 KB answer = YES
47 Correct 1 ms 340 KB answer = YES
48 Correct 1 ms 340 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 412 KB answer = YES
2 Correct 1 ms 300 KB answer = YES
3 Correct 1 ms 340 KB answer = YES
4 Correct 1 ms 304 KB answer = NO
5 Correct 1 ms 212 KB answer = YES
6 Correct 1 ms 304 KB answer = YES
7 Correct 0 ms 340 KB answer = YES
8 Correct 0 ms 212 KB answer = YES
9 Correct 1 ms 296 KB answer = NO
10 Correct 0 ms 340 KB answer = YES
11 Correct 1 ms 300 KB answer = YES
12 Correct 1 ms 360 KB answer = NO
13 Correct 0 ms 340 KB answer = YES
14 Correct 1 ms 304 KB answer = YES
15 Correct 1 ms 340 KB answer = YES
16 Correct 0 ms 340 KB answer = YES
17 Correct 0 ms 212 KB answer = YES
18 Correct 1 ms 296 KB answer = YES
19 Correct 1 ms 212 KB answer = YES
20 Correct 1 ms 340 KB answer = YES
21 Correct 0 ms 340 KB answer = YES
22 Correct 0 ms 296 KB answer = NO
23 Correct 0 ms 340 KB answer = NO
24 Correct 1 ms 340 KB answer = YES
25 Correct 1 ms 340 KB answer = YES
26 Correct 1 ms 296 KB answer = YES
27 Correct 0 ms 340 KB answer = YES
28 Correct 1 ms 340 KB answer = YES
29 Correct 1 ms 340 KB answer = YES
30 Correct 1 ms 340 KB answer = NO
31 Correct 1 ms 340 KB answer = YES
32 Correct 1 ms 340 KB answer = YES
33 Correct 1 ms 300 KB answer = YES
34 Correct 1 ms 340 KB answer = YES
35 Correct 1 ms 340 KB answer = YES
36 Correct 1 ms 340 KB answer = YES
37 Correct 1 ms 300 KB answer = YES
38 Correct 1 ms 296 KB answer = YES
39 Correct 1 ms 300 KB answer = YES
40 Correct 1 ms 340 KB answer = YES
41 Correct 1 ms 340 KB answer = NO
42 Correct 1 ms 340 KB answer = YES
43 Correct 1 ms 340 KB answer = YES
44 Correct 1 ms 308 KB answer = YES
45 Correct 1 ms 436 KB answer = YES
46 Correct 1 ms 340 KB answer = YES
47 Correct 1 ms 340 KB answer = YES
48 Correct 1 ms 340 KB answer = YES
49 Correct 8 ms 1148 KB answer = YES
50 Correct 6 ms 1516 KB answer = YES
51 Correct 7 ms 1620 KB answer = YES
52 Correct 6 ms 1612 KB answer = NO
53 Correct 1 ms 340 KB answer = YES
54 Correct 2 ms 544 KB answer = YES
55 Correct 4 ms 696 KB answer = YES
56 Correct 8 ms 1108 KB answer = YES
57 Correct 9 ms 1108 KB answer = YES
58 Correct 8 ms 1080 KB answer = YES
59 Correct 7 ms 980 KB answer = YES
60 Correct 6 ms 1208 KB answer = YES
61 Correct 4 ms 724 KB answer = YES
62 Correct 72 ms 8116 KB answer = NO
63 Correct 91 ms 8180 KB answer = YES
64 Correct 96 ms 8176 KB answer = NO
65 Correct 85 ms 8224 KB answer = YES
66 Correct 2 ms 440 KB answer = YES
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 412 KB answer = YES
2 Correct 1 ms 300 KB answer = YES
3 Correct 1 ms 340 KB answer = YES
4 Correct 1 ms 304 KB answer = NO
5 Correct 1 ms 212 KB answer = YES
6 Correct 1 ms 304 KB answer = YES
7 Correct 0 ms 340 KB answer = YES
8 Correct 0 ms 212 KB answer = YES
9 Correct 1 ms 296 KB answer = NO
10 Correct 0 ms 340 KB answer = YES
11 Correct 1 ms 300 KB answer = YES
12 Correct 1 ms 360 KB answer = NO
13 Correct 0 ms 340 KB answer = YES
14 Correct 1 ms 304 KB answer = YES
15 Correct 1 ms 340 KB answer = YES
16 Correct 0 ms 340 KB answer = YES
17 Correct 0 ms 212 KB answer = YES
18 Correct 1 ms 296 KB answer = YES
19 Correct 1 ms 212 KB answer = YES
20 Correct 1 ms 340 KB answer = YES
21 Correct 0 ms 340 KB answer = YES
22 Correct 0 ms 296 KB answer = NO
23 Correct 0 ms 340 KB answer = NO
24 Correct 1 ms 340 KB answer = YES
25 Correct 1 ms 340 KB answer = YES
26 Correct 1 ms 296 KB answer = YES
27 Correct 0 ms 340 KB answer = YES
28 Correct 1 ms 340 KB answer = YES
29 Correct 1 ms 340 KB answer = YES
30 Correct 1 ms 340 KB answer = NO
31 Correct 1 ms 340 KB answer = YES
32 Correct 1 ms 340 KB answer = YES
33 Correct 1 ms 300 KB answer = YES
34 Correct 1 ms 340 KB answer = YES
35 Correct 1 ms 340 KB answer = YES
36 Correct 1 ms 340 KB answer = YES
37 Correct 1 ms 300 KB answer = YES
38 Correct 1 ms 296 KB answer = YES
39 Correct 1 ms 300 KB answer = YES
40 Correct 1 ms 340 KB answer = YES
41 Correct 1 ms 340 KB answer = NO
42 Correct 1 ms 340 KB answer = YES
43 Correct 1 ms 340 KB answer = YES
44 Correct 1 ms 308 KB answer = YES
45 Correct 1 ms 436 KB answer = YES
46 Correct 1 ms 340 KB answer = YES
47 Correct 1 ms 340 KB answer = YES
48 Correct 1 ms 340 KB answer = YES
49 Correct 8 ms 1148 KB answer = YES
50 Correct 6 ms 1516 KB answer = YES
51 Correct 7 ms 1620 KB answer = YES
52 Correct 6 ms 1612 KB answer = NO
53 Correct 1 ms 340 KB answer = YES
54 Correct 2 ms 544 KB answer = YES
55 Correct 4 ms 696 KB answer = YES
56 Correct 8 ms 1108 KB answer = YES
57 Correct 9 ms 1108 KB answer = YES
58 Correct 8 ms 1080 KB answer = YES
59 Correct 7 ms 980 KB answer = YES
60 Correct 6 ms 1208 KB answer = YES
61 Correct 4 ms 724 KB answer = YES
62 Correct 72 ms 8116 KB answer = NO
63 Correct 91 ms 8180 KB answer = YES
64 Correct 96 ms 8176 KB answer = NO
65 Correct 85 ms 8224 KB answer = YES
66 Correct 2 ms 440 KB answer = YES
67 Correct 55 ms 18524 KB answer = YES
68 Correct 63 ms 18396 KB answer = YES
69 Correct 59 ms 26188 KB answer = YES
70 Correct 97 ms 23236 KB answer = YES
71 Correct 60 ms 26216 KB answer = YES
72 Correct 111 ms 9396 KB answer = YES
73 Correct 92 ms 9120 KB answer = YES
74 Correct 59 ms 11276 KB answer = YES
75 Correct 50 ms 10480 KB answer = NO
76 Correct 9 ms 1296 KB answer = YES
77 Correct 18 ms 2568 KB answer = YES
78 Correct 34 ms 4164 KB answer = YES
79 Correct 97 ms 7976 KB answer = YES
80 Correct 75 ms 10840 KB answer = YES
81 Correct 99 ms 21916 KB answer = NO
82 Correct 100 ms 17008 KB answer = YES
83 Correct 143 ms 26416 KB answer = YES
84 Correct 133 ms 18572 KB answer = YES
85 Correct 70 ms 18496 KB answer = YES
86 Correct 69 ms 26308 KB answer = YES
87 Correct 119 ms 14096 KB answer = NO
88 Correct 131 ms 16100 KB answer = YES
89 Correct 78 ms 8672 KB answer = YES
90 Correct 88 ms 8692 KB answer = YES
91 Correct 97 ms 8624 KB answer = YES
92 Correct 36 ms 4676 KB answer = YES
93 Correct 40 ms 4680 KB answer = YES
94 Correct 120 ms 18056 KB answer = NO
95 Correct 70 ms 8284 KB answer = NO
96 Correct 172 ms 17272 KB answer = YES
97 Correct 44 ms 18052 KB answer = NO