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意义下最多两个,于是我们可以用二分图匹配来判断. 那字典序最小呢? 回顾一下二分图匹配的算法:网络流?貌似不 ...
随机推荐
- leetcode-位1的个数(位与运算)
位1的个数 编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量). 示例 : 输入: 11 输出: 3 解释: 整数 11 的二进制表示为 00000 ...
- JavaScript 数组操作方法 和 ES5数组拓展
JavaScript中数组有各种操作方法,以下通过举例来说明各种方法的使用: 数组操作方法 push 在数组最后添加一个元素 var arr=[3,4,5,6] console.log(arr) // ...
- c字符指针与字符数组的区别
1.定义 char *pchar; //定义了指针,没赋值之前不能使用.如果:printf("*pchar:%c\n",*pchar); 出现段错误Segmentation fa ...
- matlab中设置colorbar为几种规定颜色
我们可以通过修改colormap的值来达到这种目的. 一般来说colormap的值是64*3的矩阵,64代表64种颜色,3列是这种颜色的RGB值,不过归一化了. 如果你想将colorbar颜色设成6种 ...
- 常用linux命令相关
[查看端口] netstat -tlnp netstat命令 netstat -an | grep 3306 3306替换成需要grep的端口号 lsof命令 通过list open file命令可以 ...
- Ubuntu下FileZilla的安装
FileZilla是一个免费而且开源的FTP客户端软件,共有两种版本:客户端版本.服务器版本.FileZilla有条理的界面和管理多站点的简化方式使得FileZilla Client成为一个方便高效的 ...
- LintCode-72.中序遍历和后序遍历树构造二叉树
中序遍历和后序遍历树构造二叉树 根据中序遍历和后序遍历树构造二叉树 注意事项 你可以假设树中不存在相同数值的节点 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下的树: ...
- Spring Boot(四)@EnableXXX注解分析
在学习使用springboot过程中,我们经常碰到以@Enable开头的注解,其实早在Spring3中就已经出现了类似注解,比如@EnableTransactionManagement.@ Enabl ...
- HDU 2115 I Love This Game
http://acm.hdu.edu.cn/showproblem.php?pid=2115 Problem Description Do you like playing basketball ? ...
- Agile.Net 组件式开发平台 - 服务开发示例
在上一篇文章中已经讲解了组件的开发,这篇文章讲解平台服务开发. Agile.Net开发管理平台项目,已经托管在开源中国码云平台(http://git.oschina.net) 登陆码云平台进入项目主页 ...