10.1.2 Marriage is Stable

穩定婚配~~~~~~html

我是看白書看懂的,固然網上的資料也很好~ios

http://blog.sina.com.cn/s/blog_5f48a0a10101150v.htmlapp

http://hi.baidu.com/acmdearway/item/745d54c8160d3dd3964452e9dom

http://en.wikipedia.org/wiki/Stable_marriage_problemspa

簡單來講,就是每一個男生向本身喜歡的女生表白,而後每一個女生從中選擇本身認爲最好的男生,而後接受這個男生,拒絕其餘男生,那麼被拒絕的那些男生再重複以上過程,能夠證實必定存在合法解,我不得不說讀入輸出太蛋疼了,主程序仍是很好寫的debug

Marriage is Stable

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35 Accepted Submission(s): 26

Problem Description
Albert, Brad, Chuck are happy bachelors who are in love with Laura, Marcy, Nancy. They all have three choices. But in fact, they do have some preference in mind. Say Albert, he likes Laura best, but that doesn't necesarily mean Laura likes him. Laura likes Chuck more than Albert. So if Albert can't marry Laura, he thinks Nancy a sensible choice. For Albert, he orders the girls Laura > Nancy > Marcy.

For the boys:

Albert: Laura > Nancy > Marcy
Brad: Marcy > Nancy > Laura
Chuck: Laura > Marcy > Nancy

For the girls:

Laura: Chuck > Albert > Brad
Marcy: Albert > Chuck > Brad
Nancy: Brad > Albert > Chuck

But if they were matched randomly, such as

Albert <-> Laura
Brad <-> Marcy
Chuck <-> Nancy

they would soon discover it's not a nice solution. For Laura, she likes Chuck instead of Albert. And what's more, Chuck likes Laura better than Nancy. So Laura and Chuck are likely to come together, leaving poor Albert and Nancy.

Now it's your turn to find a stable marriage. A stable marriage means for any boy G and girl M, with their choice m[G] and m[M], it will not happen that rank(G, M) < rank(G, m[G])and rank(M, G) < rank(M, m[M]).
 

Input
Each case starts with an integer n (1 <= n <= 500), the number of matches to make.

The following n lines contain n + 1 names each, the first being name of the boy, and rest being the rank of the girls.

The following n lines are the same information for the girls.

Process to the end of file.
 

Output

            If there is a stable marriage, print n lines with two names on each line. You can choose any one if there are multiple solution. Print "Impossible" otherwise.

Print a blank line after each test.
 

Sample Input
3
Albert Laura Nancy Marcy
Brad Marcy Nancy Laura
Chuck Laura Marcy Nancy
Laura Chuck Albert Brad
Marcy Albert Chuck Brad
Nancy Brad Albert Chuck
 

Sample Output
Albert Nancy
Brad Marcy
Chuck Laura
 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<fstream>
 6 #include<sstream>
 7 #include<bitset>
 8 #include<vector>
 9 #include<string>
10 #include<cstdio>
11 #include<cmath>
12 #include<stack>
13 #include<queue>
14 #include<stack>
15 #include<map>
16 #include<set>
17 #define FF(i, a, b) for(int i=a; i<b; i++)
18 #define FD(i, a, b) for(int i=a; i>=b; i--)
19 #define REP(i, n) for(int i=0; i<n; i++)
20 #define CLR(a, b) memset(a, b, sizeof(a))
21 #define debug puts("**debug**")
22 #define LL long long
23 #define PB push_back
24 using namespace std;
25 
26 const int maxn = 510;
27 int pref[maxn][maxn], order[maxn][maxn], next[maxn];
28 int hus[maxn], wife[maxn];
29 queue<int> q;
30 
31 void enage(int man, int woman)
32 {
33     int m = hus[woman];
34     if(m)
35     {
36         q.push(m);
37     }
38     wife[man] = woman;
39     hus[woman] = man;
40 }
41 
42 map<string, int> wo, ma;
43 map<int, string> ans1, ans2;
44 int man_cnt, woman_cnt, n;
45 int man_id(string a)
46 {
47     if(!ma.count(a)) ma[a] = man_cnt++, ans1[man_cnt-1] = a;
48     return ma[a];
49 }
50 int woman_id(string a)
51 {
52     if(!wo.count(a)) wo[a] = woman_cnt++, ans2[woman_cnt-1] = a;
53     return wo[a];
54 }
55 
56 int main()
57 {
58 freopen("1012.in","r",stdin);
59 //freopen("test.out","w",stdout);
60     while(~scanf("%d", &n))
61     {
62         man_cnt = woman_cnt = 1;
63         wo.clear(), ma.clear(), ans1.clear(), ans2.clear();
64         char a[111];
65         int x, y;
66         FF(i, 1, n+1)
67         {
68             scanf("%s", a); x = man_id(string(a));
69             FF(j, 1, n+1)
70             {
71                 scanf("%s", a); y = woman_id(string(a));
72                 pref[x][j] = y;
73             }
74             next[i] = 1;
75             wife[i] = 0;
76             q.push(i);
77         }
78         FF(i, 1, n+1)
79         {
80             scanf("%s", a); x = woman_id(string(a));
81             FF(j, 1, n+1)
82             {
83                 scanf("%s", a); y = man_id(string(a));
84                 order[x][y] = (int)1E9-j;
85             }
86             hus[i] = 0;
87         }
88         while(!q.empty())
89         {
90             int man = q.front(); q.pop();
91             int woman = pref[man][next[man]++];
92             if(order[woman][man] > order[woman][hus[woman]]) enage(man, woman);
93             else q.push(man);
94         }
95         FF(i, 1, n+1) cout<<ans1[i]<<" "<<ans2[wife[i]]<<endl;
96         puts("");
97         while(!q.empty()) q.pop();
98     }
99 }
相關文章
相關標籤/搜索