POJ2289 二分图的多重匹配

Jamie's Contact Groups
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 5298   Accepted: 1682

Description

Jamie is a very popular girl and has quite a lot of friends, so she always keeps a very long contact list in her cell phone. The contact list has become so long that it often takes a long time for her to browse through the whole list to find a friend's number. As Jamie's best friend and a programming genius, you suggest that she group the contact list and minimize the size of the largest group, so that it will be easier for her to search for a friend's number among the groups. Jamie takes your advice and gives you her entire contact list containing her friends' names, the number of groups she wishes to have and what groups every friend could belong to. Your task is to write a program that takes the list and organizes it into groups such that each friend appears in only one of those groups and the size of the largest group is minimized.

Input

There will be at most 20 test cases. Ease case starts with a line containing two integers N and M. where N is the length of the contact list and M is the number of groups. N lines then follow. Each line contains a friend's name and the groups the friend could belong to. You can assume N is no more than 1000 and M is no more than 500. The names will contain alphabet letters only and will be no longer than 15 characters. No two friends have the same name. The group label is an integer between 0 and M - 1. After the last test case, there is a single line `0 0' that terminates the input.

Output

For each test case, output a line containing a single integer, the size of the largest contact group.

Sample Input

3 2
John 0 1
Rose 1
Mary 1
5 4
ACM 1 2 3
ICPC 0 1
Asian 0 2 3
Regional 1 2
ShangHai 0 2
0 0

Sample Output

2
2

题意:给定一个规模为n的名单,要将名单中的人归到m个组中,给出每个人可能的分组号,需要确定一种分配方案,是的最大规模的组最小。
思路:一对多的二分图的多重匹配。二分图的多重匹配算法的实现类似于匈牙利算法,对于集合C中的元素xi,找到一个与其相连的元素yi后,检查匈牙利算法的两个条件是否成立,若yi未被匹配,则将
xi,yi匹配。否则,如果与yi匹配的元素已经达到上限,那么在所有与yi匹配的元素中选择一个元素,检查是否能找到一条增广路径,如果能,则让出位置,让xi与yi匹配。
二分求出limit,知道找到可以构成多重匹配的最小限制limit,在main函数中二分搜索。
View Code
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #include<math.h>
 5 const int M=2010;
 6 int bmap[M][M];
 7 bool bmask[M];
 8 int nx,ny;
 9 int vcy[M];
10 int cy[M][M];
11 int limit;
12 bool findpath(int u)
13 {
14     int i,j;
15     for(i=0;i<ny;i++)
16     {
17         if(bmap[u][i]&&!bmask[i])
18         {
19             bmask[i]=1;
20             if(vcy[i]<limit)
21             {
22                 cy[i][vcy[i]++]=u;
23                 return 1;
24             }
25             for(j=0;j<vcy[i];j++)
26             {
27                 if(findpath(cy[i][j]))
28                 {
29                     cy[i][j]=u;
30                     return 1;
31                 }
32             }
33         }
34     }
35     return 0;
36 }
37 bool MulMatch()
38 {
39     memset(vcy,0,sizeof(vcy));
40     for(int i=0;i<nx;i++)
41     {
42         memset(bmask,0,sizeof(bmask));
43         if(!findpath(i)) return 0;
44     }
45     return 1;
46 }
47 int main()
48 {
49     char ch[2002];
50     int _left,_right,v;
51     while(scanf("%d%d",&nx,&ny)!=EOF)
52     {
53         getchar();
54         if(nx==0&&ny==0) break;
55         memset(bmap,0,sizeof(bmap));
56         for(int i=0;i<nx;i++)
57         {
58             gets(ch);
59             int len=strlen(ch);
60             for(int j=0;j<len;j++)
61             {
62                 if(ch[j]>='0'&&ch[j]<='9')
63                 {
64                     v=0;
65                     while(ch[j]>='0'&&ch[j]<='9')
66                     {
67                         v=v*10+ch[j++]-'0';
68                     }
69                     bmap[i][v]=1;
70                 }
71             }
72         }
73         _left=0,_right=nx;
74         while(_left<_right)
75         {
76             limit=(_left+_right)/2;
77             if(MulMatch()) _right=limit;
78             else _left=limit+1;
79         }
80         printf("%d\n",_right);
81     }
82     return 0;
83 }

 网络流算法(待写):二分依旧,枚举最大值,源点与每个人连容量为1的边,每个人与他可以在一起的组连一条边,然后将每个组与汇点连线,容量为枚举的最大值,如果满流就减小最大值,否则扩大最大值。

posted @ 2012-08-30 19:44  _sunshine  阅读(1545)  评论(0编辑  收藏  举报