Swap

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5728    Accepted Submission(s): 2157
Special Judge

Problem 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
2
0 1
1 0
2
1 0
1 0
 
Sample Output
1
R 1 2
-1
 
Source
 
Recommend
gaojie

这个题一开始没有想到是二分匹配,主要是不知道这个定理.

矩阵的秩:

  一般矩阵经过初等变换之后得到的阶梯矩阵中,不全为0的行数为行秩,不全为0的列的个数为列秩,一个矩阵的行秩等于列秩等于矩阵的秩.

矩阵的秩的一般定理:

  初等变换不能改变矩阵的秩.

 /*************************************************************************
> File Name: hdu-2819.swap.cpp
> Author: CruelKing
> Mail: 2016586625@qq.com
> Created Time: 2019年09月02日 星期一 17时56分18秒
本题大意:给定一个n × n 的0 or 1矩阵,问你若只交换某些行和列(行之间进行交换,列之间进行交换)能否使得最后的矩阵在从左上角到右下角的对角线上元素都为1.
本题思路:线性代数里我们学过初等变换不会改变矩阵的秩,因此如果一个矩阵满秩,那么必定有解,如何判断矩阵是否满秩呢?因为是0 or 1矩阵,因此,原矩阵经过变换后得到的阶梯矩阵也都只有0 or 1,如果其中某两列的元素完全相等,或者某一列中本来就不存在1那么转换之后的阶梯矩阵自然也就不会满秩。所以我们可以对行和列缩点,判断是否每一列都有独特的一行与之对应,如果说有两列的1都存在与同一行,那么必定无法匹配,所以我们就想到了二分匹配,让行缩点为x,列缩点为y,匹配之后,如果最大匹配为n那么匹配成功,否则说明矩阵非满秩,如何输出解呢?我们遍历所有列,如果发现有一列与他匹配的行不与他的标号相等,则说明这个(i, j)位于第i行第j列,那么我们把第j列换到第i列即可.也就是交换j与linker[j],交换了之后肯定要改变其匹配状态,也就是让匹配i的行和匹配j的行再互换。
************************************************************************/ #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + , maxm = + , inf = 0x3f3f3f3f; typedef pair<int, int> pii;
int n, linker[maxn], g[maxn][maxn];
bool used[maxn];
pii path[maxm];
int tot; bool dfs(int u) {
for(int v = ; v <= n; v ++) {
if(!used[v] && g[u][v]) {
used[v] = true;
if(linker[v] == - || dfs(linker[v])) {
linker[v] = u;
return true;
}
}
}
return false;
} int main() {
while(~scanf("%d", &n)) {
tot = ;
for(int i = ; i <= n; i ++) {
for(int j = ; j <= n; j ++) {
scanf("%d", &g[i][j]);
}
}
memset(linker, -, sizeof linker);
int res = ;
for(int i = ; i <= n; i ++) {
memset(used, false, sizeof used);
if(dfs(i)) res ++;
}
if(res != n) printf("%d\n", -);
else {
for(int i = ; i <= n; i ++) {
while(i != linker[i]) {
path[tot ++] = make_pair(i, linker[i]);
swap(linker[i], linker[linker[i]]);
}
}
printf("%d\n", tot);
for(int i = ; i < tot; i ++) {
printf("C %d %d\n", path[i].first, path[i].second);
}
}
}
return ;
}

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 3468 BFS+二分匹配

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

  9. HDU - 2819 Swap(二分匹配)

    题意:交换任意两行或两列,使主对角线全为1. 分析: 1.主对角线都为1,可知最终,第一行与第一列匹配,第二行与第二列匹配,……. 2.根据初始给定的矩阵,若Aij = 1,则说明第i行与第j列匹配, ...

随机推荐

  1. Keepalived+Nginx+tomcat实现主备+负载

    部署系统: Red Hat Enterprise Linux Server release 7.0 软件版本:apache-tomcat-7.0.92.tar.gzkeepalived-2.0.11. ...

  2. Git Fast Forward 和 no fast foward

    如果执行了 Fast Forward,开发者根本不会看到这个分支,就像在 master 直接 commit 一样.

  3. QueryDSL通用查询框架学习目录

    转载自恒宇的博客 https://www.jianshu.com/p/99a5ec5c3bd5

  4. CSRF拦截

    CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click attack/session riding,缩写为:CSRF/XSRF. 事实上 ...

  5. vue token 过期处理

    1.登陆成功后储存token  可以利用 vuex 储存token 2.利用路由守卫处理 router.beforeEach((to, from, next) => {   })     3.我 ...

  6. gremlin语言语法--学习笔记

    学习gremlin语言的目的:测试图数据,支持gremlin语句,所以必须系统学习一下!!!! 一.基础查询 g.V() 查询所有的顶点 g.V(3) 查询顶点id为3的点.字符串id的要到引号V(& ...

  7. 【NOIP2016提高A组8.12】总结

    惨败!!!! 第一题是一道神奇的期望问题. 第二题,发现"如果两个部门可以直接或间接地相互传递消息(即能按照上述方法将信息由X传递到Y,同时能由Y传递到X),我们就可以忽略它们之间的花费&q ...

  8. jquery +点击按钮,切换div内容,按钮加高亮

    html: <div class="dw4"> <span class="dw">单位(次)</span> <div ...

  9. Java——容器(Collection)

    Collection是一个接口,定义了一系列的方法.   [常见方法]  

  10. Java 静态初始化块等的执行顺序

    实例代码 package text; class Root { static{ System.out.println("Root的静态初始化块"); } { System.out. ...