稳定婚姻问题
2016-07-18 01:36
387 查看
ACM模版
稳定婚姻问题
/* * 稳定婚姻问题O(n^2) */ const int N = 1001; struct People { bool state; int opp, tag; int list ; // man使用 int priority ; // woman使用,有必要的话可以和list合并,以节省空间 void Init() { state = tag = 0; } } man , woman ; struct R { int opp; int own; } requst ; int n; void Input(); void Output(); void stableMatching(); int main() { Input(); stableMatching(); Output(); return 0; } void Input() { scanf("%d\n", &n); int i, j, ch; for (i = 0; i < n; ++i) { man[i].Init(); for(j = 0; j < n; ++j) { // 按照man的意愿递减排序 scanf("%d", &ch); man[i].list[j] = ch - 1; } } for (i = 0; i < n; ++i) { woman[i].Init(); for (j = 0; j < n; ++j) { // 按照woman的意愿递减排序,但是,存储方法与man不同 scanf("%d", &ch); woman[i].priority[ch - 1] = j; } } return ; } void stableMatching() { int k; for (k = 0; k < n; ++k) { int i, id = 0; for (i = 0; i < n; ++i) { if (man[i].state == 0) { requst[id].opp = man[i].list[man[i].tag]; requst[id].own = i; man[i].tag += 1; ++id; } } if (id == 0) { break; } for (i = 0; i < id; i++) { if (woman[requst[i].opp].state == 0) { woman[requst[i].opp].opp = requst[i].opp; woman[requst[i].opp].state = 1; man[requst[i].own].state = 1; man[requst[i].own].opp = requst[i].opp; } else { if (woman[requst[i].opp].priority[woman[requst[i].opp].opp] >woman[requst[i].opp].priority[requst[i].own]) { man[woman[requst[i].opp].opp].state = 0; woman[requst[i].opp].opp = requst[i].own; man[requst[i].own].state = 1; man[requst[i].own].opp = requst[i].opp; } } } } return ; } void Output() { for (int i = 0; i < n; i++) { printf("%d\n", man[i].opp + 1); } return ; }
相关文章推荐
- Gale-Shapley算法中 男性优势地位 的证明
- POJ 3487 The Stable Marriage Problem
- HDU 1914 The Stable Marriage Problem
- 稳定婚姻问题
- hdu 2068 RPG的错排(组合)
- 名人名言录(句子)
- 《礼记》的礼
- javascript面向对象学习笔记(四)——常用方法/属性
- eclipse的run as里面没有run on server
- CodeForces Gym 100971J 感觉题意有问题 BFS
- hdu 3723 Delta Wave
- Material Design系列,Behavior之BottomSheetBehavior与BottomSheetDialog
- 运行时简单使用-->获取对象的属性(class_copyIvarList)
- 深度学习试验之 多层感知器
- 威佐夫博弈(2堆)应用-- HDU 1527 取石子游戏
- hdu 2512 一卡通大冒险
- 走遍中国 —— 中国三大半岛
- 龙芯笔记本(电子工程专辑)
- c语言中字符数组和字符指针的区别:(一句话,一旦要修改就用字符数组)
- Swift的循环引用以weak