#include <iostream>
#include <vector>
#include <set>
using namespace std;
bool woman_prefers(vector<int> woman_preferences, int new_man, int old_man) {
for(int i = 0; i < woman_preferences.size(); i++) {
if (woman_preferences.at(i) == new_man) return true;
if (woman_preferences.at(i) == old_man) return false;
}
throw 1;
}
// Outputs vector of length n where vec.at([i]) is the i-th woman's assigned man
vector<int> modifiedGS(vector<vector<int>>& men_preferences, vector<vector<int>>& women_preferences) {
// cout << "CALLED MGS" << endl;
// for(int m = 0; m < men_preferences.size(); m++) {
// cout << "m = " << m << ", mp[" << m << "] = [" << men_preferences.at(m).at(0);
// for(int i = 1; i < men_preferences.at(m).size();i++) cout << ", " << men_preferences.at(m).at(i);
// cout << "]" << endl;
// }
vector<int> proposal_indices;
vector<bool> men_engaged;
vector<int> output;
for (int i = 0; i < men_preferences.size(); i++) {
proposal_indices.push_back(0);
men_engaged.push_back(false);
output.push_back(-1);
}
int num_engaged = 0;
while (num_engaged < men_preferences.size()) {
int free_man = 0;
while(men_engaged[free_man]) free_man++;
// free_man proposes to men_preferences[proposal_indices[free_man]]
if (proposal_indices[free_man] >= men_preferences.size()) {
vector<int> no_sm;
return no_sm;
}
int woman = men_preferences[free_man][proposal_indices[free_man]++];
if (output[woman] == -1 || woman_prefers(women_preferences[woman], free_man, output[woman])) {
if (output[woman] > 0) men_engaged[output[woman]] = false;
else num_engaged++;
men_engaged[free_man] = true;
output[woman] = free_man;
}
}
for (int& wp : output) {
if (wp == -1) {
vector<int> no_sm;
return no_sm;
}
}
return output;
}
// Takes a partially reduced men_preferences and women_preferences vectors,
// a reference to a set to which to add solutions, and the output of the last
// GS run, which is used to trim the preference lists
// mp is passed by value so it can be safely modified
void recursiveExplore(vector<vector<int>> mp, vector<vector<int>>& wp, set<vector<int>>& output_set, vector<int>& last_run) {
// First trim per the last run:
for (int w = 0; w < last_run.size(); w++) { // for each woman, remove her match's more preferred picks
int m = last_run.at(w);
int match_index = 0;
while (mp.at(m).at(match_index) != w)match_index++;
mp.at(m).erase(mp.at(m).begin(), mp.at(m).begin() + match_index);
}
// Now, for all men who still have more than one preference, expand the search tree
for (int i = 0; i < mp.size(); i++) {
if (mp.at(i).size() > 1) {
int removed_woman = mp.at(i).at(0);
mp.at(i).erase(mp.at(i).begin());
auto res = modifiedGS(mp, wp);
if (res.size() > 0) { // if false, matching is no longer possible, cut off this tree branch
output_set.insert(res); // log result, and expand on this node
recursiveExplore(mp, wp, output_set, res);
}
// replace the removed woman for the next expansion
mp.at(i).insert(mp.at(i).begin(), removed_woman);
}
}
}
set<vector<int>> allStableMatchings(vector<vector<int>>& men_preferences, vector<vector<int>>& women_preferences) {
set<vector<int>> out;
auto gs = modifiedGS(men_preferences, women_preferences);
out.insert(gs);
recursiveExplore(men_preferences, women_preferences, out, gs);
return out;
}
void printMatching(vector<int> women_matches) {
cout << "(w" << 0 << ", m" << women_matches.at(0) << ")";
for (int i = 1; i < women_matches.size(); i++) {
cout << ", (w" << i << ", m" << women_matches.at(i) << ")";
}
cout << endl;
}
int main()
{
vector<vector<int>> mp, wp;
vector<int> m1, m2, m3, w1, w2, w3;
m1.push_back(0);
m1.push_back(1);
m1.push_back(2);
m2.push_back(1);
m2.push_back(2);
m2.push_back(0);
m3.push_back(2);
m3.push_back(0);
m3.push_back(1);
w1.push_back(1);
w1.push_back(2);
w1.push_back(0);
w2.push_back(2);
w2.push_back(0);
w2.push_back(1);
w3.push_back(0);
w3.push_back(1);
w3.push_back(2);
mp.push_back(m1);
mp.push_back(m2);
mp.push_back(m3);
wp.push_back(w1);
wp.push_back(w2);
wp.push_back(w3);
auto all_matchings = allStableMatchings(mp, wp);
for (auto& matching : all_matchings) {
printMatching(matching);
}
return 0;
}
About Online C++ Compiler
Try our Online C++ Compiler (Version GNU GCC v11.3.0) to Edit, Run, and Share your C++ Code directly from your browser. This online development environment provides you the latest version GNU GCC v11.3.0.
How to use Online C++ Compiler?
Write and Execute Code
- Write your program (or, paste it) directly under the "Source Code" tab.
- If you want to save your program, go to the "Project" menu and save it.
- You can directly execute your program without saving it by clicking on on "Execute" button.
User Input
The latest version of Coding Ground allows to provide program input at run time from the termnial window exactly the same way as you run your program at your own computer. So simply run a program and provide your program input (if any) from the terminal window available in the right side.
Online C++ Compiler: Keyboard Shortcuts
The following are the keyword shortcut of this Online C++ Compiler:
Shortcut | Description |
⌘ + Enter | Run the program |
⌘ + S | Save Project (Login Required) |
⇧ + ⌘ + S | Save As Project |
⌘ + P | New Project |
⌘ + G | Share Project |
⌘ + Z | Undo Editing |
⌘ + Y | Redo Editing |
⌘ + A | Select All Text |
⌘ + X | Cut Selected Text |
⌘ + C | Copy Selected Text |
⌘ + V | Paste Copied Text |
⌘ + F | Search Text |
⌘ + ⌥ + F | Replace Text |
Shortcut | Description |
Ctrl + Enter | Run the program |
Ctrl + S | Save Project |
Shift + Ctrl + S | Save As Project |
Ctrl + G | Share Project |
Ctrl + Z | Undo Editing |
Ctrl + Y | Redo Editing |
Ctrl + A | Select All Text |
Ctrl + X | Cut Selected Text |
Ctrl + C | Copy Selected Text |
Ctrl + V | Paste Copied Text |
Ctrl + F | Search Text |
Ctrl + H | Replace Text |
Online C++ Compiler: Save and Share C++ Code (Project)
Save C++ Project Online
You can save your C++ Project with us so that you can access this project later on. To save a project you will need to create a login Id with us. So before you save a project, please create a login Id using a link given at the top right corner of this page.
Share C++ Project Online
You can use this feature to share your C++ Code with your teachers, classmates and colleagues. Just click Share Button and it will create a short link, which can be shared through Email, WhatsApp or even through Social Media. A shared link will be deleted if it has been passive for almost 3 months.
More Features of Online C++ Compiler
- Theme – You can change the current editor's theme from the "Editor Theme" option under "Settings" menu.
- Font Size – You can change the font size of the editor /compiler from from the "Font Size" option under "Settings" menu.
- Tab Size – You can change the tab size from the "Tab Size" option under "Settings" Menu.
- Show/Hide Line Numbers – You can show/hide the line number with the code from the "Show Line Numbers" or "Hide Line Numbers" option under "Settings" Menu.
- And, many more.
Benefits of Using Online C++ Compiler
There are several benefits of using the Online C++ Compiler to run your C++ code:
- Platform independence: You can run your code from any device without taking care of operating systems.
- Convenience: You don't need to install anything for using this.
- No setup required: There is no need for additional setup to run your code.
- Updated version: Our online compiler/editors/terminals are the latest up-to-date.