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意义下最多两个,于是我们可以用二分图匹配来判断. 那字典序最小呢? 回顾一下二分图匹配的算法:网络流?貌似不 ...
随机推荐
- phpanalysis提取关键字
最近在开发一个文章模块功能,设计那边提出要给文章生成对应标签,用于文章关联推送,这里和大家分享一下实现过程: 这里需要用到PHPAnalysis,下载链接如下 链接:https://pan.baidu ...
- 【button】 按钮组件说明
原型: <button size="[default | mini]" type="[primary | default | warn]" plain=& ...
- Idea Live Templates
常用live templates 模板 注释 : * * @param $params$ * @return $return$ * $date$ $time$ chiyuanzhen743 */ lo ...
- hosts_allow配置了却不生效
hosts_allow配置了却不生效 配置了两台白名单的机器,一台生效一台不生效,google后的结果都是更新libwrap.so 安装openssh等等..(问题还是没有解决) 经过对比发现,原来 ...
- P4编程环境搭建
本文参照了sdnlab上相关文章的搭建推荐. 使用的系统环境为ubuntu 18.04 组件介绍 主要安装五个组件: BMv2:是一款支持P4编程的软件交换机 p4c:是一款P4的编译器 PI:是P4 ...
- Java容器之Map接口
Map 接口: 1. 实现 Map 接口的类是用来存储键-值(key-value)对: 2. Map 接口的实现类有 HashMap 和 TreeMap 等: 3. Map 类中存储的键-值对,通过键 ...
- Linux内核策略介绍学习笔记
主要内容 硬件 策略 CPU 进程调度.系统调用.中断 内存 内存管理 外存 文件IO 网络 协议栈 其他 时间管理 进程调度 内核的运行时间 系统启动.中断发生.系统调用以及内核线程. 进程和线程的 ...
- 最近面试前端面试题整理(css部分)
对最近面试的面试题坐下总结: 一,css部分 1,html元素的垂直居中 答案: <div id="box"> <div> 测试 </div> ...
- Oracle AWR日志使用
SQL>@?/rdbms/admin/awrrpt.sql Specify the Report Type ~~~~~~~~~~~~~~~~~~~~~~~ Would you like an H ...
- 使用bat执行java项目
前提:java项目要有main方法 类似写法如下: set JAVA_HOME=C:\jdk1.6 set LIB_HOME=. set JAVA_JAR=. set JAVA_JAR=%JAVA_J ...