hdu-1179-二分图最大匹配

Ollivanders: Makers of Fine Wands since 382 BC.

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 1585    Accepted Submission(s): 878

Problem Description

In Diagon Alley ,there is only one Wand-seller,peeling gold letters over the door read Ollivanders: Makers of Fine Wands since 382 BC.A single wand lay on a faded purple cushion in the dusty window. 
A tinkling bell rang somewhere in the depths of the shop as they stepped inside.It was a tiny place,empty execpt for a single spindly chair which Hagrid sat on to wait.Harry felt strangely as though he had entered a very strict library;he swallowd a log of new questions which had just occurred to him and looked instead at the thousands of narrow boxes piled neatly right up to the ceiling.For some reason,the back of his neck prickled.The very dust and silence in here seemed to tingle with some secret magic. 
‘Good afternoon,‘said a soft voice.Harry jumped.Hagrid must have jumped,too,because there was a loud crunching noise and he got quickly off the spindly chair. 
An old man was standing before them, his wide pale eyes shining like moons through the gloom of the shop. 
‘Hello,‘ said Harry awkwardly. 
‘Ah yes,‘ said the man. ‘Yes,yes. I thought I‘d be seeing you soon,Harry Potter.‘It wasn‘t a question.You have your mother‘s eyes. It seems only yesterday she was in here herself,buying her first wand. Ten and a quarter inches long, swishy, made of willow. Nice wand for charm work.‘ 
Mor Ollivander moved closer to Harry.Harry wished he would blink.Those sivery eyes were a bit creepy. 
‘Your father, on the other hand, favoured a mahogany wand.Eleven inches.Pliable.A little more power and excellent for transfiguration.Well ,I say your father favoured it - it‘s really the wand that choosed the wizard, of cource.‘

Yes, some wands fit some wizards ,as we all know.But what Harry doesn‘t know is Ollivander have met a big trouble.That‘s more than twenty years ago,When Harry‘s father James Potter was still a student in Hogwarts.He went Diagon Alley to buy new books,passing by Ollivander‘s shop.Ollivander was crazy for a problem:He was too busy to choose most suitable wand for every wizard.Even more,there are too many customer that day.Though Ollivader knew every wand‘s favourite,he could not choose as many wizards as possible to get the wands. So James Potter,a very clever man ,gave him a magic disk with your program ,to help him sell wands as many as possible.

Please notice: one wand can only be sold to one wizard, and one wizard can only buy one wand,too.

Input

There are several cases. For each case, there is two integers N and M in the first line,which mean there is N wizards and M wands(0 < N <= M <= 100).
Then M lines contain the choices of each wand.The first integer in i+1th line is Ki,and after these there are Ki integers Bi,j which are the wizards who fit that wand. (0<=Ki<=N,1<=Bi,j<=N)

Output

Only one integer,shows how many wands Ollivander can sell.

Sample Input

3 4

3 1

2 3

1 1

1 1

0

Sample Output

2

Hint

Hint

Wand 1 fits everyone, Wand 2,3 only fit the first wizard,and Wand 4 does not fit anyone.So Ollivanders can sell two wands:
sell Wand 1 to Wizard 2 and Wand 2 to Wizard 1,or sell Wand 1 to Wizard 3 and Wand 3 to Wizard 1 ,or some other cases. But
he cannot sell 3 wands because no 3 wands just fit 3 wizards.

题目大意:一个wand匹配一个wizard,问最大匹配

代码:

#include <iostream>
#include <vector>
#include <cstring>
#include <cstdio>
using namespace std;
int link[102],vis[102];
vector<int>mp[102];
int n,m;
int dfs(int x)
{
    for(int i=0;i<mp[x].size();i++)
    {
        int v=mp[x][i];
        if(!vis[v])
        {
            vis[v]=1;
            if(link[v]==-1||dfs(link[v]))
            {
                link[v]=x;
                return 1;
            }
        }
    }
    return 0;
}
int main()
{
    while(cin>>n>>m)
    {
        for(int i=1;i<=m;i++)
        if(mp[i].size())
        mp[i].clear();
        int q,a;
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&q);
            while(q--)
            {   scanf("%d",&a);
                mp[i].push_back(a);
            }
        }
        int res=0;
        memset(link,-1,sizeof(link));
        for(int i=1;i<=n;i++)
        {
            memset(vis,0,sizeof(vis));
            if(dfs(i))
            res++;
        }
        cout<<res<<endl;

    }
}
时间: 2024-10-13 19:33:40

