POJ 2289 Jamie's Contact Groups 二分图多重匹配 难度:1

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

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
这是一对多的匹配,就是增广路改一改,改成num<limit时可以增广就行
如果group的匹配数小于当前要找的答案,那么就直接加入group的匹配集里,否则在匹配集里找可以增广的点修改
需要注意的是used数组,如果a->groupx->a就会无限调用,所以设置了一个used数组每次都清空,有点懒
#include <iostream>

#include <cstring>

#include <vector>

#include <sstream>

#include <string>

using namespace std;



const int maxn=1502;

int m,n;

int num[maxn],cmp[maxn][maxn],used[maxn],tot;

vector<int > G[maxn];



void printg(){

    for(int i=0;i<n;i++){

        for(int j=0;j<G[i].size();j++){

            int t=G[i][j];

            cout<<"G["<<i<<"]["<<j<<"]:"<<t<<" ";

        }

        cout<<endl;

    }

}







bool fnd(int s,int limit){

    for(int i=0;i<G[s].size();i++){

        int t=G[s][i];

        if(used[t])continue;

        used[t]=true;

        if(num[t]<limit){cmp[t][num[t]++]=s;return true;}

        for(int j=0;j<num[t];j++){

            if(fnd(cmp[t][j],limit)){

                cmp[t][j]=s;

                return true;

            }

        }

    }

    return false;

}



bool hungry(int limit){

    tot=0;

    memset(num,0,sizeof(num));

    for(int i=0;i<n;i++){

        memset(used,0,sizeof(used));

        if(fnd(i,limit))tot++;

    }

    if(tot==n)return true;

    return false;

}



int getans(int s,int e){

    int mid=(s+e)/2;

    if(hungry(mid)){

        return s==mid?mid:getans(s,mid);

    }

    return s==mid?e:getans(mid,e);

}





string buff,rbuff;

int main(){

    cin.tie(0);

    ios::sync_with_stdio(false);

    stringstream ss;

    while(cin>>n>>m&&(n||m)){

        getline(cin,rbuff);

        for(int i=0;i<n;i++){

            G[i].clear();

            getline(cin,rbuff);

            ss.clear();

            ss<<rbuff;

            ss>>buff;

            int t;

            while(ss>>t){

                t+=n;

                G[i].push_back(t);

            }

        }

        //printg();

        if(m==0)cout<<0<<endl;

        else {

            int ans=getans(n/m,n+1);

            cout<<ans<<endl;

        }

    }

    return 0;

}
View Code

 

你可能感兴趣的:(group)