题意:交换任意两行或两列,使主对角线全为1。

分析:

1、主对角线都为1,可知最终,第一行与第一列匹配,第二行与第二列匹配,……。

2、根据初始给定的矩阵,若Aij = 1,则说明第i行与第j列匹配,据此求最大匹配数cnt,若cnt==N,才可通过交换使主对角线都为1。

3、交换时,可只交换行或只交换列。如:只交换列,行不变(顺序为1,2,3,……,n),那么对于列,只需要根据选择排序,将每行一开始匹配的列的顺序最终也变成1,2,3,……,n即可,因为是选择排序,每次选择第i小的交换到第i个位置,因此最多只需要交换N次。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-9;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100 + 10;
const int MAXT = 1000 + 10;
using namespace std;
int a[MAXN][MAXN];
bool used[MAXN];
int match[MAXN];
vector<pair<int, int> > v;
int N;
bool dfs(int x){
for(int i = 1; i <= N; ++i){
if(a[x][i] && !used[i]){
used[i] = true;
if(match[i] == -1 || dfs(match[i])){
match[i] = x;
return true;
}
}
}
return false;
}
int hungary(){
int cnt = 0;
for(int i = 1; i <= N; ++i){
memset(used, false, sizeof used);
if(dfs(i)) ++cnt;
}
return cnt;
}
int main(){
while(scanf("%d", &N) == 1){
memset(match, -1, sizeof match);
v.clear();
for(int i = 1; i <= N; ++i){
for(int j = 1; j <= N; ++j){
scanf("%d", &a[i][j]);
}
}
int cnt = hungary();
if(cnt != N){
printf("-1\n");
continue;
}
for(int i = 1; i <= N; ++i){
int tmp = i;
for(int j = i + 1; j <= N; ++j){
if(match[tmp] > match[j]) tmp = j;
}
if(tmp == i) continue;
v.push_back(pair<int, int>(i, tmp));
swap(match[i], match[tmp]);
}
int len = v.size();
printf("%d\n", len);
for(int i = 0; i < len; ++i){
printf("C %d %d\n", v[i].first, v[i].second);
}
}
return 0;
}

  

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

  1. HDU 2819 Swap (二分匹配+破输出)

    题意:给定上一个01矩阵,让你变成一个对角全是 1 的矩阵. 析:二分匹配,把行和列看成两个集合,用匈牙利算法就可以解决,主要是在输出解,在比赛时一紧张不知道怎么输出了. 输出应该是要把 match[ ...

  2. HDU 2819 — Swap 二分匹配

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

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

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

  4. HDU 2819 Swap (行列匹配+输出解)

    题意:是否能使对角线上全是1 ,这个简单直接按行列匹配.难在路径的输出,我们知道X,Y左右匹配完了之后,不一定是1–1,2–2,3–3--这种匹配.可能是1–3,2–1,3–2,我们要把他们交换成前一 ...

  5. hdu 2819 Swap

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

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

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

  7. hdu 1281棋盘游戏(二分匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281   Problem Description 小希和Gardon在玩一个游戏:对一个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 3468 BFS+二分匹配

    九野的博客,转载请注明出处 http://blog.csdn.net/acmmmm/article/details/10966383 开始建图打搓了,参考了大牛的题解打的版本比较清爽,后来改的基本雷同 ...

随机推荐

  1. 第1节 kafka消息队列:2、kafka的架构介绍以及基本组件模型介绍

    3.kafka的架构模型 1.producer:消息的生产者,主要是用于生产消息的.主要是接入一些外部的数据源,从外部获取数据,比如说我们可以从flume获取数据,还可以通过ftp传入数据等,还可以通 ...

  2. vue2 Excel导出数据 js-xlsx的使用

    vue2 Excel导出数据 js-xlsx的使用 https://www.jianshu.com/p/ea115a8e9107 小世界最温暖 关注 2018.11.19 16:08 字数 280 阅 ...

  3. lunix下的redis安装

    https://blog.csdn.net/qq_35992900/article/details/82950157

  4. Django3.0中向后不兼容的更改

    3.0中向后不兼容的更改 数据库后端API 本节描述了第三方数据库后端中可能需要的更改. 现在的第二个参数DatabaseIntrospection.get_geometry_type()是行描述,而 ...

  5. P1481 魔族密码(LIS变形)

    题目描述(题目链接:https://www.luogu.org/problem/P1481) 风之子刚走进他的考场,就…… 花花:当当当当~~偶是魅力女皇——花花!!^^(华丽出场,礼炮,鲜花) 风之 ...

  6. 如何修改 app.config 的配置信息

    如何修改 app.config 的配置信息 收藏 最问这个问题的人有点多,其实 .Net 提供了这样的功能我们可以在 app.config 中 userSettings 节点中保存我们的应用程序设置信 ...

  7. Redis Cluster 4.0.9 集群安装搭建

    Redis Cluster 4.0.9集群搭建步骤:yum install -y gcc g++ gcc-c++ make openssl cd redis-4.0.9 make mkdir -p / ...

  8. Flutter Web环境搭建

    接上篇Flutter Windows下AndroidStudio环境搭建 1.https://github.com/flutter/flutter_web 下载放到本地路径下 2.系统Path增加(根 ...

  9. mysql 模糊查询中包含特殊字符查询

  10. SpringBoot-配置Java方式

    SpringBoot中使用Java方式配置步骤如下: 在类上加入@Configuration注解,代表作为配置类 在该类方法上加入@Bean注解,代表将方法返回的Bean加入Spring容器 在该类中 ...