Jamie's Contact Groups
Time Limit: 15000/7000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 552 Accepted Submission(s): 190
Problem 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
Source
题意:有n个人的信息,这些人被记录在m本记事本上,一个人可能出现在多本记事本上,现在想要将这些人在这m本记事本上出现次数有且只有一次,所以要在一些记事本上删掉一些人名,问删掉之后这m本记事本里面最多人名的记事本的最小值应该是多少?
在二分图最大匹配中,每个点(不管是X方点还是Y方点)最多只能和一条匹配边相关联,然而,我们经常遇到这种问题,即二分图匹配中一个点可以和多条匹配边相关联,但有上限,或者说,Li表示点i最多可以和多少条匹配边相关联。
具体到这个题,那么我们删完之后一个人可以对应多个记事本,所以我们可以去二分上限,得到最小的上限。
#include<iostream> #include<cstdio> #include<cstring> #include <algorithm> #include <math.h> using namespace std; const int N = 1005; int n,m,cap; char str[20]; int graph[N][N]; int linker[N][N],link[N]; bool vis[N]; bool dfs(int u){ ///多重匹配,从一端到多端进行匹配 for(int v=0;v<m;v++){ if(graph[u][v]&&!vis[v]){ vis[v] = true; if(link[v]<cap){ linker[v][link[v]++] = u; return 1; } for(int i=0;i<link[v];i++){ if(dfs(linker[v][i])){ linker[v][i] = u; return true; } } } } return false; } bool match(int mid){ cap = mid; memset(link,0,sizeof(link)); for(int i=0;i<n;i++){ memset(vis,false,sizeof(vis)); if(!dfs(i)) return false; ///找不到匹配点 } return true; } int main() { while(scanf("%d%d",&n,&m)!=EOF,n+m){ char c; int v; memset(graph,0,sizeof(graph)); getchar(); for(int i=0;i<n;i++){ scanf("%s",str); while(scanf("%c",&c)&&c!=' '){ scanf("%d",&v); graph[i][v] = 1; } } int l = 1,r = n,ans=n; while(l<=r){ int mid = (l+r)>>1; if(match(mid)){ ans = mid; r = mid-1; }else l = mid+1; } printf("%d ",ans); } return 0; }