Poj 2289 Jamie's Contact Groups 【最大流Dinic+二分】

Jamie's Contact Groups

Time Limit: 7000MS

 

Memory Limit: 65536K

Total Submissions: 7393

 

Accepted: 2473

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

Shanghai 2004

 

题目大意:有n个人,将被分成m组,每个人都有一些个能去的组,问最后分配出的集合中,最大规模的那个组最少有多少人。


思路:


1、二分查找枚举当前mid值,表示最大规模的那个组最多有mid个人。


2、对于当前值mid,建图如下:
①建立超级源点S.将其连入各个人,其权值设定为1,表示每个人只能进一个组。

②建立超级汇点T,将各个组节点连入汇点T,其权值设定为mid,表示每个组最多有mid个人。

③将每个人连入其能够去的组中,权值设定为1。


3、建好图后跑最大流,如果最大流==n,那么当前mid值就是一个可行解,记录下来,并且继续二分,直到不能二分为止,输出记录下来的最后一个可行解、


Ac代码:

#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
struct node
{
    int from;
    int to;
    int next;
    int w;
}e[2000000];
int n,m,k,ss,tt,cont;
int divv[50000];
char name[5000];
vector<int >mp[5000];
int cur[50000];
int head[50000];
void add(int from,int to,int w)
{
    e[cont].from=from;
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
void getmap(int mid)
{
    ss=n+m+1;
    tt=ss+1;
    cont=0;
    memset(head,-1,sizeof(head));
    for(int i=1;i<=n;i++)
    {
        add(ss,i,1);
        add(i,ss,0);
    }
    for(int i=1;i<=m;i++)
    {
        add(i+n,tt,mid);
        add(tt,i+n,0);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<mp[i].size();j++)
        {
            int v=mp[i][j]+1;
            add(i,v+n,1);
            add(v+n,i,0);
        }
    }
}
int makedivv()
{
    queue<int >s;
    s.push(ss);
    memset(divv,0,sizeof(divv));
    divv[ss]=1;
    while(!s.empty())
    {
        int u=s.front();
        if(u==tt)return 1;
        s.pop();
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].to;
            int w=e[i].w;
            if(w&&divv[v]==0)
            {
                divv[v]=divv[u]+1;
                s.push(v);
            }
        }
    }
    return 0;
}
int Dfs(int u,int maxflow,int tt)
{
    if(u==tt)return maxflow;
    int ret=0;
    for(int &i=cur[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        int w=e[i].w;
        if(w&&divv[v]==divv[u]+1)
        {
            int f=Dfs(v,min(maxflow-ret,w),tt);
            e[i].w-=f;
            e[i^1].w+=f;
            ret+=f;
            if(ret==maxflow)return ret;
        }
    }
    return ret;
}
int Dinic(int mid)
{
    getmap(mid);
    int ans=0;
    while(makedivv()==1)
    {
        memcpy(cur,head,sizeof(head));
        ans+=Dfs(ss,INF,tt);
    }
    if(ans==n)return 1;
    else return 0;
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        if(n==0&&m==0)break;
        for(int i=1;i<=n;i++)mp[i].clear();
        for(int i=1;i<=n;i++)
        {
            scanf("%s",name);
            while(1)
            {
                int v;
                scanf("%d",&v);
                mp[i].push_back(v);
                if ( getchar() == '\n' ) break;
            }
        }
        int ans=-1;
        int mid;
        int l=0;
        int r=100000;
        while(r-l>=0)
        {
            mid=(l+r)/2;
            if(Dinic(mid)==1)
            {
                ans=mid;
                r=mid-1;
            }
            else
            {
                l=mid+1;
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值