답안 #917347

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
917347 2024-01-27T22:38:52 Z rainboy 산만한 고양이 (KOI17_cat) C++17
100 / 100
254 ms 43068 KB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N	300000
#define j1	J1	/* hahaha */

int n, m;
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;
}

int pp[N], dd[N], i1, j1, i2, j2;

void dfs(int p, int i, int d) {
	int o;

	pp[i] = p, dd[i] = d++;
	for (o = eo[i]; o--; ) {
		int j = ej[i][o];

		if (j != p) {
			if (!dd[j])
				dfs(i, j, d + 1);
			else if (dd[i] < dd[j]) {
				if (i1 == -1 && j1 == -1)
					i1 = i, j1 = j;
				else
					i2 = i, j2 = j;
			}
		}
	}
}

int lca(int i, int j) {
	while (i != j)
		if (dd[i] > dd[j])
			i = pp[i];
		else
			j = pp[j];
	return i;
}

int ds[N];

int find(int i) {
	return ds[i] < 0 ? i : (ds[i] = find(ds[i]));
}

int join(int i, int j) {
	i = find(i);
	j = find(j);
	if (i == j)
		return 0;
	if (ds[i] > ds[j])
		ds[i] = j;
	else {
		if (ds[i] == ds[j])
			ds[i]--;
		ds[j] = i;
	}
	return 1;
}

int check(int i_) {
	int i, j, o;

	memset(ds, -1, n * sizeof *ds);
	for (i = 0; i < n; i++)
		if (i != i_)
			for (o = eo[i]; o--; ) {
				j = ej[i][o];
				if (j != i_ && i < j && !join(i, j))
					return 0;
			}
	return 1;
}

int main() {
	int h, i, j;
	long long ans;

	scanf("%d%d", &n, &m);
	for (i = 0; i < n; i++)
		ej[i] = (int *) malloc(2 * sizeof *ej[i]);
	for (h = 0; h < m; h++) {
		scanf("%d%d", &i, &j), i--, j--;
		append(i, j), append(j, i);
	}
	i1 = j1 = i2 = j2 = -1, dfs(-1, 0, 1);
	if (i2 == -1 && j2 == -1) {
		ans = 0;
		for (i = j1; i != i1; i = pp[i])
			ans += i + 1;
		ans += i1 + 1;
	} else {
		i = dd[i1] > dd[i2] ? i1 : i2, j = lca(j1, j2);
		ans = 0;
		if (check(i))
			ans += i + 1;
		if (j != i && check(j))
			ans += j + 1;
	}
	printf("%lld\n", ans);
	return 0;
}

Compilation message

cat.cpp: In function 'void append(int, int)':
cat.cpp:14:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
   14 |  if (o >= 2 && (o & o - 1) == 0)
      |                     ~~^~~
