AcWing 373. Car placement

algorithm

Bipartite graph + matching

Ideas

node

Columns and rows are nodes

side

A child links an edge and a column.

0 element

A child cannot be in two columns or two rows. So connect a row with a fan

1 element

There can be only one child in each row, as well as the columns.

Code

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
inline int read()
{
    int s=0,w=1;
    char c=getchar();
    while(c<'0'||c>'9')
    {
        if(c=='-')
            w=-w;
        c=getchar();
    }
    while(c>='0'&&c<='9')
    {
        s=s*10+c-'0';
        c=getchar();
    }
    return s*w;
}
const int N=205;
int n,m,k,ans;
int fri[N];
bool flag[N][N],vis[N];
bool dfs(int x)
{
    for(int y=1;y<=m;y++)
    {
        if(flag[x][y]||vis[y])
            continue;
        vis[y]=1;
        if(!fri[y]||dfs(fri[y]))
        {
            fri[y]=x;
            return 1;
        }
    }
    return 0;
}
int main()
{
    n=read();
    m=read();
    k=read();
    for(int i=1;i<=k;i++)
    {
        int x,y;
        x=read();
        y=read();
        flag[x][y]=1;
    }
    ans=0;
    for(int i=1;i<=n;i++)
    {
        memset(vis,0,sizeof(vis));
        IF (DFS (I))
            ++ ANS; 
    } 
    printf ( "% d \ the n-", ANS); 
    return 0; 
} 

Author: ruanmowen 
link: https: //www.acwing.com/activity/content/code/content/275964/ 
Source: AcWing 
copyright of the author all. 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/12724561.html
car