Swap

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2152    Accepted Submission(s): 764
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
 
 题意:如果可以交换行列,问主对角线能不能全为1

分析:要想主对角线全为1很明显要有N个行列不想同的点就行了,可以用二分图匹配计算出来多能有几个。如果小与N就不能。输出要是对的就行,不必和答案一样
 
 #include<iostream>
#include<cstdio>
#include<cstring> using namespace std; #define N 1100 int n, vis[N], used[N], a[N], b[N];
int maps[N][N]; int found(int u)
{
for(int i = ; i <= n; i++)
{
if(!vis[i] && maps[u][i])
{
vis[i] = ;
if(!used[i] || found(used[i]))
{
used[i] = u;
return true;
}
}
}
return false;
}
int main()
{
int w; while(scanf("%d", &n) != EOF)
{
memset(vis, , sizeof(vis));
memset(used, , sizeof(used));
memset(maps, , sizeof(used));
memset(a, , sizeof(a));
memset(b, , sizeof(b)); int ans = ; for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
scanf("%d", &maps[i][j]); for(int i = ; i <= n; i++)
{
memset(vis, , sizeof(vis));
if(found(i))
ans++;
}
if(ans < n)
{
printf("-1\n");
continue;
} w = ;
for(int i = ; i <= n; i++)
{
while(used[i] != i)
{
a[w] = i;
b[w] = used[i];
swap(used[a[w]], used[b[w]]); // 如果该行匹配不是自身,就交换匹配。
w++;
}
}
printf("%d\n", w);
for(int i = ; i < w; i++)
printf("C %d %d\n", a[i], b[i]); }
return ;
}

Swap——hdu 2819的更多相关文章

  1. Swap HDU - 2819 (有关矩阵的二分匹配)

    题意见大佬:https://www.cnblogs.com/gj-Acit/archive/2013/08/17/3265502.html 题目大意很明确,交换图的某些行或者是某些列(可以都换),使得 ...

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

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

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

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

  4. hdu 2819 Swap

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

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

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

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

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

  7. HDU 2819 — Swap 二分匹配

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

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

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

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

随机推荐

  1. c#处理bin文件

    1. fs.Position  写入的位置,从哪个位置开始写 fs.Write(byte1,0,byte1.Length); byte1写入的byte[], 写入内容从第几位开始取,length取多长 ...

  2. YOLOv3 算法的详细说明

    YOLOv3没有太多的创新,主要是借鉴一些好的方案融合到YOLO里面.不过效果还是不错的,在保持速度优势的前提下,提升了预测精度,尤其是加强了对小物体的识别能力. 本文主要讲v3的改进,由于是以v1和 ...

  3. 通过挂载系统U盘搭建本地yum仓库

    首先打开hbza(CentOS)和yum,两者要连接上 第1步:在hbza中创建一个目录 输入mkdir /lxk,名字随便起.输入mount  /dev/cdrom  /lxk 第2步:打开yum, ...

  4. python 安装成linux中的systemd守护运行

    参考文档1:https://blog.csdn.net/luckytanggu/article/details/53467687 参考文档2:https://www.jianshu.com/p/e14 ...

  5. AtCoder Beginner Contest 133-C - Remainder Minimization 2019

    https://atcoder.jp/contests/abc133/tasks/abc133_c 思路:由于L,R区间太大,所以不能暴力枚举.由于求(i*j)%2019的最小值,那么2019的倍数对 ...

  6. JVM — 类加载机制

    1. 引言 java 类被虚拟机编译之后成为一个 Class 的字节码文件,该字节码文件中包含各种描述信息,最终都需要加载到虚拟机中之后才能运行和使用.那么虚拟机是如何加载这些 Class 文件?Cl ...

  7. Webpack4、iView、Vue开发环境的搭建

    导读 项目使用了 yarn ,一个快速.可靠.安全的依赖管理工具.yarn 是一个类似于npm的包管理工具,它是由 facebook 推出并开源,它在速度,离线模式,版本控制的方面具有独到的优势.此项 ...

  8. asp.net Swiper 轮播动画

    原文:https://blog.csdn.net/qq_39656138/article/details/90451289 官网:https://www.swiper.com.cn/api/index ...

  9. 机器学习-K-means聚类及算法实现(基于R语言)

    K-means聚类 将n个观测点,按一定标准(数据点的相似度),划归到k个聚类(用户划分.产品类别划分等)中. 重要概念:质心 K-means聚类要求的变量是数值变量,方便计算距离. 算法实现 R语言 ...

  10. 认知redis

    一.redis是什么? 1.基于key-value的内存No sql 数据库(非关系型数据库) 2.读写性能非常好 二.redisd的数据类型有哪些?特点分别是什么? 1)string 一个键对一个值 ...