HDU 1083 Courses

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1083

解题思路:

这是一个简单的匹配问题,只要每门课程都能和一个同学匹配,那么输出“YES”,否则输出“NO”

换句话说就是匹配数=课程数

实现代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int MAXN=300;
int map[MAXN][MAXN];
bool bmask[MAXN];
int cx[MAXN],cy[MAXN];
int N,P;

int findPath(int u){
    for(int i=1;i<=N;i++){
        if(map[u][i]&&!bmask[i]){
            bmask[i]=1;

            if(cy[i]==-1||findPath(cy[i])){
                cy[i]=u;
                cx[u]=i;
                return 1;
            }
        }
    }
    return 0;
}

int MaxPath(){
    int res=0;

    memset(cx,-1,sizeof(cx));
    memset(cy,-1,sizeof(cy));

    for(int i=1;i<=P;i++){
        if(cx[i]==-1){
            memset(bmask,0,sizeof(bmask));
            res+=findPath(i);
        }
    }
    return res;
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&P,&N);
        memset(map,0,sizeof(map));
        for(int i=1;i<=P;i++){
            int n;
            scanf("%d",&n);
            while(n--){
                int v;
                scanf("%d",&v);
                map[i][v]=1;
            }
        }
        int ans=MaxPath();
        if(ans==P)
            printf("YES
");
        else printf("NO
");
    }
}
自己选的路,跪着也要把它走完------ACM坑
原文地址:https://www.cnblogs.com/IKnowYou0/p/6548917.html