Pku3487 (marital stability issues)

Source: Internet
Author: User

Http: // 162.105.81.212/judgeonline/problem? Id = 3487

If you don't understand the meaning of the question, read my previous blog. The following describes the related algorithms: refer to the algorithm of Niu Ren;

 

Gale-Shapley Algorithm

While there is a man m who is free and has not been married to every woman

Choose this man m
Make w the highest rank of Women in the priority list of M who have not yet been married
If W is free
(M, W) Changes to appointment status
Else W is currently dating M1
If W prefers M1 rather than m
M keep free
Else W prefers m rather than M1
(M, W) Changes to appointment status
M1 becomes free
Endif
Endif
Endwhile

# Include <iostream> <br/> # include <queue> <br/> using namespace STD; <br/> int MAN [30] [30], WOMAN [30] [30]; <br/> int guple [30], MMP re [30]; <br/> int A [30], N; <br/> queue <int> q; <br/> void gale_shapley () <br/> {<br/> memset (guple, 0, sizeof (guple )); <br/> memset (matrix,-1, sizeof (matrix); <br/> int I, PM, PF; <br/> while (! Q. empty () <br/>{< br/> PM = Q. front (); <br/> q. pop (); <br/> pF = WOMAN [PM] [guple [PM]; <br/> guple [PM] ++; <br/> If (MMP re [pf] <0) // PF is free (PM, PF) and changes to appointment status <br/> MMP re [pf] = PM; <br/> else if (MAN [pf] [MMP re [pf] <MAN [pf] [PM]) // pf prefers PM1, PM keep free <br/> q. push (PM); <br/> else // pf prefers pm, instead of PM1. (PM, Pf) Changes to appointment status; <br/>{< br/> q. push (Matrix [pf]); <br/> matrix [pf] = PM; <br/>}< br/> for (I = 0; I <26; I ++) <br/> If (MMP re [I] >-1) guple [matrix [I] = I; <br/> for (I = 0; I <n; I ++) <br/> printf ("% C/N", a [I] + 'A', guple [A [I] + 'A '); <br/> puts (""); <br/>}< br/> int main () <br/>{< br/> int T, I, J; <br/> char CH, STR [30]; <br/> scanf ("% d", & T); <br/> while (t --) <br/>{< br/> scanf ("% d", & N); <br/> while (! Q. empty () <br/> q. pop (); <br/> for (I = 0; I <n; I ++) <br/> {<br/> scanf ("% C ", & Ch); // note that there is a space reserved before it, which is repeated in scanf ("% C ..) <br/> A [I] = CH-'A'; <br/> q. push (CH-'A'); <br/>}< br/> sort (A, A + n); <br/> for (I = 0; I <n; I ++) <br/> scanf ("% C", & Ch); <br/> for (I = 0; I <N; I ++) <br/>{< br/> scanf ("% s", STR); <br/> for (j = 0; j <N; j ++) <br/> WOMAN [STR [0]-'a'] [J] = STR [J + 2]-'A '; <br/>}< br/> for (I = 0; I <n; I ++) <br/> {<br/> scanf ("% s ", str); <br/> for (j = 0; j <n; j ++) <br/> MAN [STR [0]-'a'] [STR [J + 2]-'a'] = J; // pay attention to the serial number; <br/>}</P> <p> gale_shapley (); <br/> // for (I = 0; I <n; I ++) <br/> // printf ("% C/N", a [I] + 'A', womanpre [A [I] + 'A '); <br/>}< br/> return 0; <br/>}< br/> 

 

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.