Swap

http://acm.hdu.edu.cn/showproblem.php?pid=2819


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
 

题目大意:一个n*n的0 1矩阵,交换行或列使其主对角线(左对角线)都为1,问需要多少次交换,怎样交换

R a b表示第a行和第b行交换, C a b表示第a列和第b列交换

转换为二分匹配:

主对角线都为1即使G[a][b] = 1,此时a应该与b相等,a表示横坐标,b表示纵坐标(1<=a<=n, 1<=b<=n)

将横坐标存入集合X中将纵坐标存入集合Y中,(x,y)为1时,则x和y之间有一条连线,即x与y匹配

如果使主对角线都为1,则必须使其最大匹配等于n,否则就不能实现

当x == y但x与y不匹配时则交换,并记录如何交换,统计交换次数(即while(used[i] != i){a[j] = i;b[j] = used[i];swap(used[a[j]], used[b[j]]);j++})

无论是行交换还是列交换能得到结果的最终都能得到结果,但注意输出要保持一致

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define N 110
#define INF 0x3f3f3f3f using namespace std; int G[N][N], vis[N], used[N];
int n; bool Find(int u)//匈牙利算法
{
for(int i = ; i <= n ; i++)
{
if(!vis[i] && G[u][i])
{
vis[i] = ;
if(!used[i] || Find(used[i]))
{
used[i] = u;
return true;
}
}
}
return false;
} int main()
{
int a[], b[];
while(~scanf("%d", &n))
{
memset(G, , sizeof(G));
for(int i = ; i <= n ; i++)
for(int j = ; j <= n ; j++)
scanf("%d", &G[i][j]);
memset(used, , sizeof(used));
int ans = ;
for(int i = ; i <= n ; i++)
{
memset(vis, , sizeof(vis));
if(Find(i))
ans++;
}//求最大匹配
if(ans < n)
{
printf("-1\n"); continue;
}
int j = ;
for(int i = ; i <= n ; i++)
{
while(used[i] != i)//当横纵坐标相等但横坐标与纵坐标不匹配(这里注意是while而不是if,因为这个问题Wa了很多次,一直没发现)
{
a[j] = i;//a记录要交换的行
b[j] = used[i];//b记录要交换的列
swap(used[a[j]], used[b[j]]);//此时交换的并不是G里面的值,而是将匹配变了,则达到x与y匹配且x==y
j++;//统计要交换的次数
}
}
printf("%d\n", j);
for(int i = ; i < j ; i++)
printf("C %d %d\n", a[i], b[i]);//应为swap里是列交换所以这里输出“C”(used[i]表示i与used[i]匹配)
}
return ;
}

hdu 2819 Swap的更多相关文章

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

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

  2. HDU 2819 Swap(行列式性质+最大匹配)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2819 题目大意:给你一个n*n的01矩阵,问是否可以通过任意交换整行或者整列使得正对角线上都是1. ...

  3. HDU 2819 Swap(二分图匹配)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=2819 [题目大意] 给出一个棋盘,由白格子和黑格子组成,可以交换棋盘的行列, 使得其主对角线为黑格 ...

  4. HDU 2819 — Swap 二分匹配

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

  5. HDU - 2819 Swap(二分图最大匹配)

    Given an N*N matrix with each entry equal to 0 or 1. You can swap any two rows or any two columns. C ...

  6. HDU 2819 - Swap - [二分图建模+最大匹配]

    题目链接:https://cn.vjudge.net/problem/HDU-2819 Given an N*N matrix with each entry equal to 0 or 1. You ...

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

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

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

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

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

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

随机推荐

  1. HTTP使用BASIC认证的原理及实现方法

    一.   BASIC认证概述 在HTTP协议进行通信的过程中,HTTP协议定义了基本认证过程以允许HTTP服务器对WEB浏览器进行用户身份证的方法,当一个客户端向HTTP服务器进行数据请求时,如果客户 ...

  2. UVa 10048 Audiophobia【Floyd】

    题意:给出一个c个点,s条边组成的无向图,求一点到另一点的路径上最大权值最小的路径,输出这个值 可以将这个 d[i][j]=min(d[i][j],d[i][k]+d[k][j]) 改成 d[i][j ...

  3. POJ 1976 A Mini Locomotive【DP】

    题意:给出一列火车,可以由三个火车头拉,每个火车头最多拉m节车厢(这m节车厢需要保持连续),再给出n节车厢,每节车厢的人数,问最多能够载多少人到终点. 可以转化为三个长度相等的区间去覆盖n个数,使得这 ...

  4. 涵盖网站基本使用的正则表达式的验证方法.cs

    using System; using System.Text.RegularExpressions; using System.Text; using System.IO; using System ...

  5. (转载)NSTimer

    转自:http://www.cnblogs.com/smileEvday/archive/2012/12/21/NSTimer.html 看到这个标题,你可能会想NSTimer不就是计时器吗,谁不会用 ...

  6. Windows pyqt4 bat自动转换UI文件-->.pyw文件

    /***************************************************************************** * Windows pyqt4 bat自动 ...

  7. 打印Dom对象的所有属性和方法

    <html> <head> <title>Test</title> <meta http-equiv="Content-Type&quo ...

  8. 选择符优先级-----:link伪类

    问题:请指出以下结构中,A标签内的字体颜色. <style> a{ color:#ccc}/* 灰色 */ .alink a{color:#F60}/* 橙色 */ h1 a{color: ...

  9. Search in Rotated Sorted Array II

    Question: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? W ...

  10. 剑指offer—第三章高质量的代码(按顺序打印从1到n位十进制数)

    题目:输入一个数字n,按照顺序打印出1到最大n位十进制数,比如输入3,则打印出1,2,3直到最大的3位数999为止. 本题陷阱:没有考虑到大数的问题. 本题解题思路:将要打印的数字,看成字符串,不足位 ...