1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| #include <iostream> #include <cstdio> #include <cstring>
int n, m; int match[10004], vis[10004]; int map[102][102], col[102][102], num[102][102]; int head[10004], tot; struct Edge { int from, to, next; } edge[20004]; int ans;
void Addedge(int u, int v) { edge[++ tot] = (Edge) {u, v, head[u]}; head[u] = tot; }
void Build() { for(int i = 1; i <= n; i ++) col[1][i] = col[1][i - 1] ^ 1; for(int i = 2; i <= n; i ++) for(int j = 1; j <= n; j ++) col[i][j] = col[i - 1][j] ^ 1; int c = col[1][1]; for(int i = 1; i <= n; i ++) for(int j = 1; j <= n; j ++) num[i][j] = (i - 1) * n + j; for(int i = 1; i <= n; i ++) for(int j = 1; j <= n; j ++) if(col[i][j] == c && map[i][j] == 0) { if(j + 1 <= n && map[i][j + 1] == 0) Addedge(num[i][j], num[i][j + 1]); if(i + 1 <= n && map[i + 1][j] == 0) Addedge(num[i][j], num[i + 1][j]); if(j - 1 >= 1 && map[i][j - 1] == 0) Addedge(num[i][j], num[i][j - 1]); if(i - 1 >= 1 && map[i - 1][j] == 0) Addedge(num[i][j], num[i - 1][j]); } }
bool Dfs(int u) { for(int i = head[u]; i; i = edge[i].next) { int v = edge[i].to; if(! vis[v]) { vis[v] = true; if(!match[v] || Dfs(match[v])) { match[v] = u; return true; } } } return false; }
int main() { scanf("%d%d", &n, &m); for(int i = 1; i <= m; i ++) { int x, y; scanf("%d%d", &x, &y); map[x][y] = 1; } Build(); for(int i = 1; i <= n * n; i ++) { memset(vis, 0, sizeof(vis)); if(Dfs(i)) ans ++; } printf("%d\n", ans); return 0; }
|