P3386 【模板】二分图匹配

 

 

#include <bits/stdc++.h>

using namespace std;
const int maxn = 1010;

int n,m,e;
int vis[maxn][maxn];
int ask[maxn];
int cnt, ans;
int matched[maxn];

bool fid(int x) {
    for (int i = 1 ; i <= m; i++)
      if (vis[x][i]) {
        if (ask[i])
            continue;
        ask[i] = 1;
        if (!matched[i] || fid(matched[i])) {
            matched[i] = x ;
            return true;
        }
    }
    return false;
}

void match() {
    cnt = 0;
    memset(matched, 0, sizeof(matched));
    for(int i = 1; i <= n; ++i) {
        memset(ask, 0, sizeof(ask));
        if(fid(i)) {
            cnt++;
        }
    }
    ans = cnt;
}

int main()
{
    scanf("%d %d %d",&n,&m,&e);
    cnt = 0;
    for(int i = 1; i <= e; i++) {
        int x,y;
        scanf("%d %d",&x, &y);
        vis[x][y] = 1;
    }
    match();
    printf("%d\n",ans);
    return 0;
}

 

全部评论

相关推荐

10-15 03:05
门头沟学院 Java
CADILLAC_:凯文:我的邮箱是死了吗?
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务