forked from marioyc/Online-Judge-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3037 - Ladies' Choice.cpp
69 lines (51 loc) · 1.23 KB
/
3037 - Ladies' Choice.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
#include <cstdio>
#include <algorithm>
using namespace std;
#define MAX_N 1000
int N,pref_men[MAX_N][MAX_N],pref_women[MAX_N][MAX_N];
int inv[MAX_N][MAX_N],cont[MAX_N],wife[MAX_N],husband[MAX_N];
void stable_marriage(){
for(int i = 0;i<N;++i)
for(int j = 0;j<N;++j)
inv[i][pref_women[i][j]] = j;
fill(cont,cont+N,0);
fill(husband,husband+N,-1);
int m,w,dumped;
for(int i = 0;i<N;++i){
m = i;
while(m>=0){
while(true){
w = pref_men[m][cont[m]];
++cont[m];
if(husband[w]<0 || inv[w][m]<inv[w][husband[w]]) break;
}
dumped = husband[w];
husband[w] = m;
wife[m] = w;
m = dumped;
}
}
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
for(int i = 0;i < N;++i){
for(int j = 0;j < N;++j){
scanf("%d",&pref_men[i][j]);
--pref_men[i][j];
}
}
for(int i = 0;i < N;++i){
for(int j = 0;j < N;++j){
scanf("%d",&pref_women[i][j]);
--pref_women[i][j];
}
}
stable_marriage();
for(int i = 0;i < N;++i)
printf("%d\n",1 + wife[i]);
}
return 0;
}