hdu1669+ two-part multiple match + two points

Source: Internet
Author: User

n individuals are divided into m-groups to find the minimum number of people in the group.

Everyone must match only one group, but a group can match many people and therefore belong to multiple matches.

We set a limit that represents the maximum number of people each group can hold. When DFS (U) looks for a match for u, if a group of VV numbers is less than limit, you can match the U and VV, and VV already matches the number of people +1. Otherwise, when the number of people has reached limit, we do DFS for each match of VV, that is, looking for augmentation, if we can find, modify this match, that is, VV and U match.

Then we can have two points limit, when the U set (People's collection) can find a match, then reduce the limit value, otherwise as long as there is a point to find a match, increase the limit value.

Multi-match and binary matching the Hungarian algorithm has a similar notation, except that a CNT array indicates the number of people v has matched, and the linker array also adds one dimension.

1#include <cstdio>2#include <cstring>3#include <vector>4 using namespacestd;5 intn,m;6vector<int> v[1100];7 intlinker[1100][1100],vis[1100],cnt[1100];8 intlimit;9 BOOLDfsintu)Ten { One     inti,j; A      for(i=0; I<v[u].size (); i++) -     { -         intvv=V[u][i]; the         if(!VIS[VV]) -         { -vis[vv]=1; -             if(cnt[vv]<limit) +             { -linker[vv][cnt[vv]++]=u; +                 return true; A             } at             Else -             { -                  for(j=0; j<cnt[vv];j++) -                 { -                     if(Dfs (Linker[vv][j])) -                     { inlinker[vv][j]=u; -                         return true; to                     } +                 } -             } the         } *     } $     return false;Panax Notoginseng } - intMain () the { +     inti,j; A     Charstr[110000]; the      while(SCANF ("%d%d", &n,&m)! =EOF) +     { - GetChar (); $         if(n==0&&m==0) Break; $          for(i=0; i<n;i++) - v[i].clear (); -          for(i=0; i<n;i++) the         { - gets (str);Wuyi             intlen=strlen (str); thej=0; -Loop for(; j<len;j++) Wu             { -                 if(str[j]>='0'&&str[j]<='9') About                      Break; $             } -             intnum=0; -             BOOLJudge=false; -              for(; j<len;j++) A             { +                 if(str[j]>='0'&&str[j]<='9') the                 { -Judge=true; $num=num*Ten+ (str[j]-'0'); the                 } the                 Else  Break; the             } the             if(judge) v[i].push_back (num); -             if(J<len)GotoLoop; in         } the         intL=0, r=N; the          while(l<R) About         { thelimit= (L+R)/2; thememset (CNT,0,sizeof(CNT)); the              for(i=0; i<n;i++) +             { -memset (Vis,0,sizeof(Vis)); the                 if(!dfs (i)) Break;Bayi             } the             if(i>=n) r=limit; the             Elsel=limit+1; -         } -printf"%d\n", L); the     } the     return 0; the}

hdu1669+ two-part multiple match + two points

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.