zoukankan      html  css  js  c++  java
  • 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坑
  • 相关阅读:
    ubuntu14.04通过将语音,耳机没有声音
    网络编程easy错误点-手知道
    如何实现网页在线咨询框架
    C++定义自己的命名空间和头文件
    使用光标
    工作日两个日期之间的数
    Swift
    Swift
    Swift
    Swift
  • 原文地址:https://www.cnblogs.com/IKnowYou0/p/6548917.html
Copyright © 2011-2022 走看看