题目链接:https://cn.vjudge.net/problem/HDU-2819

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的01矩阵,要求你通过一些行交换或者列交换使得矩阵的主对角线上全为1;

题解:

不难想象,进行行列缩点,如果某个位置aij等于1,就往二分图中加入一条边( i , j ),如果求出最大匹配等于N,说明可以通过行列的变换得到目标矩阵;

然后根据匹配情况,输出相应的交换步骤即可;

AC代码:

#include<bits/stdc++.h>
#define MAX 103
#define INF 0x3f3f3f3f
using namespace std;
int n,matrix[MAX][MAX]; struct Hopcroft_Karp{
int edge[MAX][MAX],Mx[MAX],My[MAX],Nx,Ny;
int dx[MAX],dy[MAX],dis;
bool vis[MAX];
void init(int uN,int vN)
{
Nx=uN, Ny=vN;
for(int i=;i<=uN;i++) for(int j=;j<=vN;j++) edge[i][j]=;
}
void addedge(int u,int v){edge[u][v]=;}
bool searchP()
{
queue<int> Q;
dis=INF;
memset(dx,-,sizeof(dx));
memset(dy,-,sizeof(dy));
for(int i=;i<=Nx;i++)
{
if(Mx[i]==-)
{
Q.push(i);
dx[i]=;
}
}
while(!Q.empty())
{
int u=Q.front();Q.pop();
if(dx[u]>dis) break;
for(int v=;v<=Ny;v++)
{
if(edge[u][v] && dy[v]==-)
{
dy[v]=dx[u]+;
if(My[v]==-) dis=dy[v];
else
{
dx[My[v]]=dy[v]+;
Q.push(My[v]);
}
}
}
}
return dis!=INF;
}
bool dfs(int u)
{
for(int v=;v<=Ny;v++)
{
if(!vis[v] && edge[u][v] && dy[v]==dx[u]+)
{
vis[v]=;
if(My[v]!=- && dy[v]==dis) continue;
if(My[v]==- || dfs(My[v]))
{
My[v]=u;
Mx[u]=v;
return true;
}
}
}
return false;
}
int max_match()
{
int ret=;
memset(Mx,-,sizeof(Mx));
memset(My,-,sizeof(My));
while(searchP())
{
memset(vis,,sizeof(vis));
for(int i=;i<=Nx;i++) if(Mx[i]==- && dfs(i)) ret++;
}
return ret;
}
}HK; int main()
{
while(scanf("%d",&n)!=EOF)
{
HK.init(n,n);
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
scanf("%d",&matrix[i][j]);
if(matrix[i][j]) HK.addedge(i,j);
}
} int max_match=HK.max_match();
if(max_match==n)
{
queue< pair<int,int> > output;
for(int j=;j<=n;j++)
{
int u1=j, u2=HK.My[j];
int v1=j, v2=HK.Mx[j];
if(u1!=u2)
{
output.push(make_pair(u1,u2));
HK.My[v1]=u1, HK.My[v2]=u2;
HK.Mx[u1]=v1, HK.Mx[u2]=v2;
}
} printf("%d\n",output.size());
while(!output.empty())
{
printf("R %d %d\n",output.front().first,output.front().second);
swap(matrix[output.front().first],matrix[output.front().second]);
output.pop();
}
}
else printf("-1\n");
}
}

PS.刚开始还以为HK算法的模板出错了,吓死了……

PS.注意更改匹配情况时,match数组的变更;

HDU 2819 - Swap - [二分图建模+最大匹配]的更多相关文章

  1. HDU - 2819 Swap(二分图最大匹配)

    Given an N*N matrix with each entry equal to 0 or 1. You can swap any two rows or any two columns. C ...

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

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

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

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

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

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

  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 2828 Lamp 二分图的最大匹配 模型题

    http://acm.hdu.edu.cn/showproblem.php?pid=2828 给定n个灯,m个开关,使得每栈灯亮,前提是控制这栈灯的开关的状态是其中一个.(题目应该都看得懂) 其实我想 ...

随机推荐

  1. 【NodeJS】热更新

    1.npm install -g supervisor 2.supervisor WellDetect.js

  2. 【NodeJS】http-server.cmd

    npm install http-server @echo off  start cmd /k "D:\Program Files\nodejs\node_global\http-serve ...

  3. Websphere安装配置与项目部署

    0x01 下载安装 1.在IBM官网下载安装包 打开下载地址,需注册账户,登录后,选择Windows 64-bit下载 2.解压缩,运行install.exe文件 3.输入刚才注册的用户名和密码,并输 ...

  4. 【RF库XML测试】Element Attribute Should Be

    Name:Element Attribute Should BeSource:XML <test library>Arguments:[ source | name | expected ...

  5. Splash js_enabled 属性

    js_enabled属性是 Splash 的 JavaScript 执行开关,可以将其配置为 true 或 false 来控制是否执行 JavaScript 代码,默认为 true .例如,这里禁止执 ...

  6. ARM入门最好的文章

    一 首先说说arm的发展  可以用一片大好来形容,翻开各个公司的网站,招聘里面嵌入式占据了大半工程师职位.广义的嵌入式无非几种:传统的什么51.avr.pic称做嵌入式微控制器:arm是嵌入式微处理器 ...

  7. RabbitMQ与java、Spring结合实例详细讲解

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文介绍了rabbitMq,提供了如何在Ubuntu下安装RabbitMQ 服务的方法. ...

  8. 转 Mysql性能优化教程

    Mysql性能优化教程 背景及目标 厦门游家公司(4399.com)用于员工培训和分享. 针对用户群为已经使用过mysql环境,并有一定开发经验的工程师 针对高并发,海量数据的互联网环境. 本文语言为 ...

  9. Matlab练习——寻找完全数

    clc; clear; wq = []; : sum = ; k = ; : i / sum = sum + j; end end == i wq=[wq i]; end end disp(['2至1 ...

  10. window下遍历并修改文件

    今天需要写一个遍历文件夹下的所有文件,试了试以前的方法竟然报错了.重新改了一下. #include <iostream> #include <stdlib.h> #includ ...