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. delphi备份恢复剪切板

    http://blog.csdn.net/youthon/article/details/7327776 delphi备份恢复剪切板         分类:            Delphi编程20 ...

  2. cookie/http/https

    今天再学习顺便外加复习下http的相关知识,顺便试试在笔记中导出一个长篇的图片回事怎么样的效果. HTTP相关知识,不是很全仅供参考

  3. Iview 启动报错 TypeError [ERR_INVALID_CALLBACK]: Callback must be a function

    解决 fs.write(fd, buf, 0, buf.length, 0, function(err, written, buffer) {}); 替换为 fs.write(fd, buf, 0, ...

  4. Vue路由注意事项

    一.vue中路由的使用 1.定义组件 <template> <div class="hello"> <h1 @click="info&quo ...

  5. mooc-IDEA 列操作--005

    十一.IntelliJ IDEA -列操作 实例:根据HTTP请求JSON文件,生成一个枚举类 Step1:创建一个枚举类,把要转换的JSON串粘贴进来. 最终要实现效果 Step2:选中第一个100 ...

  6. 027 (H5*) react01

    目录 正文 1:创建Vue项目 1: 全局安装 vue-cli cnpm install --global vue-cli 2: 创建一个基于 webpack 模板的新项目 vue init webp ...

  7. nested exception is java.lang.NoClassDefFoundError: org/hibernate/validator/resourceloading/ResourceBundleLocator

    原来的hibernate-validator-5.3.0.Final.jar里没有这个接口,换成 hibernate-validator-4.1.0.Final.jar 就好了

  8. Java数据结构之队列(Queue)

    1.使用场景 银行排队的案例: 2.队列介绍 队列是一个有序列表,可以用数组或是链表来实现. 遵循先入先出的原则: 先存入队列的数据,要先取出. 后存入的要后取出 示意图:(使用数组模拟队列示意图) ...

  9. es6基本介绍及使用

    1.什么是es6 ECMAScript 6.0(以下简称ES6)是JavaScript语言的下一代标准,已经在2015年6月正式发布了.它的目标,是使得JavaScript语言可以用来编写复杂的大型应 ...

  10. 通过利用immutability的能力编写更安全和更整洁的代码

    通过利用immutability的能力编写更安全和更整洁的代码 原文:Write safer and cleaner code by leveraging the power of "Imm ...