Hdu1179 -- ollivanders: makers of fine wands since 382 BC.

Source: Internet
Author: User
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 descriptionin 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 random 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 wowould blink. Those sivery eyes were a bit creepy.
'Your father, on the other hand, favored a mahogany wand. eleven inches. pliable. A little more power and excellent for transfiguration. well, I say your father favored 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 every customer that day. though ollivader knew every wand's favorite, he cocould not choose as your 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 parse wands as stored as possible.

Please notice: One wand can only be sold to one wizard, and one wizard can only buy one wand, too.
 
Inputthere 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)
 
Outputonly one integer, shows how does wands Ollivander can handle.
 
Sample Input
3 43 1 2 31 11 10
 
Sample output
2HintHint 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.
 
Sourcegardon-dygg contest 1
Recommendjgshining | we have carefully selected several similar problems for you: 1528 1507 1287 3338


The question is so long. What is useful is the last paragraph. Tell you the right Wizards of your magic wand, and then let you find the maximum number of wizards you can do.

The obvious binary match. For example, if the magic wand I is suitable for the Wizards J, connect a side from J to I and find the maximum match.

#include <map>  #include <set>  #include <list>  #include <stack>  #include <queue>  #include <vector>  #include <cstdio>  #include <cmath>  #include <cstring>  #include <iostream>  #include <algorithm>    using namespace std;const int N = 110;int mark[N];bool vis[N];struct node{int next;int to;}edge[N * N];int head[N];int tot;void addedge(int from, int to){edge[tot].to = to;edge[tot].next = head[from];head[from] = tot++;}bool dfs(int u){for (int i = head[u]; ~i; i = edge[i].next){int v = edge[i].to;if (!vis[v]){vis[v] = 1;if (mark[v] == -1 || dfs(mark[v])){mark[v] = u;return true;}}}return false;}int hungary(int n){int ans = 0;memset ( mark, -1, sizeof(mark) );for (int i = 1; i <= n; i++){memset (vis, 0, sizeof(vis) );if (dfs(i)){ans++;}}return ans;}int main(){int n, m;while (~scanf("%d%d", &n, &m)){memset (head, -1, sizeof(head) );tot = 0;int k, j;for (int i = 1; i <= m; i++){scanf("%d", &k);while (k--){scanf("%d", &j);addedge(j, i);}}int ans = hungary(n);printf("%d\n", ans);}return 0;}


 

Hdu1179 -- ollivanders: makers of fine wands since 382 BC.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.