The choice of UVA 1175 ladies ' Choice (Stable marriage problem, GS algorithm)

Source: Internet
Author: User

Test instructions

Give each male's mind in the order of the goddess, give each woman's mind in the order of the male, that is, two n*n matrix, once any two non-partner of the male and female students feel that each other is better than the current partner, they will abandon the partner and together. To put an end to this phenomenon, ask each man who is the last companion to match.

Ideas:

How to feel test instructions a bit of a problem, the output is the first line of men with the woman, that is, the output of women!

Each man chooses the most favorite woman to propose, if the same woman no one Rob, then the temporary bride theft success, if need to rob, obviously women are like to pick long leg oba, dwarf melon automatically join the single dog queue continue to propose! When the goddess of a single dog is engaged, as long as the Goddess loves single dogs More, nothing can stop them, the goddess decisively abandon her fiance, fiance join the single dog queue. Until there are no single dogs on earth!

Complexity O (N2), each man will only to the N women to propose once, and women do not have the right to choose, only accept and reject. The famous Gale-shapley algorithm.

1#include <bits/stdc++.h>2 #defineMax (x, y) (() > (y)? (X): (Y))3 #defineMin (x, y) ((×) < (y)? (X): (Y))4 #definePII pair<int,int>5 #defineINF 0x7f7f7f7f6 #defineLL Long Long7 using namespacestd;8 Const intn=1010;9 Tendeque<int> Que[n];//in the mind of the goddess rank queue One intGirl[n], boy[n]; A intImp[n][n]; -  - intCalintN) the { -Memset (Girl,0,sizeof(Girl)); -deque<int>Single ; -      for(intI=1; i<=n; i++) Single.push_back (i); +  -      while(!single.empty ()) +     { A         intX=single.front (); Single.pop_front ();//Take out a single dog at         intOp=que[x].front (); Que[x].pop_front ();//Take out X's Crush object -  -         if(!girl[op]) Girl[op]=x,boy[x]=op;//Goddess First Love -         Else if(Imp[op][x] < Imp[op][girl[op])//Goddess abandoned incumbent -         { - Single.push_back (Girl[op]); ingirl[op]=x; -boy[x]=op; to         } +         ElseSingle.push_back (x);//single dog is rejected -     } the      for(intI=1; i<=n; i++) printf ("%d\n", Boy[i]); * } $ Panax Notoginseng  - intMain () the { +     //freopen ("Input.txt", "R", stdin); A     intT, N, B, j=0; theCin>>T; +      while(t--) -     { $         if(j) printf ("\ n"); $J + +; -scanf"%d", &n); -          for(intI=1; i<=n; i++) the         { - que[i].clear ();Wuyi              for(intj=1; j<=n; J + +) the             { -scanf"%d",&b); Wu Que[i].push_back (b); -             } About         } $          for(intI=1; i<=n; i++) -         { -              for(intj=1; j<=n; J + +) -             { Ascanf"%d",&b); +Imp[i][b]=j;//b for I, rank in the first few.  the             } -         } $  the cal (n); the     } the     return 0; the}
AC Code

  

The choice of UVA 1175 ladies ' Choice (Stable marriage problem, GS algorithm)

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.