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?

InputThere 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.OutputFor 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 题意:给你一个n*n的棋盘,值都是0或1,你可以任意交换两行两列,不要求最少步数,交换的结果让对角线都为1 思路:首先我们可以看出如果可以交换出结果,只依靠行交换或者列交换即可,然后我们要求的是一条对角线,意思是每列匹配的位置必须是固定的行,
我们把行当做二分图的左边,列当做二分图的右边,1的时候就是行和列直接有一条边,
-1问题我们直接用匈牙利算法求出,求出最大匹配是否是n,如果不是的话,说明有些行还没有匹配,所以不符合要求,如果最大匹配是n的话
我们可以去找属于自己的列,如图

说明满足第一行的匹配在第二列,但是我现在匹配的是第三列,这个时候就要把这两列交换一下,得到以下结果

这个时候第二行匹配错了,所以2,3行交换,然后得到的结果就匹配完成了

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
struct sss
{
int x,y;
}a[];
int n;
int mp[][];
int vis[];
int girl[];
int dfs(int x)
{
for(int i=;i<=n;i++)
{
if(mp[x][i]&&vis[i]==)
{
vis[i]=;
if(girl[i]==||dfs(girl[i]))
{
girl[i]=x;
return ;
}
}
}
return ;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
memset(girl,,sizeof(girl));
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
scanf("%d",&mp[i][j]);
}
int sum=;
for(int i=;i<=n;i++)
{
memset(vis,,sizeof(vis));
sum+=dfs(i);
}
if(sum!=n) printf("-1\n");//看是否满足全部匹配到了
else{
int num=;
for(int i=;i<=n;i++)
{
if(i!=girl[i])//灵活利用girl数组记录了匹配的行
{
for(int j=i+;j<=n;j++)//如果当前行匹配出错,找出需要匹配的行在哪里
{
if(i==girl[j])
{
int t;
t=girl[i];
girl[i]=girl[j];
girl[j]=t;
a[num].x=i;
a[num].y=j;
num++;
break;
}
}
}
}
printf("%d\n",num);
for(int i=;i<num;i++)
{
printf("C %d %d\n",a[i].x,a[i].y);
}
}
}
}
												

HDU - 2819 Swap(二分图最大匹配)的更多相关文章

  1. HDU 2819 ——Swap——————【最大匹配、利用linker数组、邻接表方式】

     Swap Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

  2. HDU 2819 - Swap - [二分图建模+最大匹配]

    题目链接:https://cn.vjudge.net/problem/HDU-2819 Given an N*N matrix with each entry equal to 0 or 1. You ...

  3. HDU - 2819 Swap (二分图匹配-匈牙利算法)

    题意:一个N*N的01矩阵,行与行.列与列之间可以互换.要求变换出一个对角线元素全为1的矩阵,给出互换的行号或列号. 分析:首先一个矩阵若能构成对角线元素全为1,那么矩阵的秩为N,秩小于N的情况无解. ...

  4. HDU 2819 Swap(行列式性质+最大匹配)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2819 题目大意:给你一个n*n的01矩阵,问是否可以通过任意交换整行或者整列使得正对角线上都是1. ...

  5. HDU 2819 Swap(二分图匹配)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=2819 [题目大意] 给出一个棋盘,由白格子和黑格子组成,可以交换棋盘的行列, 使得其主对角线为黑格 ...

  6. E - Swap - hdu 2819(简单二分图匹配)

    题意:如果可以交换行列,问主对角线能不能全为1 分析:要想主对角线全为1很明显要有N个行列不想同的点就行了,可以用二分图匹配计算出来多能有几个.如果小与N就不能.输出要是对的就行,不必和答案一样 ** ...

  7. hdu 2819 Swap

    Swap http://acm.hdu.edu.cn/showproblem.php?pid=2819 Special Judge Problem Description Given an N*N m ...

  8. HDU 2819 — Swap 二分匹配

    Swap Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  9. HDU 2063 (二分图最大匹配)

    RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做partner和她同坐.但是,每个女孩 ...

随机推荐

  1. No address associated with hostname

    java.net.UnknownHostException: Unable to resolve host "www.baidu.com": No address associat ...

  2. 20181013xlVba计算优秀率及合格率

    Sub 计算高一优秀合格率() Dim Wb As Workbook Dim Sht As Worksheet Dim oSht As Worksheet Dim dOs As Object 'Out ...

  3. confirm提示弹出确定和取消按钮

    js----> var con = confirm('这是一个确定加取消的提示窗口') if(con==true){ document.write("点击了确定按钮") }e ...

  4. JavaScript 入门笔记

    JavaScript   1.JS和DOM的关系 浏览器有渲染html代码的功能,把html源码在内存里形成一个DOM对象,就是文档对象 浏览器内部有一个JS的解释器/执行/引擎,如chrome用v8 ...

  5. 6.4 操作契约 Operation Contracts

    4.操作契约 Operation Contracts  “用例描述”的补充  强调: 用例中重要的动作,其开始与结束是需要一些约束 4.5 操作契约的后置条件  定义Definition  后 ...

  6. 前端Vue之vue的基本操作

    1.1 vue.js的快速入门使用 vue.js是目前前端web开发最流行的工具库之一,由尤雨溪在2014年2月发布的. 另外几个常见的工具库:react.js /angular.js 官方网站: 中 ...

  7. MyBatis中mybatis-generator代码生成的一般过程

    MyBatis框架的使用,可以参考我的文章: https://blog.csdn.net/JayInnn/article/details/81746571(基于Mybatis实现一个查库的接口) ht ...

  8. centos7 keepalived+nginx实现vip漂移高可用

    一.Keepalived 简要介绍 Keepalived 是一种高性能的服务器高可用或热备解决方案, Keepalived 可以用来防止服务器单点故障的发生,通过配合 Nginx 可以实现 web 前 ...

  9. js 处理 cookie的存储与删除

    <script> //JS操作cookies方法! //写cookies function setCookie(c_name, value, expiredays){ var exdate ...

  10. 15. 3Sum C++

    参考资料: https://leetcode.com/problems/3sum/discuss/7402/Share-my-AC-C%2B%2B-solution-around-50ms-O(N*N ...