-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1400 - Employment.cpp
83 lines (72 loc) · 1.51 KB
/
1400 - Employment.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
#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;
}
}
}
}
for (int i = 0; i < n; i++)
printf(" (%d %d)" , p[i] + 1, i + n + 1);
}
int main()
{
int tes , c = 0;
cin >> tes;
while(tes--)
{
c++;
cin >> n;
for(int i = 0; i < n * 2; i++)
{
for(int j = 0; j < n; j++)
{
cin >> pref[i][j];
pref[i][j]--;
}
}
printf("Case %d:",c);
stable_marriage();
printf("\n");
}
}