HDU 1179 Ollivanders: Makers of Fine Wands since 382 BC.(二分图匹配--匈牙利算法)

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): 1314 Accepted Submission(s): 718

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

有n个巫师,有m个巫棒。每个巫师棒都适合一定的巫师。

下面是巫棒匹配巫师的代码:


/*
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int match[200],e[205][205],book[205],n,m;

int dfs(int u)
{
    for(int i=1; i<=m; i++)
    {
        if(book[i]==0&&e[u][i]==1)
        {
            book[i]=1;
            if(match[i]==-1||dfs(i)==1)
            {
                match[i]=u;
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(e,0,sizeof(e));
        memset(match,-1,sizeof(match));
        int t1,t2;
        for(int i=1; i<=m; i++)
        {
            scanf("%d",&t1);
            for(int j=0; j<t1; j++)
            {
                scanf("%d",&t2);
                e[t2][i]=1;
            }
        }
        int sum=0;
        //memset(book,0,sizeof(book));
        for(int i=1; i<=n; i++)
        {
            if(dfs(i))
            {
                memset(book,0,sizeof(book));
                sum++;
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}
/*
#include<stdio.h>
#include<string.h>
int map[110][110],last_y[110],hav[110],N,M;
int findmax(int m);
int main()
{
    int i,j,a,b,Sum;
    while(scanf("%d %d",&N,&M)!=EOF)
    {
        memset(map,0,sizeof(map));
        memset(last_y,-1,sizeof(last_y));
        Sum=0;
        for(i=1;i<=M;i++)
        {
            scanf("%d",&a);
            for(j=0;j<a;j++)
            {
                scanf("%d",&b);
                map[b][i]=1;
            }
        }
        for(i=1;i<=N;i++)
        {
            if(findmax(i))
            {
                memset(hav,0,sizeof(hav));
                Sum++;
            }
        }
        printf("%d\n",Sum);
    }
    return 0;
}
int findmax(int m)
{
    int i;
    for(i=1;i<=M;i++)
    {
        if(!hav[i]&&map[m][i])
        {
            hav[i]=1;
            if(last_y[i]==-1||findmax(last_y[i]))
            {
                last_y[i]=m;
                return 1;
            }
        }
    }
    return 0;
}
*/

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int match[200],e[200][200],book[200],n,m;

int dfs(int u)
{
    for(int i=1;i<=n;i++)
    {
        if(book[i]==0&&e[u][i]==1)
        {
            book[i]=1;
            if(match[i]==0||dfs(match[i]))
            {
                match[i]=u;
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        memset(e,0,sizeof(e));
        memset(book,0,sizeof(book));
        memset(match,0,sizeof(match));
        int t1,t2;
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&t1);
            for(int j=0;j<t1;j++)
            {
                scanf("%d",&t2);
                e[i][t2]=1;
            }
        }
        int sum=0;
        for(int i=1;i<=m;i++)
        {
            memset(book,0,sizeof(book));
            if(dfs(i))
            {
                sum++;
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}
时间: 2024-11-09 10:22:33

HDU 1179 Ollivanders: Makers of Fine Wands since 382 BC.(二分图匹配--匈牙利算法)的相关文章

HDU——1179 Ollivanders: Makers of Fine Wands since 382 BC.

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): 1895    Accepted Submission(s): 1084 Problem Description In Diagon Alley ,there is only one Wand-

HDU 1179 Ollivanders: Makers of Fine Wands since 382 BC.(匈牙利算法)

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

hdu-----(1179)Ollivanders: Makers of Fine Wands since 382 BC.(二分匹配)

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): 935    Accepted Submission(s): 523 Problem Description In Diagon Alley ,there is only one Wand-se

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

题目链接: 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;

hdu1179——Ollivanders: Makers of Fine Wands since 382 BC.

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): 964    Accepted Submission(s): 539 Problem Description In Diagon Alley ,there is only one Wand-s

HDU1179_Ollivanders: Makers of Fine Wands since 382 BC.(二分图/最大匹配)

解题报告 题意: n个巫师m个魔杖,每个魔杖可以被不同的巫师使用.求多少个魔杖会被买. 思路: 二分图最大匹配简单题. #include <iostream> #include <cstring> #include <cstdio> using namespace std; int mmap[110][110],n,m,vis[110],pre[110]; int dfs(int x) { for(int i=1; i<=n; i++) { if(!vis[i]&

HPU1179 Ollivanders: Makers of Fine Wands since 382 BC.【二分图最大匹配】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1179 题目大意: 题目太长了,简单的意思就是:有N个魔杖,M个魔法师,魔杖有多个匹配的魔法师.但是一个魔法师 只能对应一根魔杖.那么问题来了:最多有多少魔法师能得到魔棒. 思路: 做一个二分图,一边是魔杖,另一边是魔法师.相应的匹配作为二分图的边.利用匈牙利算法,求出二 分图最大匹配是多少. AC代码: #include<iostream> #include<algorithm> #

hdu1179Ollivanders: Makers of Fine Wands since 382 BC. (二分最大匹配)

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

HDU 5943 Kingdom of Obsession 【二分图匹配 匈牙利算法】 (2016年中国大学生程序设计竞赛(杭州))

Kingdom of Obsession Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 49    Accepted Submission(s): 14 Problem Description There is a kindom of obsession, so people in this kingdom do things very