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

本文探讨了一种用于解决特定匹配问题的算法,通过构建数据结构和使用深度优先搜索等技术,实现了高效的求解过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

                                  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\n");
60       else
61             printf("NO\n");
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值