forked from mohitsh/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstablemp.cpp
63 lines (63 loc) · 1.02 KB
/
stablemp.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
// 2010-02-14
#include <iostream>
#include <set>
using namespace std;
int main()
{
int T,N,i,j;
int husband[500],wife[500];
int w_pref_r[500][500];
int m_pref[500][500];
int proposed[500];
scanf("%d",&T);
while (T--)
{
scanf("%d",&N);
for (i=0; i<N; i++)
{
scanf("%*d");
husband[i]=-1;
for (j=0; j<N; j++)
{
int x;
scanf("%d",&x);
w_pref_r[i][x-1]=j;
}
}
set<int> free_men;
for (i=0; i<N; i++)
{
scanf("%*d");
wife[i]=-1;
free_men.insert(i);
proposed[i]=0;
for (j=0; j<N; j++)
{
scanf("%d",m_pref[i]+j);
m_pref[i][j]--;
}
}
while (free_men.size())
{
int m=*(free_men.begin());
int w=m_pref[m][proposed[m]++];
if (husband[w]==-1)
{
husband[w]=m;
wife[m]=w;
free_men.erase(m);
}
else if (w_pref_r[w][husband[w]]>w_pref_r[w][m])
{
free_men.insert(husband[w]);
wife[husband[w]]=-1;
husband[w]=m;
wife[m]=w;
free_men.erase(m);
}
}
for (i=0; i<N; i++)
printf("%d %d\n",i+1,wife[i]+1);
}
return 0;
}