CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • February CookOff
    • February Long Contest
    • January CookOff
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
    • Event Calendar
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Ranks
    • Tutorials
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Practice(medium) » Stable Marriage Problem

Stable Marriage Problem

Problem code: STABLEMP

  • Submit
  • All Submissions

All submissions for this problem are available.

There are given n men and n women. Each woman ranks all men in order of her preference (her first choice, her second choice, and so on). Similarly, each man sorts all women according to his preference. The goal is to arrange n marriages in such a way that if a man m prefers some woman w more than his wife, and w prefers m more then her husband a new marriage occurs between w and m. If w prefers her husband more, then she stays married to him. This problem always has a solution and your task is to find one.

Input

The first line contains a positive integer t<=100 indicating the number of test cases. Each test case is an instance of the stable marriage problem defined above. The first line of each test case is a positive integer n<=500 (the number of marriages to find). The next n lines are the woman's preferences: ith line contains the number i (which means that this is the list given by the ith woman) and the numbers of men (the first choice of ith woman, the second choice,...). Then, the men's preferences follow in the same format.

Output

For each test case print n lines, where each line contains two numbers m and w, which means that the man number m and the woman number w should get married.

Example

Input:
2
4
1 4 3 1 2
2 2 1 3 4
3 1 3 4 2
4 4 3 1 2
1 3 2 4 1
2 2 3 1 4
3 3 1 2 4
4 3 2 4 1
7
1 3 4 2 1 6 7 5
2 6 4 2 3 5 1 7
3 6 3 5 7 2 4 1
4 1 6 3 2 4 7 5
5 1 6 5 3 4 7 2
6 1 7 3 4 5 6 2
7 5 6 2 4 3 7 1
1 4 5 3 7 2 6 1
2 5 6 4 7 3 2 1
3 1 6 5 4 3 7 2
4 3 5 6 7 2 4 1
5 1 7 6 4 3 5 2
6 6 3 7 5 2 4 1
7 1 7 4 2 6 5 3



Output:
1 3
2 2
3 1
4 4
1 4
2 5
3 1
4 3
5 7
6 6
7 2

Warning: large Input/Output data, be careful with certain languages


Date: 2008-12-01
Time limit: 3

s
Source limit: 50000
Languages: C C99 strict C++ PAS gpc PAS fpc JAVA NICE JAR C# C#2 NEM ST ASM D FORT ADA BASH PERL PYTH RUBY LUA ICON PIKE PHP SCM guile SCM qobi LISP sbcl LISP clisp HASK CAML CLPS PRLG WSPC BF ICK TEXT


  • Submit

Comments

  • Login or Register to post a comment.

"This problem always has a

chicodroid @ 15 Oct 2009 10:45 AM

"This problem always has a solution and your task is to find one." does that mean that a test case could have two answers?

Yes.

triplem @ 15 Oct 2009 11:26 AM

Yes.

my code is taking around 2.7

manib @ 25 Oct 2009 12:51 PM

my code is taking around 2.7 sec on my laptop for n==500.

Still while submitting am getting Time Limit Exceeded

I have checked the corner cases and i dont think my code will go in infinite loop in any of them.

P.S. am assuming n > 0

Any pointers is much appreciated. submission Id: 115016

Is the preferences of

manib @ 25 Oct 2009 01:04 PM

Is the preferences of women/men given sequentially (1,2,3,4 ... so on) as shown in problem sample input?

or coult it be like this too?

4 4 3 1 2
1 4 3 1 2
3 1 3 4 2
2 2 1 3 4



@admin for the case where

bsong @ 5 Feb 2010 11:12 PM

@admin for the case where n=7, isn't

 

1 4

2 7

3 1

4 6

5 5

6 3

7 2

 

a solution?

"if a man m prefers some

bsong @ 5 Feb 2010 11:19 PM

"if a man m prefers some woman w more than his wife, and w prefers m more then her husband a new marriage occurs between w and m. If w prefers her husband more, then she stays married to him."

 

in case of n=7 and m=2, the solution given is w=5.

 

man 2 prefers woman 5 over his wife, however woman 5 prefers her own husband over man 2.

 

how is the solution correct?

This problem should be in the

boxer @ 20 Feb 2010 09:06 PM

This problem should be in the "Easy" section.

I am getting a WA , working

gunjanbansal @ 20 May 2010 08:15 PM

I am getting a WA , working fine for test cases above , any corner cases , i even tried to add an extra n in last solution.

Something that i may be missing ??

any hints ?? corner cases ??

gunjanbansal @ 21 May 2010 07:36 PM

any hints ?? corner cases ?? code worrks fine for above cases .

still looking for workarounds

gunjanbansal @ 24 May 2010 12:10 AM

still looking for workarounds ..

I dont seem to find any

gunjanbansal @ 31 May 2010 10:24 PM

I dont seem to find any corner cases in this one . I am getting WA , code is working for above two test cases .

@admin -  isn't the point

jimmy valentine @ 28 Jan 2011 02:12 AM

@admin -  isn't the point made by Shreyas above correct ? isn't the second test case incorrect ? Kindly reply.

Shreyas' point makes no

triplem @ 28 Jan 2011 04:00 AM

Shreyas' point makes no sense. Man 2's wife IS woman 5, so how could he prefer woman 5 to his wife?

Thanks for reply

jimmy valentine @ 28 Jan 2011 04:11 AM

Thanks for reply Stephen....but how can you say man 2's wife is 5? It is just that 2's first preference is 5. I am assuming in the beginning that (before this input) ith man is married ith woman, is that assumption correct ?

You have to decide who

triplem @ 28 Jan 2011 04:52 AM

You have to decide who marries who in a way that satisfies the conditions. In the sample output, man 2 is married to woman 5, and doesn't prefer any other woman who prefers him to their husband. I'm not really sure what you're asking.

got you, thanks...I am

jimmy valentine @ 29 Jan 2011 04:00 AM

got you, thanks...I am getting correct answer for the above inputs but it's giving wrong answer here. some corner test cases may help.

Since it is very easy to

triplem @ 29 Jan 2011 07:10 AM

Since it is very easy to check whether an output is correct or not, you can generate your own test cases and check your answer is correct very easily, even for n=500. There aren't really any corner cases that are more special than others.

"If w prefers her husband

vivek.cs.iitr @ 31 Jan 2011 06:08 PM

"If w prefers her husband more, then she stays married to him."

But where is it given that i'th woman husband is j'th man ?

You have to output a set of

triplem @ 1 Feb 2011 06:16 AM

You have to output a set of marriages that is stable; ie where nobody would marry somebody else due to those conditions.

can a man marry twice?

ascending @ 26 May 2011 05:10 PM
can a man marry twice?

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold

HELP

Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

 

  • Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.
  • Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.
  • Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.
  • Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.
  • Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.
  • If you are still having problems, see a sample solution here.

CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of computer programming. At CodeChef we work hard to revive the geek in you by hosting programming contests on a monthly basis. We also aim to have training sessions and events related to online programming for programmers around the world. Apart from providing a platform for programming competitions, CodeChef also has various tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our judge accepts solutions in over 35+ programming languages. Online programming was never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming competitions that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly programming contests and the shorter format Cook-off programming contests. Put yourself up for recognition and win great prizes. Prizes worth up to Rs.20,000 and $700 are up for grabs every month along with lots more CodeChef goodies.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be part of CodeChefs Forums and interact with all our programmers love helping out other programmers and share their ideas.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. Be a part of the CodeChef community through CodeChef meetups and techtalks. You can also host a programming contest for your institute on CodeChef and be a guest author on our blog.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com