Problem 1202 信与信封问题 from http://acm.fzu.edu.cn/problem.php?pid=1202
2012-09-11 15:29
483 查看
![](http://acm.fzu.edu.cn/image/problem.gif)
Problem 1202 信与信封问题
Accept: 207 Submit: 1069
Time Limit: 1000 mSec Memory Limit : 32768 KB
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Problem Description
John先生晚上写了n封信,并相应地写了n个信封将信装好,准备寄出。但是,第二天John的儿子Small John将这n封信都拿出了信封。不幸的是,Small John无法将拿出的信正确地装回信封中了。编程任务
将Small John所提供的n封信依次编号为1,2,...,n; 且n个信封也依次编号为1,2,...,n。假定Small John能提供一组信息:第i封信肯定不是装在信封j中。请编程帮助Small John,尽可能多地将信正确地装回信封。
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Input
本题有多组输入数据,你必须处理到EOF为止。每组数据的第一行是一个整数n(n≤100)。信和信封依次编号为1,2,…,n。
接下来的各行中每行有2个数i和j,表示第i封信肯定不是装在第j个信封中。输入最后一行是2个0,表示结束。
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Output
对于每组数据,输出的各行中每行有2个数i和j,表示第i封信肯定是装在第j个信封中。请按信的编号i从小到大顺序输出。若不能确定正确装入信封的任何信件,则输出“none”。每组输出结束之后,输出一个空行。
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Sample Input
31 21 32 10 0
![](http://acm.fzu.edu.cn/image/prodesc.gif)
Sample Output
1 1匈牙利匹配。去掉关键边那么匹配数会减少。可知该边是确定的。
#include <iostream> #include <string> #include <algorithm> #include <memory.h> #include <cstdio> #include <cstdlib> #include <vector> using namespace std; int link[101]; int v[101]; int map[101][101]; int n; int back_link[101]; struct Node{ int a,b; bool operator<(const Node & node)const{ return a<node.a; } }; Node nodes[101]; bool find(int x){ for(int i=1;i<=n;++i){ if(!map[x][i] && !v[i]){ v[i] = 1; if(!link[i] || find(link[i])){ link[i] = x; return true; } // not return false } } return false;/*should return false*/ } int hungary(){ int ans = 0; memset(link, 0, sizeof(link)); for(int i=1;i<=n;++i){ memset(v, 0, sizeof(v));/*concentrate on this*/ if(find(i)) ans++; } return ans; } int main(){ //freopen("in.txt", "r", stdin); int a, b; while(cin>>n){ memset(map, 0, sizeof(map)); while(scanf("%d%d", &a, &b), a+b){ map[a][b] = 1; } int ans = hungary(); memcpy(back_link, link, sizeof(link)); int cnt = 0; for(int i=1;i<=n;++i){ map[back_link[i]][i] = 1; if(hungary()!=ans){ Node node; node.a = back_link[i]; node.b = i; nodes[cnt++] = node; } map[back_link[i]][i] = 0; } sort(nodes, nodes+cnt); if(cnt==0) cout<<"none"<<endl; else{ for(int i=0;i<cnt;++i){ printf("%d %d\n", nodes[i].a, nodes[i].b); } } printf("\n"); } //fclose(stdin); }
相关文章推荐
- Problem 1582 众数问题 from http://acm.fzu.edu.cn/problem.php?pid=1582
- Problem 1207 半数集问题 from http://acm.fzu.edu.cn/problem.php?pid=1207
- Problem 1453 Bignum Arithmetic from http://acm.fzu.edu.cn/problem.php?pid=1453
- Problem 1057 ab from http://acm.fzu.edu.cn/problem.php?pid=1057
- Problem 1410 变位词 from http://acm.fzu.edu.cn/problem.php?pid=1410
- Problem 1478 环形整数串 from http://acm.fzu.edu.cn/problem.php?pid=1478
- http://acm.fzu.edu.cn/problem.php?pid=1564 C(n,k)和素数P的问题
- Problem 1046 Tempter of the Bone, from http://acm.fzu.edu.cn/problem.php?pid=1046
- Problem 1607 Greedy division from http://acm.fzu.edu.cn/problem.php?pid=1607
- 矩阵问题&&http://acm.hdu.edu.cn/showproblem.php?pid=1575
- 最短路径问题 多加了一个费用 http://acm.hdu.edu.cn/showproblem.php?pid=3790
- http://acm.fzu.edu.cn/problem.php?pid=2113 数位DP
- http://acm.fzu.edu.cn/problem.php?pid=1698 最大乘积
- http://acm.hdu.edu.cn/showproblem.php?pid=3591&&背包问题
- http://acm.hdu.edu.cn/showproblem.php?pid=1203(背包问题)
- fzu 1402 中国剩余定理 http://acm.fzu.edu.cn/problem.php?pid=1402
- 士兵队列训练问题http://acm.hdu.edu.cn/showproblem.php?pid=1276
- http://acm.hdu.edu.cn/showproblem.php?pid=2844&&背包问题
- http://acm.hdu.edu.cn/showproblem.php?pid=3790&&最短路径问题
- http://acm.hdu.edu.cn/showproblem.php?pid=1272