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:

2
0 1
1 0
2
1 0
1 0
Sample Output:
1
R 1 2
-1
题意:
通过交换行和列,使左上角到右下角的对角线都为1。
 
题解:
左上角到右下角的对角线坐标为(i,i),横纵坐标相等,这是一个特性。我们要做的就是通过交换,使1的横纵坐标相等。
考虑最终的情况,对角线上的1横纵坐标都相等,这在二分图中,就相当于1-1,2-2...n-n,相当于两边集合都是平行相连的。
通过这里可以想到二分图的最大匹配就为n,如果小于n,无论怎样,都不能做到一一平行相连。
那么可行性就可以通过二分图最大匹配判定。
输出方案还是挺有意思的,最终的状态是平行相连,那么我就考虑dfs过后的match数组,不水平的连成水平就行了。
 
代码如下:
#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(二分图匹配)的更多相关文章

  1. HDU2819 Swap —— 二分图最大匹配

    题目链接:https://vjudge.net/problem/HDU-2819 Swap Time Limit: 2000/1000 MS (Java/Others)    Memory Limit ...

  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. HDU2819(二分图匹配,记录过程)

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

  5. 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 ...

  6. E - Swap - hdu 2819(简单二分图匹配)

    题意:如果可以交换行列,问主对角线能不能全为1 分析:要想主对角线全为1很明显要有N个行列不想同的点就行了,可以用二分图匹配计算出来多能有几个.如果小与N就不能.输出要是对的就行,不必和答案一样 ** ...

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

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

  8. Bzoj 1562: [NOI2009]变换序列 匈牙利算法,二分图匹配

    题目: http://cojs.tk/cogs/problem/problem.php?pid=409 409. [NOI2009]变换序列 ★★☆   输入文件:transform.in   输出文 ...

  9. 【BZOJ3140】消毒(二分图匹配)

    [BZOJ3140]消毒(二分图匹配) 题面 Description 最近在生物实验室工作的小T遇到了大麻烦. 由于实验室最近升级的缘故,他的分格实验皿是一个长方体,其尺寸为abc,a.b.c 均为正 ...

  10. [NOI2009]变换序列(二分图匹配)

    我们先不考虑字典序最小,先来求出一种可行解. 不难发现,对于每一个i值,它所对应的T值在模n意义下最多两个,于是我们可以用二分图匹配来判断. 那字典序最小呢? 回顾一下二分图匹配的算法:网络流?貌似不 ...

随机推荐

  1. python图片大小处理;

    循环一个目录将下面的所有png或者jpg文件全部缩小一定比例:     from PIL import Image import os,re work_dir = 'C:\\Users\\Admini ...

  2. Python3 小工具-TCP半连接扫描

    from scapy.all import * import optparse import threading def scan(ip,port): pkt=IP(dst=ip)/TCP(dport ...

  3. 十三:Transparent Encryption in HDFS(转)

    透明加密:http://blog.csdn.net/linlinv3/article/details/44963429 hadoop透明加密  kms 简介      Hadoop Key Manag ...

  4. “Hello world!”团队—团队选题展示(视频展示说明)

    本次博客的主要内容基本分为以下两方面: 一.视频截图展示 二.视频简要说明 博客内容展示: 视频截图1: 简要说明:这是组长在视频前期简要介绍我们这款游戏项目的内容.从可行性和需求市场方面进行了简要阐 ...

  5. MyBatis传入参数为list、数组、map写法(转载)

    MyBatis传入参数为list.数组.map写法 1.foreach简单介绍: foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有item ...

  6. Alpha 冲刺(3/10)

    队名 火箭少男100 组长博客 林燊大哥 作业博客 Alpha 冲鸭鸭鸭! 成员冲刺阶段情况 林燊(组长) 过去两天完成了哪些任务 协调各成员之间的工作 协助后端界面的开发 搭建项目运行的服务器环境 ...

  7. Mac OS安装Scrapy失败

    报错: DEPRECATION: Uninstalling a distutils installed project (six) has been deprecated and will be re ...

  8. TCP系列28—窗口管理&流控—2、延迟ACK(Delayed Acknowledgments)

    一.简介 之前的内容中我们多次提到延迟ACK(Delayed Ack),延迟ACK是在RFC1122协议中定义的,协议指出,一个TCP实现应该实现延迟ACK,但是ACK不能被过度延迟,协议给出延迟AC ...

  9. C# 正则表达式 最全的验证类

    ///<summary> ///验证输入的数据是不是正整数 ///</summary> ///<param name="str">传入字符串&l ...

  10. HDU 2068 Choose the best route

    http://acm.hdu.edu.cn/showproblem.php?pid=2680 Problem Description One day , Kiki wants to visit one ...