二分匹配的认识

Consider a group of N students and P courses. Each student visits zero, one or more than one courses. Your task is to determine whether it is possible to form a committee of exactly P students that satisfies simultaneously the conditions:

. every student in the committee represents a different course (a student can represent a course if he/she visits that course)

. each course has a representative in the committee

Your program should read sets of data from a text file. The first line of the input file contains the number of the data sets. Each data set is presented in the following format:

这是学生匹配课程:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <math.h>
#define Fast_io ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define filein freopen("input.txt", "r", stdin);
#define fileout freopen("output.txt", "w", stdout);
using namespace std;
typedef long long ll;
const int N = 1e7 + 10;
int n, m;
int e[305][305], match[305];
bool book[305];
int dfs(int u)
{
    int i;
    for (i = 1; i <= n; i++)
    {
        if (book[i] == 0 && e[u][i])
        {
            book[i] = 1;
            if (match[i] == 0 || dfs(match[i]))
            {
                match[i] = u;
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    Fast_io;
    int t;
    cin >> t;
    while (t--)
    {
        memset(e, 0, sizeof(e));
        memset(match, 0, sizeof(match));
        cin >> n >> m;
        int i, sum = 0;
        for (i = 1; i <= n; i++)
        {
            int p;
            cin >> p;
            while (p--)
            {
                int t;
                cin >> t;
                e[t][i] = 1;//试一试后匹配前
            }
        }
        for (i = 1; i <= m; i++)
        {
            memset(book, 0, sizeof(book));
            if (dfs(i))
                sum++;
        }
        if (sum == n)
            cout << "YES" << endl;
        else 
            cout << "NO" << endl;
    }
    return 0;
}

这是课程匹配学生:(摘自孙同学的(老师))

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int n, p, match[500];
bool book[500], e[500][500];

int dfs(int u) {//匈牙利算法
    int i;
    for (i = 1; i <= p; i++) {
        if (e[u][i] == 1 && book[i] == 0) {
            book[i] = 1;
            if (match[i] == 0 || dfs(match[i])) {
                match[i] = u;
                return 1;
            }
        }
    }
    return 0;
}

int main(int argc, char const *argv[]) {
    int t, i, j, x, y, sum;
    scanf("%d", &t);
    while (t--) {
        sum = 0;//注意每组数据的初始化
        memset(e, 0, sizeof(e));
        memset(match, 0, sizeof(match));
        scanf("%d %d", &p, &n);
        for (i = 1; i <= p; i++) {
            scanf("%d", &x);
            while (x--) {
                scanf("%d", &y);
                e[y][i] = 1;
            }
        }
        for (i = 1; i <= n; i++) {
            memset(book, 0, sizeof(book));
            if (dfs(i)) sum++;
        }
        if (sum == p)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}


两者的区别在于匹配的对象不同,但是多理解之后,匹配是相互的,不会存在我匹配你和你匹配我的最大数是不一样的!!(可能在研究研究)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值