zoukankan      html  css  js  c++  java
  • poj 1469 COURSES 二分匹配 dfs

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <time.h>
    using namespace std;
    
    int n,p,g[400][400],mk[400],cx[400],cy[400];
    
    int dfs(int u)
    {
        int v;
        for(v=1;v<=n;v++)
        {
            if(g[u][v]&&mk[v]==0)
            {
                mk[v]=1;
                if(cy[v]==0||dfs(cy[v]))
                {
                    cx[u]=v;
                    cy[v]=u;
                    return 1;
                }
            }
        }
        return 0;
    }
    int MaxMatch()
    {
        int ret=0,i;
        memset(cx,0,sizeof(cx));
        memset(cy,0,sizeof(cy));
        for(i=1;i<=p;i++)
        {
            if(cx[i]==0)
            {
                memset(mk,0,sizeof(mk));
                ret+=dfs(i);
            }
        }
        return ret;
    }
    int main()
    {
        int  i,_,t,u,j;
        scanf("%d",&_);
        while(_--)
        {
            scanf("%d%d",&p,&n);
            memset(g,0,sizeof(g));
            for(i=1;i<=p;i++)
            {
                scanf("%d",&t);
                for(j=0;j<t;j++)
                {
                    scanf("%d",&u);
                    g[i][u]=1;
                }
            }
            if(MaxMatch()==p) printf("YES
    ");
            else printf("NO
    ");
        }
        return 0;
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/

  • 相关阅读:
    SPI 1
    运算符
    移位运算
    Comet OJ
    图论 最短路 基础
    CF div3 582 C. Book Reading
    Comet OJ
    VScode 标记“&&”不是此版本中的有效语句分隔符。
    Educational Codeforces Round 63 (Rated for Div. 2)
    1223:An Easy Problem
  • 原文地址:https://www.cnblogs.com/xryz/p/4847802.html
Copyright © 2011-2022 走看看