"Hdoj" 1914 the Stable Marriage problem

Source: Internet
Author: User

Stable marriage problem, Gale-shapley algorithm can be solved.

1 /*1914*/2#include <iostream>3#include <sstream>4#include <string>5#include <map>6#include <queue>7#include <Set>8#include <stack>9#include <vector>Ten#include <deque> One#include <algorithm> A#include <cstdio> -#include <cmath> -#include <ctime> the#include <cstring> -#include <climits> -#include <cctype> -#include <cassert> +#include <functional> -#include <iterator> +#include <iomanip> A using namespacestd; at //#pragma COMMENT (linker, "/stack:102400000,1024000") -  - #defineSTI set<int> - #defineStpii Set<pair<int, int> > - #defineMpii map<int,int> - #defineVI vector<int> in #definePII pair<int,int> - #defineVpii vector<pair<int,int> > to #defineRep (I, A, n) for (int i=a;i<n;++i) + #definePer (i, a, n) for (int i=n-1;i>=a;--i) - #defineCLR Clear the #definePB Push_back * #defineMP Make_pair $ #defineFIR FirstPanax Notoginseng #defineSEC Second - #defineAll (x) (x). Begin (), (x). End () the #defineSZ (x) ((int) (x). Size ()) + #defineLson L, Mid, rt<<1 A #defineRson mid+1, R, rt<<1|1 the  + Const intMAXN = -; - Chars[ +]; $ Charwname[ +]; $ intPREF[MAXN][MAXN]; - intORDER[MAXN][MAXN]; - intNXT[MAXN]; the intFUTURE_HUSBAND[MAXN]; - intFUTURE_WIFE[MAXN];Wuyiqueue<int>Q; the intN; - intid[ -]; Wu  - voidEngageintManintwoman) { About     intm =Future_husband[woman]; $      -     if(m) { -FUTURE_WIFE[M] =0; - Q.push (m); A     } +      theFuture_husband[woman] =Man ; -Future_wife[man] =woman; $ } the  the voidsolve () { the      while(!Q.empty ()) { the         intMan =Q.front (); - Q.pop (); in         intWoman = pref[man][nxt[man]++]; the         if(!Future_husband[woman]) { the Engage (man, woman); About}Else if(Order[woman][man] <Order[woman][future_husband[woman]]) { the Engage (man, woman); the}Else { the Q.push (man); +         } -     } the     BayiRep (I,'a','Z'+1) { the         if(!Id[i]) the             Continue; -printf"%c%c\n", I, Wname[future_wife[id[i]]); -     } the } the  the intMain () { theIos::sync_with_stdio (false); - #ifndef Online_judge theFreopen ("data.in","R", stdin); theFreopen ("Data.out","W", stdout); the     #endif94      the     intT; the     intNLC, Nuc; the     98scanf"%d", &t); About      while(t--) { -scanf"%d", &n);101memset (ID,0,sizeof(ID));102NLC = NUC =0;103Rep (I,0, n+N) {104scanf"%s", s); the             if(Islower (s[0])) {106id[s[0]] = ++NLC;107}Else {108id[s[0]] = ++Nuc;109WNAME[NUC] = s[0]; the             }111         } the         113          while(!q.empty ()) the Q.pop (); the          the         intlid, uid;117         118Rep (I,0, N) {119scanf"%s", s); -Lid = id[s[0]];121Rep (J,1, n+1) {122UID = id[s[1+J]];123PREF[LID][J] =uid;124             } theNxt[lid] =1;126Future_wife[lid] =0;127 Q.push (lid); -         }129          theRep (I,0, N) {131scanf"%s", s); theUID = id[s[0]];133Rep (J,1, n+1) {134Lid = id[s[1+J]];135Order[uid][lid] =J;136             }137FUTURE_HUSBAND[UID] =0;138         }139 solve (); $         if(t)141Putchar ('\ n');142     }143     144 #ifndef Online_judge145printf"Time =%d.\n", (int) clock ());146     #endif147     148     return 0;149}

"Hdoj" 1914 the Stable Marriage problem

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.