Stable marriage is a type of bipartite matching.
The following code has been fixed to fix several variables in the original template:
# Include <iostream>
# Include <queue>
Using namespace STD;
# Deprecision Max 1010
Int N;
Int mlove [Max] [Max];
Int wlove [Max] [Max];
Int couple [Max];
Int pare [Max];
Queue <int> sq;
Void gale_shapley ()
{
Int I, man, woman;
While (! Sq. Empty ())
Sq. Pop ();
Memset (couple,-1, sizeof (couple ));
For (I = 1; I <= N; I ++)
Sq. Push (I );
While (! Sq. Empty ())
{
Man = SQ. Front ();
Sq. Pop ();
For (I = 1; I <= N; I ++)
{
If (mlove [Man] [I]! =-1) // if a boy has not selected this girl
{
// Select a girl that is not rejected and most popular
Woman = mlove [Man] [I]; // select your favorite girl
Mlove [Man] [I] =-1; // select and Mark
Long pre = Couple [Woman];
If (pre =-1) // if no girl is paired
{
Couple [Woman] = man; // match
Pare [Man] = woman; // match
Break;
}
Else // if paired
{
If (wlove [Woman] [Man]> wlove [Woman] [pre]) // if a girl prefers this boy more
{
Sq. Push (pre); // put the current data in the queue
Couple [Woman] = man; // match
Pare [Man] = woman; // match
Break;
}
}
}
}
}/While
}
Int main ()
{
Int I, j, T, HJ;
Scanf ("% d", & T );
While (t --)
{
Scanf ("% d", & N );
For (I = 1; I <= N; I ++)
{
For (j = 1; j <= N; j ++)
{
Scanf ("% d", & mlove [I] [J]); // record the number of the person you like
}
}
For (I = 1; I <= N; I ++)
{
For (j = 1; j <= N; j ++)
{
Scanf ("% d", & HJ );
Wlove [I] [HJ] = N-J; // The degree of liking for a record ranges from large to small.
}
}
Gale_shapley ();
For (I = 1; I <= N; I ++)
Printf ("% d \ n", pare [I]); // For the spouse of the male
}
Return 0;
}