forked from chr4ss1/SPOJ
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTABLEMP.cpp
More file actions
156 lines (120 loc) · 2.78 KB
/
STABLEMP.cpp
File metadata and controls
156 lines (120 loc) · 2.78 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <iostream>
#include <stack>
#include <vector>
#include <queue>
using namespace std;
struct human
{
int id;
queue<int> preffered;
vector<int> positions;
bool is_engaded;
int partner_id;
};
struct stable_match
{
int men_id;
int women_id;
};
bool is_first_preffered(
human first_man,
human second_man,
human woman)
{
return woman.positions[first_man.id] < woman.positions[second_man.id];
}
vector<human> read(int count, int is_women)
{
human sentinel;
vector<human> humans;
humans.push_back(sentinel);
for (int i = 0; i < count; i++)
{
int index;
cin >> index;
human temp;
temp.is_engaded = false;
temp.id = index;
if (is_women)
temp.positions.resize(count + 1);
for (int j = 0; j < count; j++)
{
int temp_pref;
cin >> temp_pref;
temp.preffered.push(temp_pref);
if (is_women)
temp.positions[temp_pref] = j;
}
humans.push_back(temp);
}
return humans;
}
vector<stable_match> solve(
vector<human> men,
vector<human> women,
int marriages)
{
vector<stable_match> result;
stack<int> free_men;
// all men are free by default.
for (int i = 1; i <= marriages; i++)
free_men.push(i);
while (!free_men.empty()){
int current_man = free_men.top();
free_men.pop();
human &man = men[current_man];
// proposte to first women that we can find.
while (!man.preffered.empty())
{
int proposal = man.preffered.front();
man.preffered.pop();
human &woman = women[proposal];
// if woman is not engaged
// we will attempt to engage her.
if (!woman.is_engaded){
woman.is_engaded = true;
man.is_engaded = true;
man.partner_id = woman.id;
woman.partner_id = man.id;
}
else{
// this is going to be a little more trickier
// what do we do when women is already engaged with someone?
// we have to figure out if our "man" will make better offer
// if he does, we can let the other man become "free" again.
human &engaged_man = men[woman.partner_id];
if (!is_first_preffered(man, engaged_man, woman))
continue;
engaged_man.is_engaded = false;
free_men.push(engaged_man.id);
man.is_engaded = true;
man.partner_id = woman.id;
woman.partner_id = man.id;
}
break;
}
}
for (int i = 1; i < men.size(); i++){
human &man = men[i];
stable_match match;
match.men_id = man.id;
match.women_id = man.partner_id;
result.push_back(match);
}
return result;
}
int main()
{
int test_cases;
cin >> test_cases;
while (test_cases--){
int marriages;
cin >> marriages;
vector<human> women = read(marriages, true);
vector<human> men = read(marriages, false);
vector<stable_match> results = solve(men, women, marriages);
for (int j = 0; j < results.size(); j++)
cout << results[j].men_id << " " << results[j].women_id << endl;
}
return 0;
}