题目链接:https://vjudge.net/problem/HDU-2819

Swap

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4003    Accepted Submission(s): 1478
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

题解:

题意:给出一个大小为n*n的01矩阵,问能否通过交换行或列,使得主对角线上的数全为1?

1.用匈牙利算法求出最大匹配数cnt,如果cnt等于n,则可以实现。

2.可知,我们可以通过只交换行或者只交换列,就能使得主对角线上的数全为1。

3.枚举每一行i,对于第i行,找到与第i列匹配的那一行k,然后交换第i行和第k行。这样就满足了第i行有1在对角线上。

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <algorithm>
using namespace std;
const int INF = 2e9;
const int MOD = 1e9+;
const int MAXN = +; int n;
int M[MAXN][MAXN], id[MAXN][MAXN], xlink[MAXN], ylink[MAXN];
bool vis[MAXN]; bool dfs(int u)
{
for(int i = ; i<=n; i++)
if(M[u][i] && !vis[i])
{
vis[i] = true;
if(ylink[i]==- || dfs(ylink[i]))
{
ylink[i] = u;
xlink[u] = i;
return true;
}
}
return false;
} int hungary()
{
int ret = ;
memset(xlink, -, sizeof(xlink));
memset(ylink, -, sizeof(ylink));
for(int i = ; i<=n; i++)
{
memset(vis, , sizeof(vis));
if(dfs(i)) ret++;
}
return ret;
} int main()
{
while(scanf("%d", &n)!=EOF)
{
for(int i = ; i<=n; i++)
for(int j = ; j<=n; j++)
scanf("%d", &M[i][j]); int cnt = hungary();
if(cnt<n)
{
printf("-1\n");
continue;
} int ans = , op[MAXN][];
for(int i = ; i<n; i++)
for(int j = i+; j<=n; j++)
{
if(xlink[j]==i)
{
swap(xlink[j], xlink[i]);
op[++ans][] = i; op[ans][] = j;
break;
}
} printf("%d\n", ans);
for(int i = ; i<=ans; i++)
printf("R %d %d\n", op[i][], op[i][]);
}
}

HDU2819 Swap —— 二分图最大匹配的更多相关文章

  1. HDU2819(KB10-E 二分图最大匹配)

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

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

  3. bzoj 1854: [Scoi2010]游戏 (并查集||二分图最大匹配)

    链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1854 写法1: 二分图最大匹配 思路:  将武器的属性对武器编号建边,因为只有10000种 ...

  4. hdu-2819.swap(二分匹配 + 矩阵的秩基本定理)

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

  5. POJ 2226二分图最大匹配

    匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名.匈牙利算法是基于Hall定理中充分性证明的思想,它是二部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图 ...

  6. POJ2239 Selecting Courses(二分图最大匹配)

    题目链接 N节课,每节课在一个星期中的某一节,求最多能选几节课 好吧,想了半天没想出来,最后看了题解是二分图最大匹配,好弱 建图: 每节课 与 时间有一条边 #include <iostream ...

  7. poj 2239 二分图最大匹配,基础题

    1.poj 2239   Selecting Courses   二分图最大匹配问题 2.总结:看到一个题解,直接用三维数组做的,很巧妙,很暴力.. 题意:N种课,给出时间,每种课在星期几的第几节课上 ...

  8. UESTC 919 SOUND OF DESTINY --二分图最大匹配+匈牙利算法

    二分图最大匹配的匈牙利算法模板题. 由题目易知,需求二分图的最大匹配数,采取匈牙利算法,并采用邻接表来存储边,用邻接矩阵会超时,因为邻接表复杂度O(nm),而邻接矩阵最坏情况下复杂度可达O(n^3). ...

  9. 二分图最大匹配的K&#246;nig定理及其证明

     二分图最大匹配的K?nig定理及其证明 本文将是这一系列里最短的一篇,因为我只打算把K?nig定理证了,其它的废话一概没有.    以下五个问题我可能会在以后的文章里说,如果你现在很想知道的话,网上 ...

随机推荐

  1. PS学习笔记(02)

    书籍推荐: <设计之下>这本APP设计书字里行间都透露出了真实,作者能将其工作流程和方法分享出来,实在值得尊敬.通过这本书全面了解了真实的设计工作是怎么做的,今后可以用到自己的工作中.赞! ...

  2. Python+selenium(Autolt实现上传)

    AutoIt是一个使用类似BASIC脚本语言的免费软件,被设计用来进行Windows GUI的自动化测试.它利用模拟键盘按键,鼠标移动和窗口/控件的组合来实现自动化任务. 此次小编介绍的是利用Auto ...

  3. python--如何在线上环境优雅的修改配置文件?

    1.如何在线上环境优雅的修改配置文件? 原配置文件 #原配置文件 global log 127.0.0.1 local2 daemon maxconn 256 log 127.0.0.1 local2 ...

  4. Light OJ-1082 - Array Queries,线段树区间查询最大值,哈哈,水过~~

                                                                                                        ...

  5. [luoguP2948] [USACO09OPEN]滑雪课Ski Lessons(DP)

    传送门 f[i][j]表示i时刻能力值为j的最大滑雪数 显然f[0][1]=0,开始搜索 三种转移: ①美美的喝上一杯**:f[i+1][j]=max(f[i+1][j],f[i][j]) ②滑雪,f ...

  6. Codeforces 892 A.Greed

    A. Greed time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  7. mysql获取行号的方法

    1.不排序 语句: ) ) ) b,bigquestion 结果:  2.排序的 语句 ) ) ) b,bigquestion order by bigquestion.bigQuestionSequ ...

  8. 某考试 T1 table

    我们把每一行看成一个多项式 f[i][1] + f[i][2] * x + f[i][3] * x^2 + ..... + f[i][n] * x^(n-1) 的话,一行转移到下一行就相当于乘上一个{ ...

  9. 查看MySQL系统变量的命令

    用了好长时间mysql,却没有用心记住一些有用的东西,加油! mysql> SHOW VARIABLES; +---------------------------------+-------- ...

  10. javascript闭包诡异的问题

    var funcs = []; for (var i = 0; i < 3; i++) { // let's create 3 functions funcs[i] = function() { ...