Uvalive 3989 Ladies ' Choice

Source: Internet
Author: User

Ladies ' choicetime limit:6000msmemory limit:131072kbthis problem'll be judged onuvalive. Original id:3989
64-bit integer IO format: %lld Java class name: Main Background

Teenagers from the local high school has asked you to help them with the organization of next years Prom. The idea was to find a suitable date for everyone in the class in a fair and civilized. So, they has organized a Web site where all students, boys and girls, state their preferences among the class members, by Ordering all the possible candidates. Your mission is to keep everyone as happy as possible. Assume that there is equal numbers of boys and girls.

problem

Given a set of preferences, set up the blind dates such that there is no other than people of opposite sex who would both Rather has each of the other than their the current partners. Since It was decided the Prom is Ladies ' Choice, we want to produce the best possible Choice for the girls.

Input

Input consists of multiple test cases the first line of the input contains the number of test cases. There is a blank line before each dataset. The input for each dataset consists of a positive integer N, not greater than, indicating the number of cou Ples in the class. Next, there is N lines, each one containing the "all" the integers from 1 to N, ordered according to the Girls preferences. Next, there is N lines, each one containing all the integers from 1 to N, ordered according to the boys Preferences.

Output

The output for each dataset consists of a sequence of N lines, where the i-th line contains the number O f the boy assigned to the i-th Girl (from 1 to N). Print a blank line between datasets.

Sample Input

1

5

1 2 3) 5 4

5 2 4) 3 1

3 5 1) 2 4

3 4 2) 1 5

4 5 1) 2 3

2 5 4) 1 3

3 2 4) 1 5

1 2 4) 3 5

4 1 2) 5 3

5 3 2) 4 1

Sample Output

1

2

5

3

4

SourceRegionals, Europe-southwestern problem solving: Stable marriage problems men in accordance with the status of ladies in their own minds from the high end, and then propose. If the woman had no previous object, was proposed, then for a couple of, if the woman had a previous object, if the man proposed at this time than her original object in her higher position, then with the original that the man in her mind, then the original man to become a bachelor. Establish an object relationship with the man who proposed the new proposal. If the man who proposes at this time does not have a woman's former man in her heart, then the man continues to be a bachelor. Until there is no bachelor, the marriage is stable at this time.
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN =1010;4 intN,MR[MAXN][MAXN],MISS[MAXN][MAXN];5 intWIFE[MAXN],HUSBAND[MAXN],NXT[MAXN];6queue<int>Q;7 voidEngageintManintwoman) {8     if(Husband[woman]) {9Wife[husband[woman]] =0;Ten Q.push (Husband[woman]); One     } AHusband[woman] =Man ; -Wife[man] =woman; - } the voidGaleshapley () { -      while(!Q.empty ()) { -         intMan =Q.front (); - Q.pop (); +         intWoman = mr[man][nxt[man]++]; -         if(!Husband[woman]) Engage (man,woman); +         Else if(Miss[woman][husband[woman]) >Miss[woman][man]) A Engage (Man,woman); at         ElseQ.push (man); -     } - } - intMain () { -     intkase,tmp; -scanf"%d",&Kase); in      while(kase--) { -scanf"%d",&n); to          while(!q.empty ()) Q.pop (); +          for(inti =1; I <= N; ++i) { -              for(intj =1; J <= N; ++j) thescanf"%d", mr[i]+j); *Nxt[i] =1; $Wife[i] =0;Panax Notoginseng Q.push (i); -         } the          for(inti =1; I <= N; ++i) { +              for(intj =1; J <= N; ++j) { Ascanf"%d",&tmp); theMISS[I][TMP] =J; +             } -Husband[i] =0; $         } $ Galeshapley (); -          for(inti =1; I <= N; ++i) -printf"%d\n", Wife[i]); the         if(Kase) Putchar ('\ n'); -     }Wuyi     return 0; the}
View Code

Uvalive 3989 Ladies ' Choice

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.