http://www.elijahqi.win/2017/12/17/poj2289-jamies-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
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
题意:我每个联系人都可以放到一组里 然后给定一些放置的方案 最后求一种分配方式 使得那个联系人最多的群组 里的联系人最少
那么我首先可以二分一个答案 然后我把这个二分的答案去建群组和汇点的边权 然后 我每个人都从源点建边权为1的边 跑一遍最大流 看我能否满流的 一直去二分这个答案即可
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define inf 0x3f3f3f3f
#define N 2000
using namespace std;
struct node{
int x,y,next,z;
}data[N*N];
int qq[N],h[N],level[N],T,n,m,num,w[N*N];
inline void insert1(int x,int y,int z){
data[++num].y=y;data[num].next=h[x];data[num].z=z;h[x]=num;data[num].x=x;
data[++num].y=x;data[num].next=h[y];data[num].z=0;h[y]=num;data[num].x=y;
}
inline bool bfs(){
memset(level,0,sizeof(level));level[0]=1;queue<int>q;q.push(0);
while(!q.empty()){
int x=q.front();q.pop();
for (int i=h[x];i;i=data[i].next){
int y=data[i].y,z=data[i].z;
if(level[y]||!z) continue;level[y]=level[x]+1;q.push(y);if(y==T) return 1;
}
}return 0;
}
inline int dfs(int x,int s){
if (x==T) return s;int ss=s;
for (int i=h[x];i;i=data[i].next){
int y=data[i].y,z=data[i].z;
if(level[x]+1==level[y]&&z){
int xx=dfs(y,min(s,z));if(!xx) level[y]=0;
s-=xx;data[i].z-=xx;data[i^1].z+=xx;if (!s) return ss;
}
}return ss-s;
}
int cnt;
inline bool check(int x){
for (int i=2;i<=num;++i) data[i].z=w[i];
for (int i=1;i<=cnt;++i) data[qq[i]].z=x,data[qq[i]^1].z=0;
int ans=0;while(bfs()) ans+=dfs(0,inf);
if (ans==n) return 1;else return 0;
}
int main(){
freopen("poj2289.in","r",stdin);
while(1){
scanf("%d%d",&n,&m);char s[110];T=n+m+1;memset(w,0,sizeof(w));
if(!n&&!m) break;num=1;memset(h,0,sizeof(h));cnt=0;
for (int i=1;i<=n;++i){
insert1(0,i,1);w[num^1]=1;
scanf("%s",s+1);int x;
while(1) {
scanf("%d",&x);x+=1;insert1(i,n+x,1),w[num^1]=1;s[0]=getchar();if (s[0]=='\n')break;
}
}
for (int i=1;i<=m;++i)insert1(i+n,T,inf),qq[++cnt]=num^1;
int l=1,r=n;
while(l<=r){
int mid=l+r>>1;
if (check(mid)) r=mid-1;else l=mid+1;
}printf("%d\n",l);
}
return 0;
}