AcWing 372. Checkerboard coverage

algorithm

Bipartite graph + matching

Ideas

side

A domino links two grids, a grid is a node, and a domino is an edge connecting the two grids.

0 element

Dye the grid black and white with no edges between the same color grids.

1 element

Each grid can only be covered by 1 domino, with only one edge.

Knowledge

Hungary

bool dfs(int x) {
    for (unsigned int i = 0; i < e[x].size(); i++) {
        int y = e[x][i];
        if (v[y]) continue;
        v[y] = 1;
        if (f[y] == -1 || dfs(f[y])) {
            f[y] = x;
            return 1;
        }
    }
    return 0;
}

  

Bipartite graph matching

There is Zeng Guanglu plus one.

Code

#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 106;
const int dx[4] = {0,0,1,-1};
const int dy[4] = {1,-1,0,0};
int n, m, ans, f[N*N];
bool b[N][N], v[N*N];
vector<int> e[N*N];

bool dfs(int x) {
    for (unsigned int i = 0; i < e[x].size(); i++) {
        int y = e[x][i];
        if (v[y]) continue;
        v[y] = 1;
        if (f[y] == -1 || dfs(f[y])) {
            f[y] = x;
            return 1;
        }
    }
    return 0;
}

int main() {
    cin >> n >> m;
    while (m--) {
        int x, y;
        scanf("%d %d", &x, &y);
        b[x-1][y-1] = 1;
    }
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
            if (!b[i][j])
                for (int k = 0; k < 4; k++) {
                    int x = i + dx[k], y = j + dy[k];
                    if (x >= 0 && x < n && y >= 0 && y < n && !b[x][y]) {
                        e[i*n+j].push_back(x * n + y);
                        e[x*n+y].push_back(i * n + j);
                    }
                }
    memset(f, -1, sizeof(f));
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) {
            if ((i ^ j) & 1) continue; 
            memset (v, 0, sizeof (v)); 
            ans + = dfs (i * n + j); 
        } 
    cout << ans << endl; 
    return 0; 
} 

Author : Ruanmowen 
link: https://www.acwing.com/activity/content/code/content/274974/ 
Source: AcWing 
copyright belongs to the author. For commercial reproduction, please contact the author for authorization, and for non-commercial reproduction, please indicate the source.

  

 

Guess you like

Origin www.cnblogs.com/ruanmowen/p/12724213.html