首页 > 基础算法 > 枚举 > hdu 1669 Jamie’s Contact Groups -分治-[解题报告]
2013
12-21

hdu 1669 Jamie’s Contact Groups -分治-[解题报告]

Jamie’s Contact Groups

问题描述 :

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.

输入:

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.

输出:

For each test case, output a line containing a single integer, the size of the largest contact group.

样例输入:

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

样例输出:

2
2

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1669

思路:由于要求minimize the size of the largest group,由此我们想到二分枚举,然后每一次求一下多重匹配就可以了。

#include<iostream>
 #include<cstdio>
 #include<cstring>
 #include<algorithm>
 #include<vector>
 using namespace std;
 #define MAXN 1010
 #define MAXM 555
 vector<int>vet[MAXN];
 bool map[MAXN][MAXN];
 int Link[MAXM];//i当前的匹配数
 int vLink[MAXM][MAXN];//与i匹配的第j个匹配的数k
 bool mark[MAXM];//标记已匹配的
 int n,m,limit,ans;
 
 bool dfs(int u)
 {
     for(int i=0;i<vet[u].size();i++){
         int v=vet[u][i];
         if(!mark[v]){
             mark[v]=true;
             if(Link[v]<limit){
                 vLink[v][Link[v]]=u;
                 Link[v]++;
                 return true;
             }
             for(int j=0;j<Link[v];j++){
                 if(dfs(vLink[v][j])){
                     vLink[v][j]=u;
                     return true;
                 }
             }
         }
     }
     return false;
 }
 
 bool Match()
 {
     memset(Link,0,sizeof(Link));
     memset(vLink,0,sizeof(vLink));
     for(int i=0;i<n;i++){
         memset(mark,false,sizeof(mark));
         if(!dfs(i))return false;
     }
     return true;
 }
 
 int main()
 {
   //  freopen("1.txt","r",stdin);
     char name[22];
     int x;
     while(scanf("%d%d",&n,&m),(n+m)){
         for(int i=0;i<n;i++)vet[i].clear();
         for(int i=0;i<n;i++){
             scanf("%s",name);
             while(getchar()==' '){
                 scanf("%d",&x);
                 vet[i].push_back(x);
             }
         }
         int low=0,high=n;
         while(low<=high){
             limit=(low+high)>>1;
             if(Match()){
                 ans=limit;//保留
                 high=limit-1;
             }else
                 low=limit+1;
         }
         printf("%d\n",ans);
     }
     return 0;
 }

 

解题转自:http://www.cnblogs.com/wally/p/3141531.html