-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStable_Marriage_.cpp
More file actions
79 lines (69 loc) · 1.44 KB
/
Stable_Marriage_.cpp
File metadata and controls
79 lines (69 loc) · 1.44 KB
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
#include <bits/stdc++.h>
using namespace std;
int pref[220][110];
int n;
bool check (int w, int m, int m1)
{
for(int i = 0; i < n; i++)
{
if(pref[w][i] == m1)
return true;
if(pref[w][i] == m)
return false;
}
}
void stable_marriage()
{
int p[110];
bool free[110];
memset(p, -1, sizeof p);
memset(free, false, sizeof free);
int cnt =n;
while (cnt > 0)
{
int m;
for (m = 0; m < n; m++)
if (free[m] == false)
break;
for (int i = 0; i < n && free[m] == false; i++)
{
int w = pref[m][i];
if (p[w-n] == -1)
{
p[w-n] = m;
free[m] = true;
cnt--;
}
else
{
int m1 = p[ w- n];
if (check(w, m, m1) == false)
{
p[w-n] = m;
free[m] = true;
free[m1] = false;
}
}
}
}
cout << "Woman Man" << endl;
for (int i = 0; i < n; i++)
cout << " " << i+n << "\t" << p[i] << endl;
}
int main()
{
int tes;
cin >> tes;
while(tes--)
{
cin >> n;
for(int i = 0; i < n * 2; i++)
{
for(int j = 0; j < n; j++)
{
cin >> pref[i][j];
}
}
stable_marriage();
}
}