답안 #545884

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
545884 2022-04-05T15:53:01 Z rainboy 007 (CEOI14_007) C
100 / 100
306 ms 23228 KB
#include <stdio.h>
#include <stdlib.h>

#define N	200000

int min(int a, int b) { return a < b ? a : b; }

int *ej[N], eo[N];

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

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

void bfs(int *dd, int n, int s) {
	static int qu[N];
	int i, head, cnt;

	for (i = 0; i < n; i++)
		dd[i] = n;
	head = cnt = 0;
	dd[s] = 0, qu[head + cnt++] = s;
	while (cnt) {
		int d, o;

		i = qu[cnt--, head++], d = dd[i] + 1;
		for (o = eo[i]; o--; ) {
			int j = ej[i][o];

			if (dd[j] > d)
				dd[j] = d, qu[head + cnt++] = j;
		}
	}
}

int dda[N], ddb[N], dd_[N]; char visited[N];

void dfs(int i) {
	int o;

	if (visited[i])
		return;
	dd_[i] = dda[i], visited[i] = 1;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (dda[j] == dda[i] - 1 && ddb[j] == ddb[i] - 1) {
			dfs(j);
			dd_[i] = min(dd_[i], dd_[j]);
		}
	}
}

int main() {
	int n, m, s, t, a, b, i, j, d;

	scanf("%d%d%d%d%d%d", &n, &m, &s, &t, &a, &b), s--, t--, a--, b--;
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	while (m--) {
		scanf("%d%d", &i, &j), i--, j--;
		append(i, j), append(j, i);
	}
	bfs(dda, n, a);
	bfs(ddb, n, b);
	d = dda[s] > dda[t] || ddb[s] > ddb[t] ? -1 : min(dda[t] - dda[s], ddb[t] - ddb[s]);
	if (d != -1 && dda[s] == ddb[s] && dda[t] == ddb[t]) {
		dfs(s), dfs(t);
		if (dd_[s] > dd_[t])
			d--;
	}
	printf("%d\n", d);
	return 0;
}

Compilation message

007.c: In function 'append':
007.c:13:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   13 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
007.c: In function 'main':
007.c:60:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   60 |  scanf("%d%d%d%d%d%d", &n, &m, &s, &t, &a, &b), s--, t--, a--, b--;
      |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
007.c:64:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |   scanf("%d%d", &i, &j), i--, j--;
      |   ^~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 288 KB Output is correct
9 Correct 1 ms 292 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 292 KB Output is correct
13 Correct 1 ms 292 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 1 ms 340 KB Output is correct
19 Correct 1 ms 300 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 300 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 17 ms 3168 KB Output is correct
2 Correct 25 ms 4760 KB Output is correct
3 Correct 22 ms 3300 KB Output is correct
4 Correct 24 ms 4880 KB Output is correct
5 Correct 16 ms 2848 KB Output is correct
6 Correct 19 ms 3244 KB Output is correct
7 Correct 18 ms 3540 KB Output is correct
8 Correct 20 ms 3628 KB Output is correct
9 Correct 31 ms 4608 KB Output is correct
10 Correct 150 ms 13708 KB Output is correct
11 Correct 39 ms 6808 KB Output is correct
12 Correct 55 ms 8500 KB Output is correct
13 Correct 52 ms 7288 KB Output is correct
14 Correct 42 ms 5744 KB Output is correct
15 Correct 64 ms 8720 KB Output is correct
16 Correct 77 ms 8604 KB Output is correct
17 Correct 74 ms 7536 KB Output is correct
18 Correct 69 ms 7788 KB Output is correct
19 Correct 100 ms 10684 KB Output is correct
20 Correct 173 ms 17064 KB Output is correct
21 Correct 99 ms 12568 KB Output is correct
22 Correct 73 ms 10220 KB Output is correct
23 Correct 85 ms 10924 KB Output is correct
24 Correct 79 ms 12028 KB Output is correct
25 Correct 92 ms 11024 KB Output is correct
26 Correct 94 ms 9652 KB Output is correct
27 Correct 115 ms 11000 KB Output is correct
28 Correct 120 ms 11268 KB Output is correct
29 Correct 154 ms 13572 KB Output is correct
30 Correct 248 ms 18324 KB Output is correct
31 Correct 158 ms 14512 KB Output is correct
32 Correct 122 ms 11688 KB Output is correct
33 Correct 131 ms 11288 KB Output is correct
34 Correct 131 ms 12748 KB Output is correct
35 Correct 123 ms 13080 KB Output is correct
36 Correct 119 ms 13568 KB Output is correct
37 Correct 163 ms 13384 KB Output is correct
38 Correct 127 ms 13004 KB Output is correct
39 Correct 137 ms 13192 KB Output is correct
40 Correct 212 ms 17724 KB Output is correct
41 Correct 306 ms 23228 KB Output is correct