ch6802 car is placed (bipartite graph matching entry)

description

Given a chessboard M columns of N rows, some known lattice placement prohibited. The maximum number can not attack each other to put on the car to ask the board. Car on the grid, attack range and Chinese chess "car" consistent. N, M≤200.

Input Format

First row n, m, t (t indicates a prohibition has lattice)
of the second row to the behavior t + 1 x, y, represent the positions where the lattice is prohibited, for the x-row x, column y is the y ranks numbering starts at 1.

Output Format

An integer representing the maximum number of vehicles can be put.

Sample input

8 8 0

Sample Output

8

analysis:

After the i, j selected, I rows and j columns can not be picked
is selected only once for each row, each column select only one
row and column corresponding to a set of two bipartite graph, i.e., seek the maximum matching problem.

my ac code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
const int maxm=405;
int g[maxm][maxm];
vector<int>chess[maxm];
int now[maxm];
int mark[maxm];
int n,m,k;
int dfs(int x){
    for(int i=0;i<(int)chess[x].size();i++){
        int v=chess[x][i];
        if(!mark[v]){
            mark[v]=1;
            if(now[v]==0||dfs(now[v])){
                now[v]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main(){
    cin>>n>>m>>k;
    while(k--){
        int x,y;
        cin>>x>>y;
        g[x][y]=1;
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(!g[i][j]){
                chess[i].push_back(j);
            }
        }
    }
    int ans=0;
    for(int i=1;i<=n+m;i++){
        memset(mark,0,sizeof mark);
        ans+=dfs(i);
    }
    cout<<ans<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_44178736/article/details/92442850