二分 Jamie's Contact Groups poj 2289

36人阅读 评论(0) 收藏 举报
分类:

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 2289--Jamie's Contact Groups【二分图多重匹配问题 &&二分查找最大值的最小化 && 最大流求解】

Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 6902   Acc...
  • hpuhjh
  • hpuhjh
  • 2015-08-27 10:20:03
  • 1018

POJ-2289-Jamie's Contact Groups

题目大意是说有一个电话本,给出它可能的分组,然后要求你求出分组组内成员最大值的最小值 思路: 最大值最小值用二分答案,然后就每次建图跑网络流即可~ 代码: #include #include ...
  • z309241990
  • z309241990
  • 2013-08-03 13:13:59
  • 594

Jamie's Contact Groups(二分图多重匹配+二分)(网络流)

Jamie's Contact Groups Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u...
  • u014665013
  • u014665013
  • 2016-05-08 11:47:01
  • 1147

UValive3268 Jamie's Contact Groups(二分+最大流)

题目地址:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&...
  • u013013910
  • u013013910
  • 2014-06-29 23:15:39
  • 1031

Jamie's Contact Groups

二分 + 多值匹配  思路:一对多的二分图的多重匹配。二分图的多重匹配算法的实现类似于匈牙利算法,对于集合C中的元素xi,找到一个与其相连的元素yi后,检查匈牙利算法的两个条件是否成立,若yi未...
  • u013570474
  • u013570474
  • 2015-03-27 23:16:53
  • 246

POJ 2289 Jamie’s Contact Groups-二分匹配&多重匹配

思路:二分匹配的多重匹配,和普通的匹配在于每个右侧集合的点可以匹配多个左侧集合的点,对于寻找增广路首先判断一个点是否匹配够上限,然后若够上限了,再枚举是否有增广路,然后此题就是二分出一个上限,然后判断...
  • SSimpLe_Y
  • SSimpLe_Y
  • 2016-08-02 23:11:10
  • 575

UVALive - 3268 Jamie's Contact Groups(二分+最大流)

题目大意:有n个人和m个组,一个人可能属于多个组,现在请你从某些组中去掉几个人,使得每个人都只属于一个组,且人数最多的组中人员数目达到最小解题思路:最大值最小化,二分 建图就比较简单了,二分主要二分...
  • L123012013048
  • L123012013048
  • 2015-08-25 00:33:55
  • 346

UVA 1345 - Jamie's Contact Groups(二分+最大流)

UVA 1345 - Jamie's Contact Groups 题目链接 题意:给定一些人,每个人有一个分组,现在要每个人选一个分组,使得所有分组中最大的人数最小,问这个最小值是多少 ...
  • u011217342
  • u011217342
  • 2014-09-18 18:34:31
  • 1080

POJ2289 Jamie's Contact Groups 二分图多重匹配

Jamie's Contact Groups Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: ...
  • zy704599894
  • zy704599894
  • 2016-08-03 10:53:51
  • 151

poj 2289 Jamie's Contact Groups 二分图多重匹配

#include #include #include #include using namespace std; const int N=2048; const int inf=1
  • xinag578
  • xinag578
  • 2015-11-23 13:15:22
  • 200
    个人资料
    持之以恒
    等级:
    访问量: 1413
    积分: 652
    排名: 7万+
    文章存档
    最新评论