Jamie's Contact Groups
Time Limit: 15000/7000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 225 Accepted Submission(s): 63
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
Recommend
xhd
题目很容易明白,我这里就不赘述了。区赛了还是刷图论,希望真心能给力点吧。这个题目的想法就是二分+二分图的多重匹配。下面我把自己的渣代码弄上来:
View Code
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> using namespace std; const int maxn=1010; const int maxm=510; int bmap[maxn][maxm],cy[maxm][maxn],bmask[maxm],vcy[maxm],n,m,limit; char s[2000]; bool findpath(int u) { for(int i=0;i<m;i++) { if(bmap[u][i]&&!bmask[i]) { bmask[i]=1; if(vcy[i]<limit) { cy[i][vcy[i]++]=u; return true; } for(int j=0;j<vcy[i];j++) { if(findpath(cy[i][j])) { cy[i][j]=u; return true; } } } } return false; } bool mulmatch() { memset(vcy,0,(m+5)*sizeof(int)); for(int i=0;i<n;i++) { memset(bmask,0,sizeof(bmask)); if(!findpath(i)) return false; } return true; } int main() { while(scanf("%d %d",&n,&m),n+m) { getchar(); memset(bmap,0,sizeof(bmap)); memset(cy,0,sizeof(cy)); memset(s,0,sizeof(s)); for(int i=0;i<n;i++) { gets(s); int len=strlen(s); int ret=0; int t; for(t=0;t<len;t++) { if((s[t]>='a'&&s[t]<='z')||(s[t]>='A'&&s[t]<='Z')) continue; else break; } t++; for(;t<len;t++) { if(s[t]>='0'&&s[t]<='9') { ret=ret*10+s[t]-'0'; if(t+1==len) bmap[i][ret]=1; } else if(s[t]==' ') { bmap[i][ret]=1; ret=0; } } memset(s,0,sizeof(s)); } int l=1,r=n+1,ans=0; while(l<r) { limit=(l+r)>>1; if(mulmatch()) ans=limit,r=limit; else l=limit+1; } printf("%d\n",ans); } return 0; }
明天晚上就要出发了。听人说是传说中的死亡赛区,所以心态也很好,基本上是去玩的,因为深知自己水平。谢谢!欢迎转载,提问,留言。