Poj3487 stable marriage matching

Source: Internet
Author: User

Stable marriage: There are n men and n women, each of whom has a ranking of their preferences for the opposite sex, representing their preferences, now I want to find a spouse for each man so that each person has a spouse of the opposite sex. If men's u and women's v are not spouse but prefer each other more than their current spouse, they are called unstable pairs. A stable marriage problem is to find a solution that does not include an unstable pair.
The algorithm is very simple, called the proposal-denial algorithm. Each man proposes a proposal to each lady from high to low as he prefers until a lady accepts him, if the current spouse proposed to her is better than her current spouse, the current spouse will be abandoned. Otherwise, the current spouse will be ignored until all people have a spouse. Interestingly, it seems that women have more options, but in fact the final result is man-optimal ).
First, describe the final matching stability. With the implementation of the algorithm, the spouse of each lady is getting better and better, and the spouse of each man is getting worse and worse. Therefore, if the men's u and women v-shaped are unstable, u must have been married to v, but was rejected. This indicates that v's spouse at that time is better than u, so the spouse after the algorithm is completed must be better than u, which is in conflict with the definition of the unstable pair. Similarly, we consider the last abandoned man and the lady who abandoned the man. It is not difficult to come to the conclusion that this algorithm must be terminated.
If a stable match matches men's I and women's j, (I, j) is a stable pair. For each male I, set all the stable pairs (I, j), among which I like best (I ), it can be proved that the algorithm provided here matches each man I with best (I. For all men, there will be no better results than this, but for women, there will be no worse results for them, therefore, this algorithm is optimal for men.
The algorithm must have a stable match, and the complexity is obviously O (n ^ 2), because each man considers each lady at most once, and the time complexity is O (1). Of course, some preprocessing is required to obtain this complexity.

Import java. util. arrays; import java. util. extends; public class stablemarry3487 {class boy {int stack [] = new int [27], top; int match; int get () {return stack [top ++];} class girl {int rank [] = new int [27], id; int stack [] = new int [30], top; void insert (int k) {stack [top ++] = k;} void match () {if (top = 0) return; // among all the boys who propose to you, select the favorite int min = 99, idx =-1; for (int I = 0; I <top; I ++) if (rank [stack [I] <Min) {idx = stack [I]; min = rank [idx];} // Add other boys to the single queue for (int I = 0; I <top; I ++) if (idx! = Stack [I]) queue [tail ++] = stack [I]; // notify the selected boys of a successful match and save it to the alternative queue bb [idx]. match = id; top = 1; stack [0] = idx;} consumer scan = new partition (System. in); int queue [] = new int [30], head, tail, n; boy bb [] = new boy [30]; girl gg [] = new girl [30]; void init () {n = scan. nextInt (); head = tail = 0; Arrays. fill (bb, null); Arrays. fill (gg, null); for (int I = 0; I <n; I ++) {String s = scan. next (); queue [tail ++] = s. charAt (0 )- 'A';} for (int I = 0; I <n; I ++) scan. next (); for (int I = 1; I <= n; I ++) {String s = scan. next (); int k = s. charAt (0)-'A'; bb [k] = new boy (); for (int j = 0; j <n; j ++) bb [k]. stack [j] = s. charAt (j + 2)-'A';} for (int I = 0; I <n; I ++) {String s = scan. next (); int k = s. charAt (0)-'A'; gg [k] = new girl (); gg [k]. id = k; for (int j = 0; j <n; j ++) gg [k]. rank [s. charAt (j + 2)-'a'] = j ;}} void work () {wh Ile (head! = Tail) {// single male proposes to a girl who does not reject herself and does not like most. for (int I = head; I <tail; I ++) {int k = queue [I]; int p = bb [k]. get (); gg [p]. insert (k);} head = tail = 0; for (int I = 0; I <26; I ++) if (gg [I]! = Null) gg [I]. match (); // among the boys who want to propose a proposal, select the favorite one and add others to the queue.} for (int I = 0; I <26; I ++) if (bb [I]! = Null) System. out. println (char) ('A' + I) + "" + (char) ('A' + bb [I]. match);} void run () {int cas = scan. nextInt (); while (cas --> 0) {init (); work (); System. out. println () ;}} public static void main (String [] args) {new stablemarry3487 (). run ();}}

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.