【POJ 3487】 The Stable Marriage Problem (稳定婚姻问题)
The Stable Marriage ProblemDescription
The stable marriage problem consists of matching members of two different sets according to the member’s preferences for the other set’s members. The input for our problem consists of:
- a set M of n males;
- a set F of n females;
- for each male and female we have a list of all the members of the opposite gender in order of preference (from the most preferable to the least).
A marriage is a one-to-one mapping between males and females. A marriage is called stable, if there is no pair (m, f) such that f ∈ F prefers m ∈ M to her current partner and m prefers f over his current partner. The stable marriage A is called male-optimal if there is no other stable marriage B, where any male matches a female he prefers more than the one assigned in A.
Given preferable lists of males and females, you must find the male-optimal stable marriage.
Input
The first line gives you the number of tests. The first line of each test case contains integer n (0 < n < 27). Next line describes n male and n female names. Male name is a lowercase letter, female name is an upper-case letter. Then go n lines, that describe preferable lists for males. Next n lines describe preferable lists for females.
Output
For each test case find and print the pairs of the stable marriage, which is male-optimal. The pairs in each test case must be printed in lexicographical order of their male names as shown in sample output. Output an empty line between test cases.
Sample Input
2 3 a b c A B C a:BAC b:BAC c:ACB A:acb B:bac C:cab 3 a b c A B C a:ABC b:ABC c:BCA A:bac B:acb C:abcSample Output
a A b B c C a B b A c CSource
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 #include<queue> 7 using namespace std; 8 #define Maxn 40 9 10 int ml[Maxn][Maxn],rk[Maxn][Maxn]; 11 int fw[Maxn],fh[Maxn],nt[Maxn]; 12 13 int n; 14 // bool pm[Maxn],pf[Maxn]; 15 char s[Maxn]; 16 17 queue<int > q; 18 19 void init() 20 { 21 scanf("%d",&n); 22 // memset(pm,0,sizeof(pm)); 23 // memset(pf,,sizeof(pf)); 24 for(int i=1;i<=n;i++) 25 { 26 scanf("%s",s); 27 // pm[s[0]-'a'+1]=1; 28 } 29 for(int i=1;i<=n;i++) 30 { 31 scanf("%s",s); 32 // pf[s[0]-'A'+1]=1; 33 } 34 while(!q.empty()) q.pop(); 35 memset(fw,0,sizeof(fw)); 36 memset(fh,0,sizeof(fh)); 37 for(int i=1;i<=n;i++) 38 { 39 scanf("%s",s); 40 int x=s[0]-'a'+1; 41 for(int j=1;j<=n;j++) 42 { 43 int y=s[j+1]-'A'+1; 44 ml[x][j]=y; 45 } 46 fw[x]=0;nt[x]=1; 47 q.push(x); 48 } 49 for(int i=1;i<=n;i++) 50 { 51 scanf("%s",s); 52 int x=s[0]-'A'+1; 53 for(int j=1;j<=n;j++) 54 { 55 int y=s[j+1]-'a'+1; 56 rk[x][y]=j; 57 } 58 fh[x]=0; 59 } 60 } 61 62 void engage(int x,int y) 63 { 64 int z=fh[y]; 65 if(z) 66 { 67 fw[z]=0; 68 q.push(z); 69 } 70 fw[x]=y;fh[y]=x; 71 } 72 73 void ffind() 74 { 75 while(!q.empty()) 76 { 77 int x=q.front();q.pop(); 78 int y=ml[x][nt[x]++]; 79 if(fh[y]==0||rk[y][x]<rk[y][fh[y]]) 80 engage(x,y); 81 else q.push(x); 82 } 83 } 84 85 int main() 86 { 87 int T; 88 scanf("%d",&T); 89 while(T--) 90 { 91 init(); 92 ffind(); 93 for(int i=1;i<=30;i++) if(fw[i]) 94 { 95 printf("%c %c\n",'a'+i-1,'A'+fw[i]-1); 96 } 97 if(T) printf("\n"); 98 } 99 return 0; 100 }
这样我还WA了两次,next不记得++,还有最好清零用memset全清了来得干净~~
2016-10-26 20:51:44