POJ3487 The Stable Marriage Problem

題意:穩定婚姻問題。

分析:

od(i, j)表示編號爲i的女士心目中,編號爲x的男士的排名。

pf(i, j)表示編號爲i的男士第j喜歡的人。

f(0, i)表示編號爲i的女士的未婚夫,f(1, i)表示編號爲i的男士的未婚妻。

nxt(i)表示編號爲i的男士下一個求婚對象的排名。

q隊列裏存着所有未訂婚的男士。

這題輸入有些奇怪,小心一點就是了。

然後用站在女性角度考慮好像不符合題目要求,得站在男性角度考慮,白白WA一次。

這題代碼就沒有什麼難理解的地方了。

#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;

const int N = 30;
char str[N], name[N*2];
int T, n, x, od[N][N], pf[N][N], nxt[N], f[2][N];
queue<int> q;

void date(int x, int y) {
	int m = f[0][y];
	if(m) f[1][m] = 0, q.push(m);
	f[1][x] = y, f[0][y] = x;
}

int main() {
	scanf("%d", &T);
	while(T--) {
		scanf("%d", &n);
		for(int i = 1; i <= n*2; i++) scanf("%s", str), name[i] = str[0];
		sort(name+1, name+n+1);
		for(int i = 1; i <= n; i++) {
			scanf("%s", str);
			for(int j = 2; j < n+2; j++) pf[str[0]-'a'+1][j-1] = str[j]-'A'+1;
			nxt[str[0]-'a'+1] = 1, f[1][str[0]-'a'+1] = 0;
			q.push(str[0]-'a'+1);
		}
		for(int i = 1; i <= n; i++) {
			scanf("%s", str);
			for(int j = 2; j < n+2; j++) od[str[0]-'A'+1][str[j]-'a'+1] = j-1;
			f[0][str[0]-'A'+1] = 0;
		}
		while(!q.empty()) {
			int u = q.front(); q.pop();
			int v = pf[u][nxt[u]++];
			if(!f[0][v]) date(u, v);
			else if(od[v][u] < od[v][f[0][v]]) date(u, v);
			else q.push(u);
		}
		for(int i = 1; i <= n; i++)	printf("%c %c\n", name[i], f[1][name[i]-'a'+1]+'A'-1);
		if(T) printf("\n");
	}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章