二分 Jamie's Contact Groups poj 2289

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

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


#include <cstdio>
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;


const int MAX_D=2000+5;
const int MAX_B=2000000+5;
const int INF=1e9;


int tot;


int head[MAX_D];
int headt[MAX_D];
int iter[MAX_D];
int level[MAX_D];
struct node{
    int to;
    int cap;
    int rev;
    int next;
};
node edge[2*MAX_B];
node edget[2*MAX_B];




void add_edge(int a,int b,int c){


    edge[tot].to=b;
    edge[tot].cap=c;
    edge[tot].rev=tot+1;
    edge[tot].next=head[a];
    head[a]=tot++;


    edge[tot].to=a;
    edge[tot].cap=0;
    edge[tot].rev=tot-1;
    edge[tot].next=head[b];
    head[b]=tot++;


}




void bfs(int s){
    memset(level,-1,sizeof(level));
    queue<int> que;
    level[s]=0;
    que.push(s);
    while(!que.empty()){
        int v=que.front();que.pop();
        for(int i=head[v];i!=0;i=edge[i].next){
            node &e=edge[i];
            if(e.cap>0&&level[e.to]<0){
                level[e.to]=level[v]+1;
                que.push(e.to);
            }
        }
    }
}


int dfs(int v,int t,int f){
    if(v==t)return f;
    for(int &i=iter[v];i!=0;i=edge[i].next){
        node &e = edge[i];
        if(e.cap>0&&level[v]<level[e.to]){
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0){
                e.cap-=d;
                edge[e.rev].cap+=d;
                return d;
            }
        }
    }


    return 0;
}


int max_flow(int s,int t){
    int flow=0;
    while(1){
        bfs(s);
        if(level[t]<0)return flow;
        for(int i=0;i<MAX_D;i++){
            iter[i]=head[i];
        }
        int f;
        while((f=dfs(s,t,INF))>0){
            flow+=f;
        }
    }
}




void solve(){
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        if(n==0&&m==0)break;
        tot=1;
        memset(head,0,sizeof(head));
        for(int i=1;i<=n;i++){
            add_edge(1,i+2,1);
        }
        for(int i=1;i<=n;i++){
            char s[20];
            scanf("%s",s);
            while(getchar()!='\n'){
                int x;
                scanf("%d",&x);
                add_edge(i+2,x+1+n+2,1);
            }
        }

        int tott=tot;
        for(int i=1;i<n+m+5;i++){
            headt[i]=head[i];
        }
        for(int i=1;i<tot;i++){
            edget[i]=edge[i];
        }

        int left=0,right=n,ans=0;
        while(left<=right){
            int mid=(left+right)>>1;
            tot=tott;
            for(int i=1;i<n+m+5;i++){
                head[i]=headt[i];
            }
            for(int i=1;i<tot;i++){
                edge[i]=edget[i];
            }
            for(int i=1;i<=m;i++){
                add_edge(n+i+2,2,mid);
            }
            int w=max_flow(1,2);
            if(w==n){
                ans=mid;
                right=mid-1;
            }
            else{
                left=mid+1;
            }
        }
        printf("%d\n",ans);
    }
}


int main()
{
    solve();


    return 0;
}

阅读更多
个人分类: 网络流
上一篇poj 3281 Dining
下一篇POJ 2112 Optimal Milking 二分网络流
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