POJ3041, the minimum point coverage of the bipartite graph

is regarded as the left part of the bipartite graph, and the column is regarded as the right part of the bipartite graph. If there is an obstacle at the intersection of a row and a column, then the row is connected to the column, and the problem is converted to the minimum point coverage of the bipartite graph. The minimum point coverage of the bipartite graph is equal to the largest match of the bipartite graph.

/********************************************* ****************************
    > File Name: main.cpp
    > Author:Eagles
    > Mail:None
    > Created Time: Friday, September 14, 2018 18:46:23
    > Description:POJ3041, minimum vertex cover
 ************************************************** **********************/

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
using namespace std;
#define N 600
struct node
{
    int to;
    int nex;
}E[N*N];
int head[N];
int pre[N];
bool vis[N];
int n,cnt,k;

void addEdge(int a, int b)
{
    E[cnt].to=b;
    E[cnt].nex=head[a];
    head[a]=cnt++;
}

void init()
{
    memset(head,-1,sizeof(head));
    memset(pre,-1,sizeof(pre));
    cnt=0;
    for (int i=0; i<k; i++)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        addEdge(a-1,b-1);
    }
}

int dfs(int x)
{
    for (int i=head[x]; i!=-1; i=E[i].nex)
    {
        if (!vis[E[i].to])
        {
            vis[E[i].to]=true;

            if (pre[E[i].to]==-1||dfs(pre[E[i].to]))
            {
                pre[E[i].to]=x;
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    while (~scanf("%d%d",&n,&k))
    {
        init();
        int sum=0;

        for (int i=0; i<n; i++)
        {
            memset(vis,false,sizeof(vis));
            if (dfs(i))
                sum++;
        }

        printf("%d\n",sum);
    }
    return 0;
}