-
Notifications
You must be signed in to change notification settings - Fork 77
/
stable_marriage_problem.cpp
83 lines (74 loc) · 2.55 KB
/
stable_marriage_problem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* Ignore the dirty comments */
#define MAX 100001
int prefM[MAX][MAX], rankW[MAX][MAX], options[MAX], marriedTo[MAX];
queue <int> bachelor;
int N;
void stableMarriage() {
memset(marriedTo, -1, sizeof marriedTo);
int man, woman, husband;
// while there is a free man m who still has a woman to propose to
while(!bachelor.empty()) {
man = bachelor.front();
bachelor.pop();
bool friendZoned = true;
for(int j = options[man] - 1; j >= 0; --j) {
// man's highest ranked such woman to whom he has not yet proposed
woman = prefM[man][j];
// man won't propose this woman again
options[man]--;
// if this woman is free
if(marriedTo[woman] == -1) {
// man and woman become engaged
marriedTo[woman] = man;
// man is no longer friend, so stop
friendZoned = false;
break;
} else { // if woman is already engaged
husband = marriedTo[woman];
// if woman prefers man to husband (bitch)
if(rankW[woman][man] > rankW[woman][husband]) {
// woman divorces her husband and man-woman get engaged
marriedTo[woman] = man;
// welcome to friendzone, you miserable bastard! :3)
bachelor.push(husband);
// man is no longer friend, so stop
friendZoned = false;
break;
}
}
}
// if man got his dick still dry, push the loser into frinedzone again
if(friendZoned) bachelor.push(man);
}
}
/*
Problem: http://www.codechef.com/problems/STABLEMP (Classical Stable Marriage problem)
*/
int main(void) {
int tcase;
scanf("%d", &tcase);
int man, woman;
while(tcase--) {
scanf("%d", &N);
for(int i = 1; i <= N; i++) {
scanf("%d", &woman);
for(int j = N - 1; j >= 0; j--) {
scanf("%d", &man);
rankW[woman][man] = j;
}
}
for(int i = 1; i <= N; i++) {
scanf("%d", &man);
bachelor.push(man);
options[man] = N;
for(int j = N - 1; j >= 0; j--) {
scanf("%d", &prefM[man][j]);
}
}
stableMarriage();
for(int i = 1; i <= N; i++) {
printf("%d %d\n", marriedTo[i], i);
}
}
return EXIT_SUCCESS;
}