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 }
原文地址:https://www.cnblogs.com/767355675hutaishi/p/4437517.html