Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions STABLEMP.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

vector<int> stableMariage(vector<vector<int>> &menPref, vector<vector<int>> &womenPref)
{
int n = menPref.size();
vector<vector<int>> mapWM(n);
for (int i = 0; i < n; ++i)
mapWM[i].resize(n);
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
mapWM[i][ womenPref[i][j] ] = j;
vector<int> womanMatch(n, -1);
vector<int> manMatch(n, -1);
queue<int> q;
for (int i = 0; i < n; ++i)
q.push(i);
while (!q.empty())
{
int man = q.front();
q.pop();
for (int woman : menPref[man])
{
if (womanMatch[woman] == -1)
{
womanMatch[woman] = man;
manMatch[man] = woman;
break;
}
else
{
int otherMan = womanMatch[woman];
if (mapWM[woman][otherMan] > mapWM[woman][man])
{
manMatch[otherMan] = -1;
q.push(otherMan);
womanMatch[woman] = man;
manMatch[man] = woman;
break;
}
}
}
}

return manMatch;
}

int main() {
IOS;
int t;
cin>>t;
while(t--) {
int n,temp;
cin>>n;
vector<vector<int>> menPref(n), womenPref(n);
for (int i = 0; i < n; ++i) {
womenPref[i].resize(n);
cin>>temp;
for (int j = 0; j < n; ++j) {
cin>>womenPref[i][j];
womenPref[i][j]--;
}
}
for (int i = 0; i < n; ++i) {
menPref[i].resize(n);
cin>>temp;
for (int j = 0; j < n; ++j) {
cin>>menPref[i][j];
menPref[i][j]--;
}
}
vector<int> marriages = stableMariage(menPref,womenPref);
for (int i = 0; i < n; ++i) {
cout<<i+1<<" "<<marriages[i]+1<<endl;
}
}

return 0;
}
24 changes: 24 additions & 0 deletions catalan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include<bits/stdc++.h>
#define ull unsigned long long int

using namespace std;

//Program to find the nth catalan number

ull catalan(ull n) {
ull c = 1;
for (int i = 0; i < n; ++i) {
c *= (2*n-i);
c /= (i+1);
}
c /= (n+1);
return c;
}

int main() {
ull n;
cin>>n;
cout<<catalan(n)<<endl;

return 0;
}
26 changes: 26 additions & 0 deletions fib.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include<bits/stdc++.h>
#define ull unsigned long long int

using namespace std;

//Program to find the nth fibonnaci number

int main() {
ull n;
cin>>n;
ull t1,t2;
t1 = 1;
t2 = 1;
if(n==1 || n==2) {
cout<<1<<endl;
return 0;
}
n -= 2;
while(n != 0) {
t2 += t1;
t1 = t2 - t1;
n--;
}
cout<<t2<<endl;
return 0;
}