#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100000
#define INF 0x3f3f3f3f
int max(int a, int b) { return a > b ? a : b; }
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
char ss[N * 2][16];
void sort(int *hh, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, h = hh[l + rand_() % (r - l)], tmp;
while (j < k) {
int c = strcmp(ss[hh[j]], ss[h]);
if (c == 0)
j++;
else if (c < 0) {
tmp = hh[i], hh[i] = hh[j], hh[j] = tmp;
i++, j++;
} else {
k--;
tmp = hh[j], hh[j] = hh[k], hh[k] = tmp;
}
}
sort(hh, l, i);
l = k;
}
}
int *ej[N], eo[N], dp[N], dq[N]; char visited[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 dfs(int i) {
int o, x, y;
visited[i] = 1;
x = 0, y = 0;
for (o = 0; o < eo[i]; o++) {
int j = ej[i][o];
if (!visited[j]) {
dfs(j);
x += dq[j];
if (dp[j] == dq[j])
y = 1;
}
}
dp[i] = x, dq[i] = x + y;
}
int main() {
static int ii[N * 2], hh[N * 2], pp[N];
int n, h, i, j, ans;
scanf("%d", &n);
if (n % 2 != 0) {
printf("-1\n");
return 0;
}
for (h = 0; h < n * 2; h++) {
scanf("%s", ss[h]);
hh[h] = h;
}
sort(hh, 0, n * 2);
for (h = 0, i = 0; h < n * 2; h++)
ii[hh[h]] = h + 1 == n * 2 || strcmp(ss[hh[h + 1]], ss[hh[h]]) ? i++ : i;
for (i = 0; i < n; i++)
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
for (h = 0; h < n; h++) {
i = ii[h * 2 + 0], j = ii[h * 2 + 1];
pp[i] = j;
append(j, i);
}
ans = n;
for (i = 0; i < n; i++)
if (!visited[i]) {
int j_;
for (j = i; !visited[j]; j = pp[j])
visited[j] = 1;
j_ = j;
for (j = i; j != j_; j = pp[j])
visited[j] = 0;
j = j_;
do
dfs(j), j = pp[j];
while (j != j_);
if (j == pp[j])
ans -= dq[j];
else if (j == pp[pp[j]])
ans -= dp[j] + dp[pp[j]] + 2;
else {
int i_, x, y, z, tmp;
i_ = -1, x = -1, y = 0;
do {
tmp = max(x == -1 ? -1 : x + dp[i_] + dp[j] + 1, y + dq[j]), x = y, y = tmp;
i_ = j, j = pp[j];
} while (j != j_);
z = y;
i_ = j_, j = pp[j_], x = -1, y = 0;
do {
tmp = max(x == -1 ? -1 : x + dp[i_] + dp[j] + 1, y + dq[j]), x = y, y = tmp;
i_ = j, j = pp[j];
} while (j != j_);
z = max(z, x + dp[i_] + dp[j] + 1);
ans -= z;
}
}
printf("%d\n", ans);
return 0;
}
Compilation message
polygon.c: In function 'append':
polygon.c:45:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
45 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
polygon.c: In function 'main':
polygon.c:72:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d", &n);
| ^~~~~~~~~~~~~~~
polygon.c:78:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | scanf("%s", ss[h]);
| ^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
288 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
288 KB |
Output is correct |
14 |
Correct |
0 ms |
296 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
296 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
94 ms |
12620 KB |
Output is correct |
5 |
Correct |
100 ms |
12636 KB |
Output is correct |
6 |
Correct |
99 ms |
12588 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
12700 KB |
Output is correct |
2 |
Correct |
95 ms |
14332 KB |
Output is correct |
3 |
Correct |
87 ms |
12620 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
98 ms |
19888 KB |
Output is correct |
6 |
Correct |
93 ms |
12748 KB |
Output is correct |
7 |
Correct |
95 ms |
12752 KB |
Output is correct |
8 |
Correct |
93 ms |
12620 KB |
Output is correct |
9 |
Correct |
96 ms |
13020 KB |
Output is correct |
10 |
Correct |
54 ms |
12732 KB |
Output is correct |
11 |
Correct |
0 ms |
292 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
296 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
288 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
288 KB |
Output is correct |
14 |
Correct |
0 ms |
296 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
296 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
296 KB |
Output is correct |
19 |
Correct |
94 ms |
12620 KB |
Output is correct |
20 |
Correct |
100 ms |
12636 KB |
Output is correct |
21 |
Correct |
99 ms |
12588 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
101 ms |
12700 KB |
Output is correct |
24 |
Correct |
95 ms |
14332 KB |
Output is correct |
25 |
Correct |
87 ms |
12620 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
98 ms |
19888 KB |
Output is correct |
28 |
Correct |
93 ms |
12748 KB |
Output is correct |
29 |
Correct |
95 ms |
12752 KB |
Output is correct |
30 |
Correct |
93 ms |
12620 KB |
Output is correct |
31 |
Correct |
96 ms |
13020 KB |
Output is correct |
32 |
Correct |
54 ms |
12732 KB |
Output is correct |
33 |
Correct |
0 ms |
292 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 KB |
Output is correct |
37 |
Correct |
91 ms |
12620 KB |
Output is correct |
38 |
Correct |
94 ms |
12620 KB |
Output is correct |
39 |
Correct |
104 ms |
12748 KB |
Output is correct |
40 |
Correct |
92 ms |
12724 KB |
Output is correct |
41 |
Correct |
94 ms |
12748 KB |
Output is correct |
42 |
Correct |
93 ms |
12624 KB |
Output is correct |
43 |
Correct |
96 ms |
12716 KB |
Output is correct |
44 |
Correct |
95 ms |
12752 KB |
Output is correct |
45 |
Correct |
92 ms |
12736 KB |
Output is correct |
46 |
Correct |
92 ms |
12688 KB |
Output is correct |
47 |
Correct |
84 ms |
13020 KB |
Output is correct |
48 |
Correct |
90 ms |
12620 KB |
Output is correct |
49 |
Correct |
95 ms |
14408 KB |
Output is correct |
50 |
Correct |
81 ms |
12608 KB |
Output is correct |
51 |
Correct |
0 ms |
308 KB |
Output is correct |
52 |
Correct |
100 ms |
19884 KB |
Output is correct |
53 |
Correct |
96 ms |
12760 KB |
Output is correct |
54 |
Correct |
97 ms |
12856 KB |
Output is correct |
55 |
Correct |
89 ms |
12656 KB |
Output is correct |
56 |
Correct |
89 ms |
13040 KB |
Output is correct |
57 |
Correct |
55 ms |
12760 KB |
Output is correct |
58 |
Correct |
0 ms |
212 KB |
Output is correct |
59 |
Correct |
1 ms |
212 KB |
Output is correct |
60 |
Correct |
1 ms |
212 KB |
Output is correct |
61 |
Correct |
0 ms |
292 KB |
Output is correct |
62 |
Correct |
0 ms |
212 KB |
Output is correct |
63 |
Correct |
0 ms |
212 KB |
Output is correct |
64 |
Correct |
0 ms |
212 KB |
Output is correct |
65 |
Correct |
93 ms |
12712 KB |
Output is correct |
66 |
Correct |
91 ms |
12616 KB |
Output is correct |
67 |
Correct |
92 ms |
12680 KB |
Output is correct |
68 |
Correct |
0 ms |
340 KB |
Output is correct |
69 |
Correct |
1 ms |
292 KB |
Output is correct |
70 |
Correct |
0 ms |
292 KB |
Output is correct |
71 |
Correct |
1 ms |
296 KB |
Output is correct |
72 |
Correct |
1 ms |
292 KB |
Output is correct |
73 |
Correct |
0 ms |
212 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
296 KB |
Output is correct |
76 |
Correct |
0 ms |
288 KB |
Output is correct |
77 |
Correct |
0 ms |
292 KB |
Output is correct |
78 |
Correct |
1 ms |
340 KB |
Output is correct |
79 |
Correct |
0 ms |
288 KB |
Output is correct |
80 |
Correct |
0 ms |
212 KB |
Output is correct |
81 |
Correct |
0 ms |
212 KB |
Output is correct |
82 |
Correct |
0 ms |
212 KB |
Output is correct |
83 |
Correct |
1 ms |
340 KB |
Output is correct |