#include <stdio.h>
#define N 200000
#define M 200000
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
int ij[M * 2], eh[N][2], eo[N];
int solve1(int *aa, int n) {
int i, k, k_;
k = 0;
for (i = 0; i < n; i++)
if (aa[i] == 1)
k++;
k_ = INF;
for (i = 0; i < n; i++) {
if (aa[i] == 1)
k--;
k_ = min(k_, k);
if (aa[i] == 0)
k++;
}
return k_;
}
int solve2(int *aa, int n) {
static int bb[N];
int i, j, k_;
k_ = INF;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
bb[j] = aa[(i + j) % n];
k_ = min(k_, solve1(bb, n) + 1);
}
return k_;
}
int main() {
static char visited[N];
static int aa[N];
int n, m, h, p, i, i_, j, k, c, d, cnt, o;
scanf("%d%d", &n, &m);
k = 0, d = n;
for (h = 0; h < m; h++) {
scanf("%d%d", &i, &j), i--, j--;
if (i == -1)
k++;
else if (j != -1) {
if (i != j) {
ij[h << 1 | 0] = i, ij[h << 1 | 1] = j;
eh[i][eo[i]++] = h << 1 | 0, eh[j][eo[j]++] = h << 1 | 1;
} else
d--;
}
}
cnt = d;
for (i = 0; i < n; i++)
if (eo[i] == 1 && !visited[i]) {
p = -1, i_ = i, m = 0;
do {
visited[i_] = 1;
for (o = eo[i_]; o--; ) {
h = eh[i_][o], j = ij[h ^ 1];
if (j != p) {
aa[m++] = h & 1;
p = i_, i_ = j;
break;
}
}
} while (eo[i_] != 1);
visited[i_] = 1;
c = solve1(aa, m);
if (k == 0 && c > 0) {
printf("-1\n");
return 0;
}
cnt += c, k++;
}
for (i = 0; i < n; i++)
if (eo[i] == 2 && !visited[i]) {
p = -1, i_ = i, m = 0;
do
for (o = eo[i_]; o--; ) {
h = eh[i_][o], j = ij[h ^ 1];
if (j != p) {
aa[m++] = h & 1;
p = i_, i_ = j;
break;
}
}
while (i_ != i);
if (k == 0) {
printf("-1\n");
return 0;
}
c = solve2(aa, m);
if (k == 1 && c > 1) {
printf("-1\n");
return 0;
}
cnt += c;
}
printf("%d\n", cnt);
return 0;
}
Compilation message
Main.c: In function 'main':
Main.c:47:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
Main.c:50:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | scanf("%d%d", &i, &j), i--, j--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
0 ms |
212 KB |
Output is partially correct |
2 |
Partially correct |
0 ms |
212 KB |
Output is partially correct |
3 |
Partially correct |
0 ms |
212 KB |
Output is partially correct |
4 |
Execution timed out |
2072 ms |
212 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
33 ms |
4160 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2074 ms |
296 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2074 ms |
296 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Partially correct |
0 ms |
212 KB |
Output is partially correct |
2 |
Partially correct |
0 ms |
212 KB |
Output is partially correct |
3 |
Partially correct |
0 ms |
212 KB |
Output is partially correct |
4 |
Execution timed out |
2072 ms |
212 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |