Pairing
Time limit3sMemory limit128 MB
Given complete preference rankings of N men and N women, compute the man-optimal stable pairing.
- Level
Medium5 of 10
- Topics
- Greedy, Simulation, Queue
- Solved
- No attempts yet
Problem
There are men and women to pair up. Every man is paired with exactly one woman, and every woman is paired with exactly one man. Each person ranks everyone on the other side from most preferred to least preferred, with no ties.
A pairing is stable when this holds: there is no man and woman , not paired with each other, such that prefers to his own partner and prefers to her own partner.
Several stable pairings can exist, so print only the man-optimal one. In the man-optimal pairing, every man is paired with the woman he prefers most among the women who are his partner in at least one stable pairing. This pairing always exists and it is unique.
Given every ranking, find that pairing.
Input
The first line contains the number of people (). The men are numbered 1 to , and the women are numbered 1 to as well.
Line of the next lines holds the ranking of man . It lists every number from 1 to once, with the number of the more preferred woman earlier in the line.
Line of the following lines holds the ranking of woman in the same format.
Output
Print lines. Line holds the number of the woman paired with man in the man-optimal pairing.