cat.cpp: In function 'int main()':
cat.cpp:89:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |  scanf("%d%d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~
cat.cpp:93:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |   scanf("%d%d", &i, &j), i--, j--;
      |   ~~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4528 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 2 ms 4700 KB Output is correct
6 Correct 2 ms 4700 KB Output is correct
7 Correct 2 ms 4700 KB Output is correct
8 Correct 2 ms 4700 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4804 KB Output is correct
11 Correct 2 ms 4956 KB Output is correct
12 Correct 2 ms 4800 KB Output is correct
13 Correct 2 ms 4700 KB Output is correct
14 Correct 2 ms 4700 KB Output is correct
15 Correct 1 ms 4960 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 3 ms 4956 KB Output is correct
21 Correct 3 ms 4956 KB Output is correct
22 Correct 2 ms 4956 KB Output is correct
23 Correct 2 ms 4700 KB Output is correct
24 Correct 2 ms 4540 KB Output is correct
25 Correct 1 ms 4696 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 118 ms 20280 KB Output is correct
2 Correct 171 ms 20308 KB Output is correct
3 Correct 162 ms 20396 KB Output is correct
4 Correct 126 ms 20372 KB Output is correct
5 Correct 118 ms 20504 KB Output is correct
6 Correct 113 ms 20308 KB Output is correct
7 Correct 120 ms 20400 KB Output is correct
8 Correct 152 ms 20488 KB Output is correct
9 Correct 180 ms 20308 KB Output is correct
10 Correct 114 ms 20280 KB Output is correct
11 Correct 141 ms 20880 KB Output is correct
12 Correct 155 ms 21064 KB Output is correct
13 Correct 112 ms 20820 KB Output is correct
14 Correct 161 ms 20888 KB Output is correct
15 Correct 117 ms 20820 KB Output is correct
16 Correct 115 ms 22980 KB Output is correct
17 Correct 105 ms 25464 KB Output is correct
18 Correct 110 ms 23864 KB Output is correct
19 Correct 111 ms 25940 KB Output is correct
20 Correct 136 ms 24132 KB Output is correct
21 Correct 164 ms 21844 KB Output is correct
22 Correct 138 ms 28220 KB Output is correct
23 Correct 157 ms 30804 KB Output is correct
24 Correct 146 ms 21844 KB Output is correct
25 Correct 149 ms 27112 KB Output is correct
26 Correct 68 ms 43068 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 69 ms 39252 KB Output is correct
2 Correct 89 ms 43068 KB Output is correct
3 Correct 76 ms 42752 KB Output is correct
4 Correct 71 ms 41300 KB Output is correct
5 Correct 68 ms 41044 KB Output is correct
6 Correct 70 ms 35668 KB Output is correct
7 Correct 72 ms 37944 KB Output is correct
8 Correct 66 ms 33988 KB Output is correct
9 Correct 71 ms 38100 KB Output is correct
10 Correct 60 ms 26452 KB Output is correct
11 Correct 72 ms 30836 KB Output is correct
12 Correct 85 ms 33984 KB Output is correct
13 Correct 64 ms 28076 KB Output is correct
14 Correct 73 ms 35668 KB Output is correct
15 Correct 69 ms 24288 KB Output is correct
16 Correct 66 ms 27400 KB Output is correct
17 Correct 80 ms 23808 KB Output is correct
18 Correct 85 ms 24652 KB Output is correct
19 Correct 61 ms 21700 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 4444 KB Output is correct
2 Correct 1 ms 4444 KB Output is correct
3 Correct 1 ms 4528 KB Output is correct
4 Correct 1 ms 4444 KB Output is correct
5 Correct 2 ms 4700 KB Output is correct
6 Correct 2 ms 4700 KB Output is correct
7 Correct 2 ms 4700 KB Output is correct
8 Correct 2 ms 4700 KB Output is correct
9 Correct 2 ms 4956 KB Output is correct
10 Correct 2 ms 4804 KB Output is correct
11 Correct 2 ms 4956 KB Output is correct
12 Correct 2 ms 4800 KB Output is correct
13 Correct 2 ms 4700 KB Output is correct
14 Correct 2 ms 4700 KB Output is correct
15 Correct 1 ms 4960 KB Output is correct
16 Correct 1 ms 4444 KB Output is correct
17 Correct 1 ms 4444 KB Output is correct
18 Correct 1 ms 4444 KB Output is correct
19 Correct 1 ms 4444 KB Output is correct
20 Correct 3 ms 4956 KB Output is correct
21 Correct 3 ms 4956 KB Output is correct
22 Correct 2 ms 4956 KB Output is correct
23 Correct 2 ms 4700 KB Output is correct
24 Correct 2 ms 4540 KB Output is correct
25 Correct 1 ms 4696 KB Output is correct
26 Correct 118 ms 20280 KB Output is correct
27 Correct 171 ms 20308 KB Output is correct
28 Correct 162 ms 20396 KB Output is correct
29 Correct 126 ms 20372 KB Output is correct
30 Correct 118 ms 20504 KB Output is correct
31 Correct 113 ms 20308 KB Output is correct
32 Correct 120 ms 20400 KB Output is correct
33 Correct 152 ms 20488 KB Output is correct
34 Correct 180 ms 20308 KB Output is correct
35 Correct 114 ms 20280 KB Output is correct
36 Correct 141 ms 20880 KB Output is correct
37 Correct 155 ms 21064 KB Output is correct
38 Correct 112 ms 20820 KB Output is correct
39 Correct 161 ms 20888 KB Output is correct
40 Correct 117 ms 20820 KB Output is correct
41 Correct 115 ms 22980 KB Output is correct
42 Correct 105 ms 25464 KB Output is correct
43 Correct 110 ms 23864 KB Output is correct
44 Correct 111 ms 25940 KB Output is correct
45 Correct 136 ms 24132 KB Output is correct
46 Correct 164 ms 21844 KB Output is correct
47 Correct 138 ms 28220 KB Output is correct
48 Correct 157 ms 30804 KB Output is correct
49 Correct 146 ms 21844 KB Output is correct
50 Correct 149 ms 27112 KB Output is correct
51 Correct 68 ms 43068 KB Output is correct
52 Correct 69 ms 39252 KB Output is correct
53 Correct 89 ms 43068 KB Output is correct
54 Correct 76 ms 42752 KB Output is correct
55 Correct 71 ms 41300 KB Output is correct
56 Correct 68 ms 41044 KB Output is correct
57 Correct 70 ms 35668 KB Output is correct
58 Correct 72 ms 37944 KB Output is correct
59 Correct 66 ms 33988 KB Output is correct
60 Correct 71 ms 38100 KB Output is correct
61 Correct 60 ms 26452 KB Output is correct
62 Correct 72 ms 30836 KB Output is correct
63 Correct 85 ms 33984 KB Output is correct
64 Correct 64 ms 28076 KB Output is correct
65 Correct 73 ms 35668 KB Output is correct
66 Correct 69 ms 24288 KB Output is correct
67 Correct 66 ms 27400 KB Output is correct
68 Correct 80 ms 23808 KB Output is correct
69 Correct 85 ms 24652 KB Output is correct
70 Correct 61 ms 21700 KB Output is correct
71 Correct 194 ms 34824 KB Output is correct
72 Correct 229 ms 37180 KB Output is correct
73 Correct 233 ms 38600 KB Output is correct
74 Correct 221 ms 40252 KB Output is correct
75 Correct 196 ms 40372 KB Output is correct
76 Correct 215 ms 41796 KB Output is correct
77 Correct 191 ms 36888 KB Output is correct
78 Correct 165 ms 37716 KB Output is correct
79 Correct 222 ms 42300 KB Output is correct
80 Correct 166 ms 39068 KB Output is correct
81 Correct 183 ms 33620 KB Output is correct
82 Correct 195 ms 41016 KB Output is correct
83 Correct 218 ms 35000 KB Output is correct
84 Correct 254 ms 39992 KB Output is correct
85 Correct 175 ms 36440 KB Output is correct
86 Correct 158 ms 36500 KB Output is correct
87 Correct 218 ms 34452 KB Output is correct
88 Correct 89 ms 21588 KB Output is correct
89 Correct 91 ms 21912 KB Output is correct
90 Correct 127 ms 21844 KB Output is correct