#include <stdio.h>
#include <stdlib.h>
#define N 100000
#define M 500000
int ij[M]; char used[M];
int *eh[N], eo[N], n, m, k;
int ii_[N], n_;
void append(int i, int h) {
int o = eo[i]++;
if (o == 0) {
eh[i] = (int *) malloc(2 * sizeof *eh[i]);
ii_[n_++] = i;
} else if (o >= 2 && (o & o - 1) == 0)
eh[i] = (int *) realloc(eh[i], o * 2 * sizeof *eh[i]);
eh[i][o] = h;
}
int hh[M], cnt;
void dfs(int i) {
while (eo[i]) {
int h = eh[i][--eo[i]], j = i ^ ij[h];
if (!used[h])
used[h] = 1, dfs(j), hh[cnt++] = h;
}
}
void solve(int *ii, int m) {
static char odd[N];
int g, h, h_, i, i_, j;
if (m == 1)
return;
m /= 2;
n_ = 0;
for (h = 0; h < m; h++)
for (g = 0; g < k; g++) {
h_ = h * k + g, i = ii[h_], j = ii[m * k + h_];
ij[h_] = i ^ j, used[h_] = 0;
append(i, h_), append(j, h_);
}
for (i = 0; i < n_; i++) {
i_ = ii_[i];
if (eo[i_] % 2 != 0)
odd[i_] = 1;
}
for (i = 0; i < n_; i++) {
i_ = ii_[i];
if (odd[i_]) {
odd[i_] = 0;
cnt = 0, dfs(i_);
while (cnt--) {
h_ = hh[cnt], h = h_ / k, g = h_ % k;
ii[h_] = i_, ii[m * k + h_] = i_ ^= ij[h_];
}
odd[i_] = 0;
}
}
for (i = 0; i < n_; i++) {
i_ = ii_[i];
cnt = 0, dfs(i_);
while (cnt--) {
h_ = hh[cnt], h = h_ / k, g = h_ % k;
ii[h_] = i_, ii[m * k + h_] = i_ ^= ij[h_];
}
}
for (i = 0; i < n_; i++) {
i_ = ii_[i];
free(eh[i_]), eh[i_] = NULL, eo[i_] = 0;
}
solve(ii, m), solve(ii + m * k, m);
}
int main() {
static int ii[M];
int g, h;
scanf("%d%d%d", &k, &m, &n);
for (g = 0; g < k; g++)
for (h = 0; h < m; h++)
scanf("%d", &ii[h * k + g]), ii[h * k + g]--;
solve(ii, m);
for (g = 0; g < k; g++) {
for (h = 0; h < m; h++)
printf("%d ", ii[h * k + g] + 1);
printf("\n");
}
return 0;
}
Compilation message
balance.c: In function 'append':
balance.c:17:30: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
17 | } else if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
balance.c: In function 'main':
balance.c:83:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | scanf("%d%d%d", &k, &m, &n);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
balance.c:86:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
86 | scanf("%d", &ii[h * k + g]), ii[h * k + g]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Correct |
2 |
Correct |
0 ms |
348 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Correct |
2 |
Incorrect |
0 ms |
348 KB |
Integer 30 violates the range [1, 20] |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
13140 KB |
Correct |
2 |
Correct |
34 ms |
12964 KB |
Correct |
3 |
Correct |
31 ms |
10332 KB |
Correct |
4 |
Correct |
43 ms |
14252 KB |
Correct |
5 |
Correct |
57 ms |
12112 KB |
Correct |
6 |
Correct |
37 ms |
13648 KB |
Correct |
7 |
Correct |
32 ms |
7508 KB |
Correct |
8 |
Correct |
60 ms |
14160 KB |
Correct |
9 |
Correct |
33 ms |
14156 KB |
Correct |
10 |
Correct |
27 ms |
14932 KB |
Correct |
11 |
Correct |
27 ms |
14940 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
13140 KB |
Correct |
2 |
Correct |
34 ms |
12964 KB |
Correct |
3 |
Correct |
31 ms |
10332 KB |
Correct |
4 |
Correct |
43 ms |
14252 KB |
Correct |
5 |
Correct |
57 ms |
12112 KB |
Correct |
6 |
Correct |
37 ms |
13648 KB |
Correct |
7 |
Correct |
32 ms |
7508 KB |
Correct |
8 |
Correct |
60 ms |
14160 KB |
Correct |
9 |
Correct |
33 ms |
14156 KB |
Correct |
10 |
Correct |
27 ms |
14932 KB |
Correct |
11 |
Correct |
27 ms |
14940 KB |
Correct |
12 |
Correct |
36 ms |
13908 KB |
Correct |
13 |
Correct |
32 ms |
13744 KB |
Correct |
14 |
Correct |
35 ms |
12124 KB |
Correct |
15 |
Correct |
22 ms |
11612 KB |
Correct |
16 |
Correct |
50 ms |
13020 KB |
Correct |
17 |
Correct |
55 ms |
14672 KB |
Correct |
18 |
Correct |
31 ms |
8276 KB |
Correct |
19 |
Correct |
52 ms |
14980 KB |
Correct |
20 |
Correct |
30 ms |
14988 KB |
Correct |
21 |
Correct |
49 ms |
14988 KB |
Correct |
22 |
Correct |
26 ms |
17320 KB |
Correct |
23 |
Runtime error |
32 ms |
24236 KB |
Execution killed with signal 11 |
24 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Correct |
2 |
Incorrect |
0 ms |
348 KB |
Integer 30 violates the range [1, 20] |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Correct |
2 |
Correct |
4 ms |
6748 KB |
Correct |
3 |
Correct |
4 ms |
880 KB |
Correct |
4 |
Correct |
2 ms |
860 KB |
Correct |
5 |
Correct |
2 ms |
7004 KB |
Correct |
6 |
Correct |
2 ms |
7004 KB |
Correct |
7 |
Correct |
3 ms |
7000 KB |
Correct |
8 |
Correct |
2 ms |
7004 KB |
Correct |
9 |
Correct |
2 ms |
604 KB |
Correct |
10 |
Correct |
3 ms |
2908 KB |
Correct |
11 |
Correct |
3 ms |
6748 KB |
Correct |
12 |
Correct |
2 ms |
1116 KB |
Correct |
13 |
Correct |
2 ms |
860 KB |
Correct |
14 |
Correct |
3 ms |
1116 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Correct |
2 |
Correct |
4 ms |
6748 KB |
Correct |
3 |
Correct |
4 ms |
880 KB |
Correct |
4 |
Correct |
2 ms |
860 KB |
Correct |
5 |
Correct |
2 ms |
7004 KB |
Correct |
6 |
Correct |
2 ms |
7004 KB |
Correct |
7 |
Correct |
3 ms |
7000 KB |
Correct |
8 |
Correct |
2 ms |
7004 KB |
Correct |
9 |
Correct |
2 ms |
604 KB |
Correct |
10 |
Correct |
3 ms |
2908 KB |
Correct |
11 |
Correct |
3 ms |
6748 KB |
Correct |
12 |
Correct |
2 ms |
1116 KB |
Correct |
13 |
Correct |
2 ms |
860 KB |
Correct |
14 |
Correct |
3 ms |
1116 KB |
Correct |
15 |
Correct |
1 ms |
6492 KB |
Correct |
16 |
Correct |
2 ms |
860 KB |
Correct |
17 |
Correct |
3 ms |
896 KB |
Correct |
18 |
Correct |
2 ms |
6748 KB |
Correct |
19 |
Correct |
2 ms |
7004 KB |
Correct |
20 |
Correct |
2 ms |
7004 KB |
Correct |
21 |
Correct |
2 ms |
856 KB |
Correct |
22 |
Correct |
2 ms |
7004 KB |
Correct |
23 |
Correct |
2 ms |
6748 KB |
Correct |
24 |
Correct |
2 ms |
712 KB |
Correct |
25 |
Correct |
2 ms |
2652 KB |
Correct |
26 |
Correct |
2 ms |
7004 KB |
Correct |
27 |
Correct |
1 ms |
860 KB |
Correct |
28 |
Correct |
2 ms |
1116 KB |
Correct |
29 |
Incorrect |
3 ms |
7004 KB |
Integer 60 violates the range [1, 50] |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Correct |
2 |
Correct |
4 ms |
6748 KB |
Correct |
3 |
Correct |
4 ms |
880 KB |
Correct |
4 |
Correct |
2 ms |
860 KB |
Correct |
5 |
Correct |
2 ms |
7004 KB |
Correct |
6 |
Correct |
2 ms |
7004 KB |
Correct |
7 |
Correct |
3 ms |
7000 KB |
Correct |
8 |
Correct |
2 ms |
7004 KB |
Correct |
9 |
Correct |
2 ms |
604 KB |
Correct |
10 |
Correct |
3 ms |
2908 KB |
Correct |
11 |
Correct |
3 ms |
6748 KB |
Correct |
12 |
Correct |
2 ms |
1116 KB |
Correct |
13 |
Correct |
2 ms |
860 KB |
Correct |
14 |
Correct |
3 ms |
1116 KB |
Correct |
15 |
Correct |
1 ms |
6492 KB |
Correct |
16 |
Correct |
2 ms |
860 KB |
Correct |
17 |
Correct |
3 ms |
896 KB |
Correct |
18 |
Correct |
2 ms |
6748 KB |
Correct |
19 |
Correct |
2 ms |
7004 KB |
Correct |
20 |
Correct |
2 ms |
7004 KB |
Correct |
21 |
Correct |
2 ms |
856 KB |
Correct |
22 |
Correct |
2 ms |
7004 KB |
Correct |
23 |
Correct |
2 ms |
6748 KB |
Correct |
24 |
Correct |
2 ms |
712 KB |
Correct |
25 |
Correct |
2 ms |
2652 KB |
Correct |
26 |
Correct |
2 ms |
7004 KB |
Correct |
27 |
Correct |
1 ms |
860 KB |
Correct |
28 |
Correct |
2 ms |
1116 KB |
Correct |
29 |
Incorrect |
3 ms |
7004 KB |
Integer 60 violates the range [1, 50] |
30 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
13140 KB |
Correct |
2 |
Correct |
34 ms |
12964 KB |
Correct |
3 |
Correct |
31 ms |
10332 KB |
Correct |
4 |
Correct |
43 ms |
14252 KB |
Correct |
5 |
Correct |
57 ms |
12112 KB |
Correct |
6 |
Correct |
37 ms |
13648 KB |
Correct |
7 |
Correct |
32 ms |
7508 KB |
Correct |
8 |
Correct |
60 ms |
14160 KB |
Correct |
9 |
Correct |
33 ms |
14156 KB |
Correct |
10 |
Correct |
27 ms |
14932 KB |
Correct |
11 |
Correct |
27 ms |
14940 KB |
Correct |
12 |
Correct |
1 ms |
6492 KB |
Correct |
13 |
Correct |
4 ms |
6748 KB |
Correct |
14 |
Correct |
4 ms |
880 KB |
Correct |
15 |
Correct |
2 ms |
860 KB |
Correct |
16 |
Correct |
2 ms |
7004 KB |
Correct |
17 |
Correct |
2 ms |
7004 KB |
Correct |
18 |
Correct |
3 ms |
7000 KB |
Correct |
19 |
Correct |
2 ms |
7004 KB |
Correct |
20 |
Correct |
2 ms |
604 KB |
Correct |
21 |
Correct |
3 ms |
2908 KB |
Correct |
22 |
Correct |
3 ms |
6748 KB |
Correct |
23 |
Correct |
2 ms |
1116 KB |
Correct |
24 |
Correct |
2 ms |
860 KB |
Correct |
25 |
Correct |
3 ms |
1116 KB |
Correct |
26 |
Correct |
41 ms |
13092 KB |
Correct |
27 |
Correct |
39 ms |
16512 KB |
Correct |
28 |
Correct |
34 ms |
14956 KB |
Correct |
29 |
Correct |
37 ms |
14428 KB |
Correct |
30 |
Correct |
52 ms |
13260 KB |
Correct |
31 |
Correct |
42 ms |
17232 KB |
Correct |
32 |
Correct |
48 ms |
8296 KB |
Correct |
33 |
Correct |
30 ms |
17488 KB |
Correct |
34 |
Correct |
32 ms |
17324 KB |
Correct |
35 |
Correct |
29 ms |
17588 KB |
Correct |
36 |
Correct |
28 ms |
14172 KB |
Correct |
37 |
Correct |
0 ms |
348 KB |
Correct |
38 |
Correct |
4 ms |
860 KB |
Correct |
39 |
Correct |
3 ms |
6944 KB |
Correct |
40 |
Correct |
2 ms |
6952 KB |
Correct |
41 |
Correct |
1 ms |
7004 KB |
Correct |
42 |
Correct |
2 ms |
860 KB |
Correct |
43 |
Correct |
2 ms |
860 KB |
Correct |
44 |
Correct |
3 ms |
860 KB |
Correct |
45 |
Correct |
2 ms |
2704 KB |
Correct |
46 |
Correct |
2 ms |
2908 KB |
Correct |
47 |
Correct |
1 ms |
604 KB |
Correct |
48 |
Correct |
2 ms |
1116 KB |
Correct |
49 |
Correct |
3 ms |
7004 KB |
Correct |
50 |
Correct |
3 ms |
7332 KB |
Correct |
51 |
Correct |
173 ms |
26948 KB |
Correct |
52 |
Correct |
163 ms |
26072 KB |
Correct |
53 |
Correct |
38 ms |
4952 KB |
Correct |
54 |
Correct |
51 ms |
14576 KB |
Correct |
55 |
Correct |
134 ms |
20324 KB |
Correct |
56 |
Correct |
159 ms |
25940 KB |
Correct |
57 |
Correct |
138 ms |
30264 KB |
Correct |
58 |
Correct |
145 ms |
25048 KB |
Correct |
59 |
Correct |
139 ms |
18308 KB |
Correct |
60 |
Correct |
119 ms |
23788 KB |
Correct |
61 |
Correct |
163 ms |
26040 KB |
Correct |
62 |
Correct |
97 ms |
17776 KB |
Correct |
63 |
Correct |
85 ms |
17024 KB |
Correct |
64 |
Correct |
95 ms |
23408 KB |
Correct |
65 |
Correct |
62 ms |
25460 KB |
Correct |
66 |
Correct |
64 ms |
17928 KB |
Correct |
67 |
Correct |
58 ms |
16980 KB |
Correct |
68 |
Correct |
59 ms |
25540 KB |
Correct |
69 |
Correct |
57 ms |
25552 KB |
Correct |
70 |
Correct |
103 ms |
18708 KB |
Correct |
71 |
Correct |
84 ms |
18220 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
13140 KB |
Correct |
2 |
Correct |
34 ms |
12964 KB |
Correct |
3 |
Correct |
31 ms |
10332 KB |
Correct |
4 |
Correct |
43 ms |
14252 KB |
Correct |
5 |
Correct |
57 ms |
12112 KB |
Correct |
6 |
Correct |
37 ms |
13648 KB |
Correct |
7 |
Correct |
32 ms |
7508 KB |
Correct |
8 |
Correct |
60 ms |
14160 KB |
Correct |
9 |
Correct |
33 ms |
14156 KB |
Correct |
10 |
Correct |
27 ms |
14932 KB |
Correct |
11 |
Correct |
27 ms |
14940 KB |
Correct |
12 |
Correct |
36 ms |
13908 KB |
Correct |
13 |
Correct |
32 ms |
13744 KB |
Correct |
14 |
Correct |
35 ms |
12124 KB |
Correct |
15 |
Correct |
22 ms |
11612 KB |
Correct |
16 |
Correct |
50 ms |
13020 KB |
Correct |
17 |
Correct |
55 ms |
14672 KB |
Correct |
18 |
Correct |
31 ms |
8276 KB |
Correct |
19 |
Correct |
52 ms |
14980 KB |
Correct |
20 |
Correct |
30 ms |
14988 KB |
Correct |
21 |
Correct |
49 ms |
14988 KB |
Correct |
22 |
Correct |
26 ms |
17320 KB |
Correct |
23 |
Runtime error |
32 ms |
24236 KB |
Execution killed with signal 11 |
24 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Correct |
2 |
Correct |
0 ms |
348 KB |
Correct |
3 |
Correct |
0 ms |
348 KB |
Correct |
4 |
Incorrect |
0 ms |
348 KB |
Integer 30 violates the range [1, 20] |
5 |
Halted |
0 ms |
0 KB |
- |