zoukankan      html  css  js  c++  java
  • hdu 1083 Courses(二分图最大匹配)

    题意:

    P门课,N个学生。      (1<=P<=100    1<=N<=300)

    每门课有若干个学生可以成为这门课的代表(即候选人)。

    又规定每个学生最多只能成为一门课的代表(即要专一)。

    问是否存在一种安排方案使得每门课都有代表。

    思路:

    二分图最大匹配经典,,看代码。

    代码:

    int T,n,p;
    vector<int> graph[505];
    bool bmask[505];
    int cx[505],cy[505];
    
    int findPath(int u){
        int L=graph[u].size();
        rep(i,0,L-1){
            int v=graph[u][i];
            if(!bmask[v]){
                bmask[v]=true;
                if(cy[v]==-1||findPath(cy[v])){
                    cy[v]=u;
                    cx[u]=v;
                    return 1;
                }
            }
        }
        return 0;
    }
    int MaxMatch(){
        int ans=0;
        rep(i,1,p) cx[i]=-1;
        rep(i,1,n) cy[i]=-1;
        rep(i,1,p) if(cx[i]==-1){
            mem(bmask,false);
            ans+=findPath(i);
        }
        return ans;
    }
    int main(){
        cin>>T;
        while(T--){
            scanf("%d%d",&p,&n);
            rep(i,1,p) graph[i].clear(); //p kinds of class
            rep(i,1,p){
                int x,a;
                scanf("%d",&x); //student num
                while(x--){
                    scanf("%d",&a);
                    graph[i].push_back(a);
                }
            }
            int dd=MaxMatch();
            if(dd!=p)
                puts("NO");
            else
                puts("YES");
        }
    }
  • 相关阅读:
    pwnable
    pwnable
    pwnable
    uva 11971
    uva 11582
    【数据结构】关于递归的几个例子
    【数据结构】快速排序
    【Python】range 倒序
    【数据结构】静态链表的实现(C语言描述)
    【数据结构】KMP 算法
  • 原文地址:https://www.cnblogs.com/fish7/p/4088217.html
Copyright © 2011-2022 走看看