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

    二分图最大匹配

    每个学生只能代表一个课程,每个课程最多有一个代表。问能否合法。

    最大匹配数等于课程则输出“YES”。

    // File Name: 1469.cpp
    // Author: zlbing
    // Created Time: 2013/2/23 22:15:52
    
    #include<iostream>
    #include<string>
    #include<algorithm>
    #include<cstdlib>
    #include<cstdio>
    #include<set>
    #include<map>
    #include<vector>
    #include<cstring>
    #include<stack>
    #include<cmath>
    #include<queue>
    using namespace std;
    #define CL(x,v); memset(x,v,sizeof(x));
    #define INF 0x3f3f3f3f
    
    #define MAXN 305
    int Left[MAXN];
    int w[MAXN][MAXN];
    bool S[MAXN],T[MAXN];
    int P,N;
    bool match(int i)
    {
        S[i]=true;
        for(int j=1;j<=N;j++)if(w[i][j]&&!T[j])
        {
            T[j]=true;
            if(Left[j]==0||match(Left[j]))
            {
                Left[j]=i;
                return true;
            }
        }
        return false;
    }
    int main(){
        int cas;
        scanf("%d",&cas);
        while(cas--)
        {
            scanf("%d%d",&P,&N);
            CL(w,0);
            CL(Left,0);
            for(int i=1;i<=P;i++)
            {
                int a,b;
                scanf("%d",&a);
                for(int j=0;j<a;j++)
                {
                    scanf("%d",&b);
                    w[i][b]=1;
                }
            }
            int sum=0;
            for(int i=1;i<=P;i++)
            {
                CL(S,0);
                CL(T,0);
                if(match(i))sum++;
            }
            if(sum==P)
                printf("YES\n");
            else printf("NO\n");
        }
        return 0;
    }
  • 相关阅读:
    [SCOI2013]火柴棍数字(背包)
    [NOI2015]品酒大会
    后缀数组小结
    [POI2009]Slw
    [POI2009]Wie
    [POI2008]账本BBB
    ant语法和规范
    使用Hudson进行持续集成
    gnu make
    可信执行环境(TEE)介绍
  • 原文地址:https://www.cnblogs.com/arbitrary/p/2923897.html
Copyright © 2011-2022 走看看