hdu-1179-二分图最大匹配的相关文章

HDU 1528 (二分图最大匹配 + 最小覆盖, 14.07.17)

Problem Description Adam and Eve play a card game using a regular deck of 52 cards. The rules are simple. The players sit on opposite sides of a table, facing each other. Each player gets k cards from the deck and, after looking at them, places the c

hdu 1281 二分图最大匹配

对N个可以放棋子的点(X1,Y1),(x2,Y2)......(Xn,Yn);我们把它竖着排看看~(当然X1可以对多个点~) X1   Y1 X2   Y2 X3   Y3 ..... Xn   Yn 可以发现:可以根据X坐标与Y坐标把这些点转换为二分图! 首先:只有左边的点与右边的点有关系 其次:符合二分图的最大匹配特性,可以看到如果选择了(X1,Y1)这个点,那么X1与Y1都不能与其他点匹配了,不然的话棋子会互相攻击! 最后:找关键点,只要枚举每条边,删了,看看最大匹配有没有减小,减小了就是

hdu 3729(二分图最大匹配)

I'm Telling the Truth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2006    Accepted Submission(s): 1011 Problem Description After this year’s college-entrance exam, the teacher did a survey i

HDU 3279 二分图最大匹配

DES: 就是说对每个人都给你一个区间.但一个人只匹配一个数.问你满足匹配的人的序号字典序最大时的最大匹配是什么. 前几天刚做的UVALive 6322...当然是不一样的...那个要求的最大匹配的个数...求V2的最小字典序...这个呢...就是最大匹配中V1字典序最小是多少...(已晕菜) 大概理解了算法工作流程...对每一个V1中的位置...寻找匹配...直到找不到增广路算法结束...先匹配谁就可以先满足它的匹配咯...(是吗...) #include<stdio.h> #include

hdu 1054 二分图最大匹配

思路:模板题,注意是无向图,所以最后结果要除以2.点有1500个,邻接矩阵会超时,用了邻接表. 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 #include<cstdlib> 7 #include<sstream> 8 #include<iomanip>

HDU 3729 I&#39;m Telling the Truth(二分图最大匹配+结果输出)

题目地址:HDU 3729 二分图最大匹配+按字典序输出结果.只要从数字大的开始匹配就可以保证字典序最大了.群里有人问..就顺手写了这题.. 代码如下: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int vis[110000], head[110000], cnt, link[110000], n, a[

[HDU] 2063 过山车(二分图最大匹配)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2063 女生为X集合,男生为Y集合,求二分图最大匹配数即可. 1 #include<cstdio> 2 #include<iostream> 3 #include<string.h> 4 #include<algorithm> 5 #include<math.h> 6 #include<stdbool.h> 7 #include<ti

hdu 1179 最大匹配

题意:n个ren m个棍子 每个棍子可以与i个人结合 问最大有多少个结合#include<iostream> #include<cmath> using namespace std; int map[111][111]; int fa[111]; int v[111]; int n,m; int dfs(int x) { for(int i=1;i<=n;i++) { if(map[x][i]&&!v[i])//回溯时i不能再匹配 { v[i]=1; if(!

HDU ACM 1281 棋盘游戏-&gt;二分图最大匹配(匈牙利算法实践)

分析:该題可以用x坐标去匹配y坐标,匹配成功一次就是一个可放棋子的点,最后求得的的二分图最大匹配就是可以放的最大棋子数.求二分图的最大匹配使用匈牙利算法.之后通过删除一条边来判断一个点是否为关键点,若删边后,最大匹配数不变则不是,否则是,通过分别删除每个点进行测试,最终即可算出关键点的个数. #include<iostream> using namespace std; #define N 102 int map[N][N]; //记录连接x和y的边 bool vis[N]; //记录y中节点

HDU ACM 1083 Courses 二分图最大匹配

题意:p门课,每门课有若干学生,要为每门课分配一名课代表,每个学生只能担任一门课的课代表,若每个课都能找到课代表,则输出"YES",否则"NO". 分析:二分图的最大匹配,对课程.学生关系建立一个图,进行二分图最大匹配,当最大匹配数==课程数时说明能够满足要求,否则不能. #include<iostream> using namespace std; #define N 303 bool cs[N][N]; //cs[i][j]表示学生j是否选i这个课程