hdu 2819 Swap【完美二分匹配】
来源:互联网 发布:java断点调试快捷键 编辑:程序博客网 时间:2024/05/18 03:22
Swap
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2514 Accepted Submission(s): 900
Special Judge
Problem Description
Given an N*N matrix with each entry equal to 0 or 1. You can swap any two rows or any two columns. Can you find a way to make all the diagonal entries equal to 1?
Input
There are several test cases in the input. The first line of each test case is an integer N (1 <= N <= 100). Then N lines follow, each contains N numbers (0 or 1), separating by space, indicating the N*N matrix.
Output
For each test case, the first line contain the number of swaps M. Then M lines follow, whose format is “R a b” or “C a b”, indicating swapping the row a and row b, or swapping the column a and column b. (1 <= a, b <= N). Any correct answer will be accepted, but M should be more than 1000.
If it is impossible to make all the diagonal entries equal to 1, output only one one containing “-1”.
Sample Input
2
0 1
1 0
2
1 0
1 0
Sample Output
1
R 1 2
-1
Source
2009 Multi-University Training Contest 1 - Host by TJU
题目大意:给你一个 n*n的 矩阵,使用行交换或者列交换的方式来使得矩阵的主对角线(左上到右下)都是1.
分析:对于这个题,不需要最少的操作次数,那么我们无论是怎样做,只要是搞定了这个问题,我们就胜利了,对于输出-1的情况,我们来行列匹配看看能否使得所有行都有列来匹配就好,如果没有,那么就不是完美匹配,也就是说输出-1,否则就一定有结果输出。
思路:
完美匹配:如果一个图的某个匹配中,所有的顶点都是匹配点,那么它就是一个完美匹配。图 4 是一个完美匹配。显然,完美匹配一定是最大匹配(完美匹配的任何一个点都已经匹配,添加一条新的匹配边一定会与已有的匹配边冲突)。但并非每个图都存在完美匹配。
遍历所有行,如果都有列匹配的话,就是一个完美匹配,否则输出-1.
如果是完美匹配,那么交换pri【】数组,使得pri【i】=i、
AC代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxn = 505;int map[maxn][maxn]; //图的数组.int vis[maxn]; //标记数组.int pri[maxn];int ans[maxn*2][2];int n;int find(int x){ for(int i=1; i<=n; i++) { if(vis[i]==0&&map[i][x]) { vis[i]=1; if(pri[i]==-1||find(pri[i])) { pri[i]=x; return 1; } } } return 0;}int main(){ while(~scanf("%d",&n)) { memset(pri,-1,sizeof(pri)); for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) { scanf("%d",&map[i][j]); } } int ok=1; for(int i=1; i<=n; i++) { memset(vis,0,sizeof(vis)); if(find(i))continue; else ok=0; } if(ok==0){printf("-1\n");continue;} int cont=0; for(int i=1;i<=n;i++) { if(pri[i]==i)continue; for(int j=1;j<=n;j++) { if(pri[j]==i) { swap(pri[i],pri[j]); ans[cont][0]=i;ans[cont++][1]=j; } } } printf("%d\n",cont); for(int i=0;i<cont;i++) { printf("R %d %d\n",ans[i][0],ans[i][1]); } }}
- hdu 2819 Swap【完美二分匹配】
- HDU 2819 Swap 二分匹配
- hdu 2819 Swap 二分图匹配
- hdu-2819 Swap 二分图匹配
- HDU 2819 Swap(二分图匹配)
- Swap - HDU 2819 二分图匹配
- hdu 2819 Swap(二分图匹配)
- hdu-2819 Swap (二分匹配)
- hdu 2819 Swap (二分图匹配)
- HDU 2819 — Swap 二分匹配
- 【图论】【二分图匹配】[HDU 2819]Swap
- hdu 2819 Swap(二分图匹配)
- HDU 2819 Swap 二分图最大匹配
- HDU-2819 swap(二分图最大匹配)
- HDU 2819 Swap(最大二分图匹配)
- HDU-2819-Swap [二分匹配][输出路径]
- hdu 2819 Swap [二分图匹配]
- HDU 2819 Swap (最大二分匹配+输出路径)
- spring-data-jpa 中,如果使用了one-to-many , many-to-one的注释,会在Jackson进行json字符串化的时候出现错误
- 【HUSTOJ】1075: 拐角VIII
- iOS 关于已配对设备重新检测连接问题
- 小龙虾有寄生虫?煮熟了就没事
- leetcode--Rotate Array
- hdu 2819 Swap【完美二分匹配】
- IOS UIView 设置单边实现
- 圆
- Say Hello to CSDN
- poj-1177 Picture(矩形周长并,线段树+扫描线)
- KMP算法入门
- Ubuntu14.04 创建 wifi 热点
- 第十一周实践项目2.2-储存班长信息的学生类(如果将基类数据成员改为私有的)
- Android EventBus实战 没听过你就out了