HDU2819:Swap(二分图匹配)
Swap
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4932 Accepted Submission(s): 1836
Special Judge
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2819
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:
R 1 2
-1
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std; const int N = ;
int map[N][N],link[N][N],match[N],check[N],r[N],a[N],b[N];
int n,ans,tot; inline void init(){
memset(map,,sizeof(map));memset(link,,sizeof(link));
memset(match,-,sizeof(match));ans=;tot=;
memset(a,,sizeof(a));memset(b,,sizeof(b));
} inline int dfs(int x){
for(int i=;i<=n;i++){
if(link[x][i] && !check[i]){
check[i]=;
if(match[i]==- || dfs(match[i])){
match[i]=x;
return ;
}
}
}
return ;
} inline void Swap(){
for(int i=;i<=n;i++){
if(match[i]!=i){
a[++tot]=i;b[tot]=match[i];
for(int j=;j<=n;j++){
if(match[j]==i){
swap(match[i],match[j]);
break ;
}
}
}
}
} int main(){
while(scanf("%d",&n)!=EOF){
init();
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
scanf("%d",&map[i][j]);
if(map[i][j]) link[i][j]=;
}
}
for(int i=;i<=n;i++){
memset(check,,sizeof(check));
if(dfs(i)) ans++;
}
if(ans!=n){
puts("-1");continue ;
}
Swap();
printf("%d\n",tot);
for(int i=;i<=tot;i++) printf("R %d %d\n",a[i],b[i]);
}
return ;
}
HDU2819:Swap(二分图匹配)的更多相关文章
- HDU2819 Swap —— 二分图最大匹配
题目链接:https://vjudge.net/problem/HDU-2819 Swap Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...
- hdu-2819.swap(二分匹配 + 矩阵的秩基本定理)
Swap Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU - 2819 Swap (二分图匹配-匈牙利算法)
题意:一个N*N的01矩阵,行与行.列与列之间可以互换.要求变换出一个对角线元素全为1的矩阵,给出互换的行号或列号. 分析:首先一个矩阵若能构成对角线元素全为1,那么矩阵的秩为N,秩小于N的情况无解. ...
- HDU2819(二分图匹配,记录过程)
Swap Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu2819二分图匹配
Given an N*N matrix with each entry equal to 0 or 1. You can swap any two rows or any two columns. C ...
- E - Swap - hdu 2819(简单二分图匹配)
题意:如果可以交换行列,问主对角线能不能全为1 分析:要想主对角线全为1很明显要有N个行列不想同的点就行了,可以用二分图匹配计算出来多能有几个.如果小与N就不能.输出要是对的就行,不必和答案一样 ** ...
- HDU 2819 Swap(二分图匹配)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=2819 [题目大意] 给出一个棋盘,由白格子和黑格子组成,可以交换棋盘的行列, 使得其主对角线为黑格 ...
- Bzoj 1562: [NOI2009]变换序列 匈牙利算法,二分图匹配
题目: http://cojs.tk/cogs/problem/problem.php?pid=409 409. [NOI2009]变换序列 ★★☆ 输入文件:transform.in 输出文 ...
- 【BZOJ3140】消毒(二分图匹配)
[BZOJ3140]消毒(二分图匹配) 题面 Description 最近在生物实验室工作的小T遇到了大麻烦. 由于实验室最近升级的缘故,他的分格实验皿是一个长方体,其尺寸为abc,a.b.c 均为正 ...
- [NOI2009]变换序列(二分图匹配)
我们先不考虑字典序最小,先来求出一种可行解. 不难发现,对于每一个i值,它所对应的T值在模n意义下最多两个,于是我们可以用二分图匹配来判断. 那字典序最小呢? 回顾一下二分图匹配的算法:网络流?貌似不 ...
随机推荐
- [转]bashrc与profile区别
作者:KornLee 2005-02-03 15:49:57 来自:Linux先生 /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/pro ...
- 加密SecurityHelper
接下来给大家分享一下我用的加密helper,现在只用的md5加密的方法,网上很多方法找到的时候加密完了会变成乱码,这样对于密码这种字段保存的时候就会出错.其实只需要把加密完的byte字节转化成16位就 ...
- Java 单例模式探讨
以下是我再次研究单例(Java 单例模式缺点)时在网上收集的资料,相信你们看完就对单例完全掌握了 Java单例模式应该是看起来以及用起来简单的一种设计模式,但是就实现方式以及原理来说,也并不浅显哦. ...
- POJ 2069 Super Star(计算几何の最小球包含+模拟退火)
Description During a voyage of the starship Hakodate-maru (see Problem 1406), researchers found stra ...
- 【转】jQuery最佳实践
上周,我整理了<jQuery设计思想>. 那篇文章是一篇入门教程,从设计思想的角度,讲解"怎么使用jQuery".今天的文章则是更进一步,讲解"如何用好jQu ...
- 《javascript模式--by Stoyan Stefanov》书摘--基本技巧
一.基本技巧 1,变量释放的副作用 a.使用var创建的全局变量(在函数外部创建)不能删除. b.不使用var创建的隐含全局变量(尽管在函数内部创建)可以删除. // 定义三个全局变量 var glo ...
- 关于14道魔鬼js考题的整理
1.(function(){ return typeof arguments })(); 这里返回时是argument类型,它是个类数组,也就对象,所以是object,准确谁是[object argu ...
- Bacon's Cipher(培根密码)
Description Bacon's cipher or the Baconian cipher is a method of steganography (a method of hiding a ...
- 2017秋-软件工程第四次作业(2)-结对使用TDD框架完成单元测试
第一次接触“单元测试”这个要求,我和队友学习了一些示例后开始操作.如下展示一些建立单元测试的过程.Step1:右键单击[解决方案]->左键单击[添加(D)]->[新建项目(N)]. Ste ...
- Android蓝牙开发浅谈(转)
http://www.eoeandroid.com/thread-18993-1-1.html 对于一般的软件开发人员来说,蓝牙是很少用到的,尤其是Android的蓝牙开发,国内的例子很少 A ...