【HD 1179】 Makers of Fine Wands( 水 二分图 )

来源:互联网 发布:乐清知临寄宿学校招聘 编辑:程序博客网 时间:2024/05/22 09:40

本来是一道没什么好说的水题,写了杖匹配人之后又想写人匹配杖,结果出问题了,事实证明手打模板比照搬模板要来的好,写每一次都有新的错误,,,
一个是边界值问题,还是从1开始比较好,可能是杖匹配人的测试数据比较水,从0开始也1A了,就是换了人匹配杖就不行,还得规范写。
重新手打了一遍之后,怎么改都不会WA了,见具体代码见2.3.

————————————————————————————————————————————

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): 1569 Accepted Submission(s): 867

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

HintHint
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.

Source
Gardon-DYGG Contest 1

Recommend
JGShining | We have carefully selected several similar problems for you: 1054 1528 1068 1565 1507

————————————————————————————————————————————

1

第一次侥幸水过的代码

杖 匹配 人#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespace std;const int  MAX_N = 101;vector<int>map[MAX_N];int vis[MAX_N];//int Zy[MAX_N];  因为魔杖是按照顺序排序,所以可以不用标记。但标记了也不会错。 int Ry[MAX_N];bool find( int u ){    for( int i=0; i<map[u].size(); i++ )    {        int v = map[u][i];  // 魔杖与不同人的关系     //  printf("%d \n",v);        if( !vis[v] )        {            vis[v] = 1;            if( Ry[v] == -1 || find(Ry[v]))            {                Ry[v] = u;                //Zy[u] = v;                return true;            }        }    }    return false;}int main(){    int n,m;    while( ~scanf("%d%d",&n,&m) )//人 杖     {        //memset(Zy,-1,sizeof(Zy));        memset(Ry,-1,sizeof(Ry));        for( int i=0; i<m; i++ )        {            map[i].clear();        }        for( int i=0; i<m; i++ )        {            int num,a;            scanf("%d",&num);            for( int j=0; j<num; j++ )            {                scanf("%d",&a);                map[i].push_back(a); // 存的是 魔杖 -> 人 的关系             }        }        int cnt =0;        for( int i=0; i<m; i++ ) // 检查 每根魔杖         {            //if( Zy[i] == -1 ){                memset(vis,0,sizeof(vis));                if( find(i) )cnt++;        //  }        }        printf("%d\n",cnt);    }    return 0;} 

——————————————————————————————————————————

2

人匹配杖#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespace std;const int MAX_N = 101;vector<int>edge[MAX_N];int vis[MAX_N];int Ky[MAX_N];bool find( int u ){    int v;    for( int i=0; i<edge[u].size(); i++ )    {        v = edge[u][i];         //printf("%d \n",v);        if( !vis[v] ){            vis[v] = 1;            if( Ky[v]== -1 || find(Ky[v]) ){                Ky[v] = u;                return true;            }        }    }    return false;}int main(){    int r,g;    while( ~scanf("%d%d",&r,&g) )    {        for( int i=1; i<=r; i++ )        {            edge[i].clear();        }        memset(Ky,-1,sizeof(Ky));        for( int i=1; i<=g; i++ )        {            int num,a;            scanf("%d",&num);            for( int j=1; j<=num; j++ )            {                scanf("%d",&a);                edge[a].push_back(i);            }        }        int cnt =0;        for( int i=1; i<=r; i++ )        {            memset(vis,0,sizeof(vis));            if( find(i) )cnt++;        }        printf("%d\n",cnt);    }    return 0;}

3.

杖匹配人#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespace std;const int MAX_N = 101;vector<int>edge[MAX_N];int vis[MAX_N];int Ky[MAX_N];bool find( int u ){    int v;    for( int i=0; i<edge[u].size(); i++ )    {        v = edge[u][i];         //printf("%d \n",v);        if( !vis[v] ){            vis[v] = 1;            if( Ky[v]== -1 || find(Ky[v]) ){                Ky[v] = u;                return true;            }        }    }    return false;}int main(){    int r,g;    while( ~scanf("%d%d",&r,&g) )    {        for( int i=1; i<=g; i++ )        {            edge[i].clear();        }        memset(Ky,-1,sizeof(Ky));        for( int i=1; i<=g; i++ )        {            int num,a;            scanf("%d",&num);            for( int j=1; j<=num; j++ )            {                scanf("%d",&a);                edge[i].push_back(a);            }        }        int cnt =0;        for( int i=1; i<=g; i++ )        {            memset(vis,0,sizeof(vis));            if( find(i) )cnt++;        }        printf("%d\n",cnt);    }    return 0;}
0 0
原创粉丝点击