码迷,mamicode.com
首页 > 其他好文 > 详细

hdu-1179 Ollivanders: Makers of Fine Wands since 382 BC.---二分图匹配模板

时间:2018-04-23 00:09:13      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:ref   nbsp   dfs   mem   ret   +=   个数   const   make   

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1179

题目大意:

有n个人要去买魔杖,有m根魔杖(和哈利波特去买魔杖的时候一样,是由魔杖选人)。接下来是m行,每行第一个数k是第i根魔杖可以选的人数,接着k个数表示这根魔杖选的人的编号。最后问老板最多能卖出多少根魔杖。这个赤裸裸的模版题,套下就OK了。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 const int maxn = 1000 + 10;
 5 const int INF = 1e9;
 6 bool Map[maxn][maxn];
 7 int cx[maxn], cy[maxn];
 8 int cntx, cnty;
 9 bool vis[maxn];
10 bool dfs(int u)
11 {
12     for(int v = 1; v <= cnty; v++)
13     {
14         if(Map[u][v] && !vis[v])
15         {
16             vis[v] = 1;
17             if(cy[v] == -1 || dfs(cy[v]))
18             {
19                 cx[u] = v;
20                 cy[v] = u;
21                 return 1;
22             }
23         }
24     }
25     return 0;
26 }
27 int maxmatch()
28 {
29     int ans = 0;
30     memset(cx, -1, sizeof(cx));
31     memset(cy, -1, sizeof(cy));
32     for(int i = 1; i <= cntx; i++)
33     {
34         if(cx[i] == -1)
35         {
36             memset(vis, 0, sizeof(vis));
37             ans += dfs(i);
38         }
39     }
40     return ans;
41 }
42 int main()
43 {
44     int n, i, u, v;
45     while(cin >> cnty && cnty)
46     {
47         cin >> cntx;
48         memset(Map, 0, sizeof(Map));
49         for(int u = 1; u <= cntx; u++)
50         {
51             cin >> i;
52             while(i--)
53             {
54                 cin >> v;
55                 Map[u][v] = 1;
56             }
57         }
58         cout<<maxmatch()<<endl;
59     }
60     return 0;
61 }

 

hdu-1179 Ollivanders: Makers of Fine Wands since 382 BC.---二分图匹配模板

标签:ref   nbsp   dfs   mem   ret   +=   个数   const   make   

原文地址:https://www.cnblogs.com/fzl194/p/8910064.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!