• COURSES 赤裸裸的二分匹配大水题


                                      COURSES

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <string>
     7 #include <vector>
     8 #include <set>
     9 #include <map>
    10 #include <stack>
    11 #include <queue>
    12 #include <sstream>
    13 #include <iomanip>
    14 using namespace std;
    15 typedef long long LL;
    16 const int INF = 0x4fffffff;
    17 const double EXP = 1e-5;
    18 const int MS = 305;
    19 const int SIZE = 100005;
    20 
    21 // data struct
    22 int n,m;
    23 int edges[MS][MS];
    24 int cx[MS],cy[MS];
    25 int mark[MS];
    26 
    27 int path(int u)
    28 {
    29       for(int v=1;v<=m;v++)
    30       {
    31             if(edges[u][v]&&!mark[v])
    32             {
    33                   mark[v]=1;
    34                   if(cy[v]==-1||path(cy[v]))
    35                   {
    36                         cx[u]=v;   //  可以不要cx
    37                         cy[v]=u;
    38                         return 1;
    39                   }
    40             }
    41       }
    42       return 0;
    43 }
    44 
    45 void match()
    46 {
    47       int ans=0;
    48       memset(cx,0xff,sizeof(cx));
    49       memset(cy,0xff,sizeof(cy));
    50       for(int u=1;u<=n;u++)
    51       {
    52             if(cx[u]==-1)
    53             {
    54                   memset(mark,0,sizeof(mark));
    55                   ans+=path(u);
    56             }
    57       }
    58       if(ans==n)
    59             printf("YES
    ");
    60       else
    61             printf("NO
    ");
    62 }
    63 
    64 int main()
    65 {
    66       int T,u,v;
    67       scanf("%d",&T);
    68       while(T--)
    69       {
    70             scanf("%d%d",&n,&m);
    71             memset(edges,0,sizeof(edges));
    72             int cnt;
    73             for( u=1;u<=n;u++)
    74             {
    75                   scanf("%d",&cnt);
    76                   for(int i=1;i<=cnt;i++)
    77                   {
    78                         scanf("%d",&v);
    79                         edges[u][v]=1;
    80                   }
    81             }
    82             match();
    83 
    84       }
    85       return 0;
    86 }
  • 相关阅读:
    小程序_递归求年纪
    小程序_递归求阶层
    把字符串复制到剪贴板
    主流的三种RF方案及其优缺点比较
    CC1100E的ESD指标?
    delphi怎样打开一个文本文件
    CC1100模块 250K的速率的问题
    delphi 中显示access数据库表怎么实现
    delphi与Access数据库连接的步骤
    数据库基本语句
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/4437517.html
Copyright © 2020-2023  润新知