POJ 2289-Jamie's Contact Groups binary + Multi-matching of bipartite graphs

Source: Internet
Author: User

Question:

To group the address book... each person can only be assigned to one of its specific groups. Ask how many groups can be minimized ..

Question:

At the beginning, I got a wrong question. I thought it was necessary to minimize the group difference between the largest group and the smallest group. Then I fell down...

By the question, it is still very simple... the capacity of the binary enumeration... and then the multi-matching judgment of the Binary Graph...

Program:

#include<iostream>#include<stdio.h>#include<algorithm>#include<cmath>#include<stack>#include<string.h>#include<queue>#define ll long long#define esp 1e-5#define MAXN 1006#define MAXM 5000000#define oo 100000007using namespace std;int n,m,num[MAXN],line[MAXN][MAXN];bool g[MAXN][MAXN],used[MAXN];bool dfs(int x,int C){       for (int i=1;i<=m;i++)          if (g[x][i] && !used[i])          {                  used[i]=true;                  if (num[i]<C)                  {                        line[i][++num[i]]=x;                        return true;                  }                  for (int j=1;j<=num[i];j++)                     if (dfs(line[i][j],C))                     {                             line[i][j]=x;                             return true;                     }          }       return false;}bool getmax(int C){       memset(num,0,sizeof(num));       for (int i=1;i<=n;i++)       {              memset(used,false,sizeof(used));              if (!dfs(i,C)) return false;       }              return true;}int main()  {                  int i,x;       char c;        while (~scanf("%d%d",&n,&m) && n)       {                  memset(g,false,sizeof(g));               for (i=1;i<=n;i++)                {                      do { c=getchar(); }while (c<'0' || c>'9');                      while (1)                      {                             x=0;                             while (c>='0' && c<='9')                             {                                    x=x*10+c-'0';                                    c=getchar();                             }                             g[i][x+1]=true;                             if (c=='\n') break;                               do { c=getchar(); }while (c==' ');                             if (c=='\n') break;                                               }                                     }               int l=0,r=1005,mid;                while (r-l>1)               {                       mid=r+l>>1;                       if (!getmax(mid)) l=mid;                                    else r=mid;               }               printf("%d\n",r);       }       return 0;  }  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.