zoukankan      html  css  js  c++  java
  • poj 1469 COURSES

    最基本的二分匹配,用匈牙利算法DFS实现。

    #include<iostream>
    #include<string.h>
    #include<stdio.h>
    #define MAXD 301
    #define INF 0x3f3f3f3f
    using namespace std;
    int P,N,res,cx[MAXD],cy[MAXD];
    bool edge[MAXD][MAXD],vis[MAXD];
    
    int path(int u)
    {
        int i;
        for(i=1;i<=N;i++)
        {
            if(edge[u][i]&&!vis[i])
            {
                vis[i]=true;
                if(cy[i]==-1||path(cy[i]))
                {
                    cx[u]=i;
                    cy[i]=u;
                    return 1;
                }
            }
        }
        return 0;
    }
    
    int maxmatch()
    {
        int sum=0;
        memset(cx,-1,sizeof(cx));
        memset(cy,-1,sizeof(cy));
        int i;
        for(i=1;i<=P;i++)
        {
            if(cx[i]==-1)
            {
                memset(vis,0,sizeof(vis));
                sum+=path(i);
            }
        }
        return sum;
    }
    int main()
    {
        //freopen("test.txt","r",stdin);
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d%d",&P,&N);
            int k,temp,i,j;
            memset(edge,0,sizeof(edge));
            for(i=1;i<=P;i++)
            {
                scanf("%d",&k);
                for(j=1;j<=k;j++)
                {
                    scanf("%d",&temp);
                    edge[i][temp]=true;
                }
            }
            res=maxmatch();
            if(res==P)
            printf("YES\n");
            else
            printf("NO\n");
        }
    }
  • 相关阅读:
    C++ string char[] 转化
    c++ 转化
    2014/4/16
    2014/4/11
    垂直电商现倒闭潮
    经典K线组合图解 > 正文
    上下影线
    分​析​主​力​试​盘​手​法
    nginx重新编译不停服
    nexus
  • 原文地址:https://www.cnblogs.com/longlongagocsu/p/2874246.html
Copyright © 2011-2022 走看看