Swap

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1800    Accepted Submission(s): 606
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的矩阵,然后进行行列变换,使得矩形的对角线上都是1
做法:二分图匹配匈牙利
**/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
#define maxn 110
int g[maxn][maxn];
int linker[maxn];
bool vis[maxn];
vector<int>G[maxn];
int n;
int dfs(int u)
{
for(int i=;i<(int)G[u].size();i++)
{
int v = G[u][i];
if(!vis[v])
{
vis[v] = true;
if(linker[v] == - || dfs(linker[v]))
{
linker[v] = u;
return ;
}
}
}
return ;
}
int hungary()
{
int res =;
memset(linker,-,sizeof(linker));
for(int i=;i<n;i++)
{
memset(vis,false,sizeof(vis));
res += dfs(i);
}
return res;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
while(~scanf("%d",&n))
{
for (int i = ; i <= n; i++) G[i].clear();
for (int i = ; i < n; i++)
{
for (int j = ; j < n; j++)
{
scanf("%d", &g[i][j]);
if (g[i][j]) G[i].push_back(j);
}
}
int res = hungary();
if(res == n)
{
printf("%d\n",res);
for(int i=;i<n;i++)
{
printf("R %d %d\n",linker[i]+,i+);
for(int j=;j<n;j++)
{
if(linker[j] == i ) linker[j] = linker[i];
}
}
}
else printf("-1\n");
}
return ;
}

HDU-2819的更多相关文章

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

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

  2. HDU 2819 隐式二分图匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=2819 这道题乍一看是矩阵变换题,估计用矩阵之类的也可以做 但是分析一下就可以知道 要凑成对角线都是1,题目允许行 ...

  3. hdu 2819 Swap

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

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

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

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

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

  6. HDU 2819 — Swap 二分匹配

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

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

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

  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. Swap HDU - 2819 (有关矩阵的二分匹配)

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

  10. 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. 项目管理---git----快速使用git笔记(四)------远程项目代码的首次获取

    使用git最常见的场景是 你需要参与到一个项目中,而这个项目的代码,同事已经上传到github或者https://coding.net了. 这时候他会给你一个项目代码的远程仓库链接. 例如: http ...

  2. bzoj4552: [Tjoi2016&Heoi2016]排序(二分+线段树)

    又是久违的1A哇... 好喵喵的题!二分a[p],把大于mid的数改为1,小于等于mid的数改为0,变成01串后就可以用线段树进行那一连串排序了,排序后如果p的位置上的数为0,说明答案比mid小,如果 ...

  3. 【Codeforces 506E】Mr.Kitayuta’s Gift&&【BZOJ 4214】黄昏下的礼物 dp转有限状态自动机+矩阵乘法优化

    神题……胡乱讲述一下思维过程……首先,读懂题.然后,转化问题为构造一个长度为|T|+n的字符串,使其内含有T这个子序列.之后,想到一个简单的dp.由于是回文串,我们就增量构造半个回文串,设f(i,j, ...

  4. 用ByteArrayOutputStream解决IO流乱码问题

    IO中用ByteArrayOutputStream解决乱码问题 --另一种解决乱码的方法 IO中另外一种防止乱码的方法:使用ByteArrayOutputStream在创建ByteArrayOutpu ...

  5. A great tutorial with Jupyter notebook for ML beginners

    An end to end implementation of a Machine Learning pipeline SPANDAN MADAN Visual Computing Group, Ha ...

  6. Leetcode 381. O(1) 时间插入、删除和获取随机元素 - 允许重复

    1.题目描述 设计一个支持在平均 时间复杂度 O(1) 下, 执行以下操作的数据结构. 注意: 允许出现重复元素. insert(val):向集合中插入元素 val. remove(val):当 va ...

  7. ACE服务端编程1:使用VS2010编译ACE6.0及从ACE5.6升级的注意事项

    ACE是一个跨平台的用于并发通信的C++框架,项目开始时使用的是ACE 5.6发布版,目前最新的ACE版本是6.3.0. 网上一直有一种黑ACE的氛围,主要黑点在于ACE的复杂和作者的背景,结合实际应 ...

  8. java集合之深入分析ArrayList

    ArrayList特点: ArrayList方法实现: 扩容方法的实现: 源码: private void ensureCapacityInternal(int minCapacity) { //如果 ...

  9. 痛苦之旅——安装Eric4

    因为想做桌面程序,所以在学PyQt4, 顺便装了下Eric4,这Eric4装起来可不简单,活活花了一个星期..... 网上有很多装Eric4的教程,详细我就不说了,大概步骤是: 1.安装SIP (需要 ...

  10. 51Nod 1083 矩阵取数问题 | 动态规划

    #include "bits/stdc++.h" using namespace std; #define LL long long #define INF 0x3f3f3f3f3 ...