Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdio>
- #include <queue>
- #include <string.h>
- using namespace std;
- int ranking[505][505]; // Ranking gives the ranking of each man in the preference list of each woman
- int man_pref[505][505]; // man's preference list
- int woman_pref[505][505]; // woman's preference list
- int next[505]; // which woman will be proposed for each man
- int matches[505]; // the current engagement of each woman
- int main(){
- queue<int> freeMen;
- int count , i , j ,w , m , current_woman , current_man;
- scanf("%d" , &count);
- for(i=1;i<=count;i++){
- scanf("%d" , &w);
- for(j=1 ; j <=count ; j++){
- scanf("%d" , &woman_pref[w][j]);
- }
- } // initialize the preference list of woman
- for(i=1;i<=count;i++){
- scanf("%d" , &m);
- for(j=1 ; j <=count ; j++){
- scanf("%d" , &man_pref[m][j]);
- }
- } // initialize the preference list of man
- for(i=1;i<=count;i++)
- for(j=1;j<=count;j++)
- ranking[i][woman_pref[i][j]] = j; // initialize ranking
- memset(matches , 0 , (count+1)*sizeof(int));
- for(i=1;i<=count;i++){
- freeMen.push(i);
- next[i] = 1;
- }
- while(!freeMen.empty()){
- current_man = freeMen.front();
- current_woman = man_pref[current_man][next[current_man]];
- if(matches[current_woman] == 0){
- matches[current_woman] = current_man;
- freeMen.pop();
- }
- else if(ranking[current_woman][current_man] < ranking[current_woman][matches[current_woman]]){
- int ex_man = matches[current_woman];
- freeMen.pop();
- matches[current_woman] = current_man;
- freeMen.push(ex_man);
- }
- next[current_man]++;
- }
- for(i = 1 ; i <= count ; i++){
- printf("man : %d , woman : %d get married!\n" , matches[i] , i);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement