poj3487[延迟认可算法]

来源:互联网 发布:男生挎包 知乎 编辑:程序博客网 时间:2024/05/06 00:26
The Stable Marriage Problem
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 1785 Accepted: 758

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 (mf) such that f ∈ F prefers m ∈ 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 matches a female he prefers more than the one assigned in A.

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 lines, 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:acbB:bacC:cab3a b c A B Ca:ABCb:ABCc:BCAA:bacB:acbC:abc

Sample Output

a Ab Bc Ca Bb Ac C

Source

Southeastern Europe 2007
======================================================================================================
稳定婚姻系统-延迟认可算法(Gale-Shapley算法)
就我理解这个算法就是在模拟,每次在左集合中取一个点,让它找一个最优的匹配点,然后如果替换了别的点,就把它入队,重复~,这个过程一定是可以终止的(可以证明~囧),而且按照这种方法找到的匹配一定是左侧最优的~
代码写的比较挫~:
program poj3487;const  MaxN = 30;var  order:  array['a'..'z'] of string;  weight: array['A'..'Z', 'a'..'z'] of longint;  partx:  array['a'..'z'] of char;  party:  array['A'..'Z'] of char;  n: longint;  que: array[0..30] of char;  top, last: longint;  procedure Push(x: char);  begin    last      := (last + 1) mod MaxN;    que[last] := x;  end;  function Empty: boolean;  begin    if top = last then      exit(True)    else      exit(False);  end;  function Pop: char;  begin    top := (top + 1) mod MaxN;    Pop := que[top];  end;  procedure Gale;  var    x, y: char;    i:    longint;  begin    for x := 'a' to 'z' do      partx[x] := '#';    for y := 'A' to 'Z' do      party[y] := '#';    while not Empty do    begin      x := Pop;      for i := 1 to n do      begin        y := order[x, i];        if party[y] = '#' then        begin          party[y] := x;          partx[x] := y;          break;        end;        if weight[y, party[y]] > weight[y, x] then        begin          Push(party[y]);          party[y] := x;          partx[x] := y;          break;        end;      end;    end;  end;  procedure Scan;  var    i, j: longint;    str:  string;  begin    readln(n);    readln;    for i := 1 to n + n do    begin      readln(str);      if (str[1] >= 'a') and (str[1] <= 'z') then      begin        order[str[1]] := Str;        Delete(order[str[1]], 1, 2);        Push(str[1]);      end;      if (str[1] >= 'A') and (str[1] <= 'Z') then      begin        for j := 1 to n do          weight[str[1], str[2 + j]] := j;      end;    end;  end;  procedure Print;  var    ch: char;  begin    for ch := 'a' to 'z' do      if partx[ch] <> '#' then        writeln(ch, #32, partx[ch]);  end;  procedure Main;  var    CaseNo: longint;  begin    readln(CaseNo);    while CaseNo > 0 do    begin      Dec(CaseNo);      Scan;      Gale;      Print;      if CaseNo > 0 then        writeln;    end;  end;begin  Assign(input, 'poj3487.in');  reset(input);  Assign(output, 'poj3487.out');  rewrite(output);  Main;  Close(input);  Close(output);end.


原创粉丝点击