Poj3487 The Stable Marriage Problem (The most Stable Marriage)

Source: Internet
Author: User

The Stable Marriage ProblemTime Limit: 1000 MS Memory Limit: 65536 K Total Submissions: 1966 Accepted: 838 Description The stable marriage problem consists of matching members of two different sets according to the member's preferences for the other set's members. the input for our problem consists of: a set M of n males; a set F of n females; for each male and female we have a list of all the members Of the opposite gender in order of preference (from the most preferable to the least ). A marriage is a one-to-one mapping between males and females. A marriage is called stable, if there is no pair (m, f) such that f in F prefers m in M to her current partner and m prefers f over his current partner. the stable marriage A is called male-optimal if there is no other stable marriage B, where any male m Atches a female he prefers more than the one assigned in. given preferable lists of males and females, you must find the male-optimal stable marriage. input The first line gives you the number of tests. the first line of each test case contains integer n (0 <n <27 ). next line describes n male and n female names. male name is a lowercase letter, female name is an upper-case letter. then go n li Nes, that describe preferable lists for males. next n lines describe preferable lists for females. output For each test case find and print the pairs of the stable marriage, which is male-optimal. the pairs in each test case must be printed in lexicographical order of their male names as shown in sample output. output an empty line between test cases. sample Input 23a B c a B Ca: BACb: BACc: ACBA: acb B: bacC: cab3a B c a B Ca: ABCb: ABCc: BCAA: bacB: acbC: abcSample Output a AB Bc C a Bb Ac C There are n men n ladies, everyone has an evaluation value for all the opposite sex. A male expresses his confession to a female. If a female has no target, the male will be accepted, if a girl has an object, but her affection for this object is inferior to her affection for this person, she will abandon her current object and the person who expresses her feelings together, requires the most stable marital status. [Cpp] # include <stdio. h> # include <string. h >#include <iostream >#include <queue> using namespace std; int male [30], female [30]; int m [30] [30], fm [30] [30]; int vis [30]; int n; char t [60]; queue <int> q; void scanf_name () {char tmp [30]; gets (t); // cout <t <endl; for (int I = 0; I <n; I ++) {gets (tmp ); // cout <tmp <endl; int poit = tmp [0]-'A' + 1; q. push (poit); for (int j = 2; j <strlen (tmp); j ++) {int k = tmp [j]-'A' + 1; m [poit] [J-1] = k;} for (int I = 0; I <n; I ++) {// cout <"cin" <endl; gets (tmp); int poit = tmp [0]-'A' + 1; for (int j = 2; j <strlen (tmp); j ++) {int k = tmp [j]-'A' + 1; fm [poit] [k] = J-1 ;}}} void GaleShapley () {while (! Q. empty () {int poit = q. front (); // The poit men's q. pop (); // if (male [poit]! =-1) // continue; if (vis [poit]> n) continue; int I = ++ vis [poit]; // ladies I, the favorite position of poit men // vis [poit] ++; int fem = m [poit] [I]; // female I, the most favorite of poit men, is the fem. if (female [fem] =-1) // if no one is interested in the fem, {female [fem] = poit; male [poit] = fem; continue;} else {int temp = female [fem]; if (fm [fem] [temp]> fm [fem] [poit]) // if female prefers more people than she does for the current poit man, {female [fem] = poit; male [poit] = fem; male [temp] =-1; q. push (temp); continue;} Else {q. push (poit); continue ;}}} int main () {int s; scanf ("% d", & s); while (s --) {memset (vis, 0, sizeof (vis); memset (male,-1, sizeof (male); memset (female,-1, sizeof (female); while (! Q. empty () q. pop (); scanf ("% d", & n); getchar (); scanf_name (); GaleShapley (); int I = 1; while (I <= n) {if (male [I]! =-1) printf ("% c \ n", I + 'a'-1, male [I] + 'a'-1); I ++ ;} if (s) printf ("\ n") ;}return 0 ;}

Related Article

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.