POJ2289-Jamie's Contact Groups(二分图多重匹配)
2017-03-13 21:49
615 查看
Jamie's Contact Groups
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
Sample Output
Source
Shanghai 2004
题意:有n个人,m个分组,每个人可以分配到一些组别,问如何分能使得人数最多的组别在所有分法中最少
解题思路:二分+二分图多重匹配
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
#include <bitset>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
int nx,ny;
int y[1005][505];
int visit[1005];
int sumy[1005];
int s[1005],nt[500009],e[500009];
bool path(int u,int sum)
{
for(int i=s[u];~i;i=nt[i])
{
int ee=e[i];
if(!visit[ee])
{
visit[ee]=1;
if(sumy[ee]<sum)
{
y[ee][sumy[ee]++]=u;
return 1;
}
for(int j=0;j<sumy[ee];j++)
{
if(path(y[ee][j],sum))
{
y[ee][j]=u;
return 1;
}
}
}
}
return 0;
}
int MaxMatch(int sum)
{
int ans=0;
memset(sumy,0,sizeof sumy);
for(int i=0;i<nx;i++)
{
memset(visit,0,sizeof visit);
if(path(i,sum)) ans++;
else break;
}
return ans;
}
int main()
{
while(~scanf("%d %d",&nx,&ny)&&(nx+ny))
{
memset(s,-1,sizeof s);
int cnt=1,x;
char ch[20],s1;
for(int i=0; i<nx; i++)
{
scanf("%s",ch);
while(scanf("%d%c",&x,&s1)==2)
{
nt[cnt]=s[i],s[i]=cnt,e[cnt++]=x;
if(s1=='\n') break;
}
}
int l=1,r=nx,ans;
while(l<=r)
{
int mid=(l+r)>>1;
if(MaxMatch(mid)>=nx) {ans=mid;r=mid-1;}
else l=mid+1;
}
printf("%d\n",ans);
}
return 0;
}
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 7582 | Accepted: 2551 |
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
Shanghai 2004
题意:有n个人,m个分组,每个人可以分配到一些组别,问如何分能使得人数最多的组别在所有分法中最少
解题思路:二分+二分图多重匹配
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
#include <bitset>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
int nx,ny;
int y[1005][505];
int visit[1005];
int sumy[1005];
int s[1005],nt[500009],e[500009];
bool path(int u,int sum)
{
for(int i=s[u];~i;i=nt[i])
{
int ee=e[i];
if(!visit[ee])
{
visit[ee]=1;
if(sumy[ee]<sum)
{
y[ee][sumy[ee]++]=u;
return 1;
}
for(int j=0;j<sumy[ee];j++)
{
if(path(y[ee][j],sum))
{
y[ee][j]=u;
return 1;
}
}
}
}
return 0;
}
int MaxMatch(int sum)
{
int ans=0;
memset(sumy,0,sizeof sumy);
for(int i=0;i<nx;i++)
{
memset(visit,0,sizeof visit);
if(path(i,sum)) ans++;
else break;
}
return ans;
}
int main()
{
while(~scanf("%d %d",&nx,&ny)&&(nx+ny))
{
memset(s,-1,sizeof s);
int cnt=1,x;
char ch[20],s1;
for(int i=0; i<nx; i++)
{
scanf("%s",ch);
while(scanf("%d%c",&x,&s1)==2)
{
nt[cnt]=s[i],s[i]=cnt,e[cnt++]=x;
if(s1=='\n') break;
}
}
int l=1,r=nx,ans;
while(l<=r)
{
int mid=(l+r)>>1;
if(MaxMatch(mid)>=nx) {ans=mid;r=mid-1;}
else l=mid+1;
}
printf("%d\n",ans);
}
return 0;
}
相关文章推荐
- POJ 2289 Jamie's Contact Groups (二分答案+二分图的多重匹配)
- POJ2289 Jamie's Contact Groups(二分图多重匹配)
- 【二分图多重匹配 && Dinic】POJ - 2289 Jamie's Contact Groups
- POJ -2289 -Jamie's Contact Groups (二分图多重匹配)
- poj2289--Jamie's Contact Groups(二分多重匹配)
- POJ 2289 Jamie’s Contact Groups-二分匹配&多重匹配
- Poj 2289 Jamie's Contact Groups【二分+多重匹配】
- POJ2289 Jamie's Contact Groups(二分图多重匹配+二分)
- POJ 2289——Jamie's Contact Groups——————【多重匹配、二分枚举匹配次数】
- hdu 1669 Jamie's Contact Groups(二分+二分图最大多重匹配)
- POJ 2289 Jamie's Contact Groups 多重匹配+二分
- POJ 2289 Jamie's Contact Groups (二分+匹配/网络流)
- POJ2289 Jamie's Contact Groups 二分图多重匹配
- Jamie's Contact Groups(二分图多重匹配+二分)(网络流)
- poj2289 Jamie's Contact Groups(二分答案+最大流)
- POJ 2289 Jamie's Contact Groups (二分+dinic)
- Jamie's Contact Groups POJ - 2289
- Poj 2289 Jamie's Contact Groups 【最大流Dinic+二分】
- HDU1669 Jamie's Contact Groups 二分法+二分图的多重匹配
- POJ2289--Jamie's Contact Groups