Marriage is Stable
Time Limit: 5 Seconds Memory Limit: 32768 KB Special Judge
Albert, Brad, Chuck are happy bachelors who are in love with Laura, Marcy, Nancy. they all have three choices. but in fact, they do have some preference in mind. say Albert, he likes Laura best, but that doesn't necesarily mean Laura likes him. laura likes Chuck more than Albert. so if Albert can't marry Laura, he thinks Nancy a sensible choice. for Albert, he orders the girls Laura> Nancy> Marcy.
For the boys:
Albert: Laura> Nancy> Marcy
Brad: Marcy> Nancy> Laura
Chuck: Laura> Marcy> Nancy
For the girls:
Laura: Chuck> Albert> Brad
Marcy: Albert> Chuck> Brad
Nancy: Brad> Albert> Chuck
But if they were matched randomly, such
Albert <-> Laura
Brad <-> Marcy
Chuck <-> Nancy
They wocould soon discover it's not a nice solution. for Laura, she likes Chuck instead of Albert. and what's more, Chuck likes Laura better than Nancy. so Laura and Chuck are likely to come together, leaving poor Albert and Nancy.
Now it's your turn to find a stable marriage. A stable marriage means for any boy G and girl M, with their choice m [G] and m [M], it will not happen that rank (G, M) <rank (G, m [G] �� and rank (M, G) <rank (M, m [M]).
Input
Each case starts with an integer n (1 <= n <= 500), the number of matches to make.
The following n lines contain n + 1 names each, the first being name of the boy, and rest being the rank of the girls.
The following n lines are the same information for the girls.
Process to the end of file.
Output
If there is a stable marriage, print n lines with two names on each line. You can choose any one if there are multiple solution. Print "Impossible" otherwise.
Print a blank line after each test.
Sample Input
3
Albert Laura Nancy Marcy
Brad Marcy Nancy Laura
Chuck Laura Marcy Nancy
Laura Chuck Albert Brad
Marcy Albert Chuck Brad
Nancy Brad Albert Chuck
Sample Output
Albert Nancy
Brad Marcy
Chuck Laura
This is the stable marriage system,
Note that there are men and women with the same name.
[Cpp]
# Include <cstdio>
# Include <cstring>
# Include <cstdlib>
# Include <iostream>
# Include <functional>
# Include <algorithm>
# Include <queue>
# Include <iostream>
# Include <map>
# Include <string>
Using namespace std;
# Define MAXN (2000 + 10)
Int n;
Queue <int> q;
Map <string, int> hpos_male, hpos_female;
Int female_like [MAXN] [MAXN]; // SIS
Int male_like [MAXN] [MAXN]; // SSI
Int match [MAXN];
String a [MAXN];
Int main ()
{
// Freopen ("zoj1576.in", "r", stdin );
While (scanf ("% d", & n )! = EOF)
{
Hpos_male.clear ();
Hpos_female.clear ();
Memset (match, 0, sizeof (match ));
Int size = n + 1;
For (int I = 1; I <= n; I ++)
{
Cin> a [I]; hpos_male [a [I] = I; q. push (I );
For (int j = 1; j <= n; j ++)
{
String s;
Cin> s;
If (I = 1)
{
Hpos_female [s] = size;
A [size] = s;
Size ++;
}
Male_like [I] [j] = hpos_female [s];
}
}
For (int I = n + 1; I <= 2 * n; I ++)
{
String sg;
Cin> sg;
For (int j = 1; j <= n; j ++)
{
String s;
Cin> s;
Female_like [hpos_female [sg] [hpos_male [s] = j;
}
}
/*
For (int I = n + 1; I <= 2 * n; I ++)
{
For (int j = 1; j <= n; j ++)
Cout <female_like [make_pair (a [I], a [j])] <'';
Cout <endl;
}
*/
While (! Q. empty ())
{
Int now = q. front ();
Q. pop ();
For (int j = 1; j <= n; j ++)
{
Int v = male_like [now] [j];
If (! Match [v])
{
Match [now] = v; match [v] = now; // cout <now <<'' <v <endl;
Break;
}
Else if (female_like [v] [match [v]> female_like [v] [now])
{
Q. push (match [v]);
Match [match [v] = 0;
Match [now] = v; match [v] = now; // cout <now <<'' <v <endl;
Break;
}
}
} Www.2cto.com
For (int I = 1; I <= n; I ++)
{
Cout <a [I] <''<a [match [I] <endl;
}
Printf ("\ n ");
}
Return 0;
}