AcWing car placement

AcWing car placement

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.

Input

  • The first line contains three integers N, M, T, where T represents the number of lattice disposed prohibited.

    Next T lines contains two integers x and y, x represents a line located on the y-th column grid placement prohibited, the number of ranks from 1 starts.

Output

  • Output an integer representing the result.

Data Size

  • 1≤N,M≤200

answer:

  • Bipartite graph matching.
  • easier. If the piece is placed in a grid, so that the pieces where the columns, the line, can not be put. Abstract look, the abstract of each line into a set of points on the left, each column abstracted into a set of points on the right. Place a piece of the line is equal to the point to which they point where even a side column. Obviously, we want to put as many pieces as much as necessary to make up for the connection. It is a bipartite graph matching.
#include <iostream>
#include <cstdio>
#include <cstring>
#define N 205
using namespace std;

struct E {int next, to;} e[N * N];
int n, m, t, num, ans;
int h[N], mat[N];
bool vis[N];
bool tag[N][N];

void add(int u, int v)
{
    e[++num].next = h[u];
    e[num].to = v;
    h[u] = num;
}

bool dfs(int x)
{
    for(int i = h[x]; i != 0; i = e[i].next)
        if(!vis[e[i].to])
        {
            vis[e[i].to] = 1;
            if(!mat[e[i].to] || dfs(mat[e[i].to]))
            {
                mat[e[i].to] = x;
                return 1;
            }
        }
    return 0;
}

int main()
{
    cin >> n >> m >> t;
    for(int i = 1; i <= t; i++)
    {
        int x, y;
        cin >> x >> y;
        tag[x][y] = 1;
    }
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= m; j++)
            if(!tag[i][j]) add(i, j);
    for(int i = 1; i <= n; i++)
    {
        memset(vis, 0, sizeof(vis));
        if(dfs(i)) ans++;
    }
    cout << ans;
    return 0;
}

Guess you like

Origin www.cnblogs.com/BigYellowDog/p/11525195.html
car