Swap

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2174    Accepted Submission(s): 774
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
题意:给定一个n*n的01矩阵;通过行或列的变换使得主对角线上都为1;
题解:

 1.第i行放到第j行可以使得第j行的主对角线为1;

2.第j列放到第i列可以使得第j列的主对角线为1;

那么将行作为X集合,列作为Y集合,如果map[i][j]==1,那么Xi->Yj连边,求最大匹配,这样的话没有任何一个行被两个列匹配,

倘若最大匹配为n,即满足题意;

  1. ///
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <ctime>
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <set>
  9. #include <vector>
  10. #include <queue>
  11. #include <typeinfo>
  12. #include <map>
  13. typedef long long ll;
  14. using namespace std;
  15. #define inf 10000000
  16. inline ll read()
  17. {
  18. ll x=,f=;
  19. char ch=getchar();
  20. while(ch<''||ch>'')
  21. {
  22. if(ch=='-')f=-;
  23. ch=getchar();
  24. }
  25. while(ch>=''&&ch<='')
  26. {
  27. x=x*+ch-'';
  28. ch=getchar();
  29. }
  30. return x*f;
  31. }
  32. //***************************************************************
  33. int lk[],vis[],n;
  34. char mp[][];
  35. bool dfs(int x){
  36. for(int i=;i<=n;i++){
  37. if(mp[x][i]==''&&!vis[i])
  38. {
  39. vis[i]=;
  40. if(lk[i]==||dfs(lk[i]))
  41. {
  42. lk[i]=x;
  43. return ;
  44. }
  45. }
  46. }
  47. return ;
  48.  
  49. }
  50. int main()
  51. {
  52.  
  53. while(scanf("%d",&n)!=EOF)
  54. {int x;
  55. for(int i=;i<=n;i++){
  56. for(int j=;j<=n;j++){
  57. scanf("%d",&x);
  58. mp[i][j]=x+'';
  59. }
  60. }
  61. memset(lk,,sizeof(lk));
  62. int ans=;
  63. for(int i=;i<=n;i++){
  64. memset(vis,,sizeof(vis));
  65. if(dfs(i))ans++;
  66. }
  67. int flag;
  68. if(ans<n)printf("-1\n");
  69. else {
  70. cout<<n<<endl;
  71. //for(int i=1;i<=n;i++) cout<<i<<" "<<lk[i]<<endl;
  72. // for(int i=1;i<=n;i++)flk[lk[i]]=i;
  73. for(int i=;i<=n;i++)
  74. {
  75. for(int j=i;j<=n;j++)
  76. {
  77. if(lk[j]==i){
  78. flag=j;break;
  79. }
  80. }
  81. lk[flag]=lk[i];
  82. cout<<"C "<<i<<" "<<flag<<endl;
  83. }
  84. }
  85. }
  86. return ;
  87. }

代码

HDU 2819 — Swap 二分匹配的更多相关文章

  1. HDU 2819 Swap (二分匹配+破输出)

    题意:给定上一个01矩阵,让你变成一个对角全是 1 的矩阵. 析:二分匹配,把行和列看成两个集合,用匈牙利算法就可以解决,主要是在输出解,在比赛时一紧张不知道怎么输出了. 输出应该是要把 match[ ...

  2. HDU - 2819 Swap (二分图匹配-匈牙利算法)

    题意:一个N*N的01矩阵,行与行.列与列之间可以互换.要求变换出一个对角线元素全为1的矩阵,给出互换的行号或列号. 分析:首先一个矩阵若能构成对角线元素全为1,那么矩阵的秩为N,秩小于N的情况无解. ...

  3. HDU 2819 Swap (行列匹配+输出解)

    题意:是否能使对角线上全是1 ,这个简单直接按行列匹配.难在路径的输出,我们知道X,Y左右匹配完了之后,不一定是1–1,2–2,3–3--这种匹配.可能是1–3,2–1,3–2,我们要把他们交换成前一 ...

  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——————【最大匹配、利用linker数组、邻接表方式】

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

  6. hdu 1281棋盘游戏(二分匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281   Problem Description 小希和Gardon在玩一个游戏:对一个N*M的棋盘, ...

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

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

  8. HDU 3468 BFS+二分匹配

    九野的博客,转载请注明出处 http://blog.csdn.net/acmmmm/article/details/10966383 开始建图打搓了,参考了大牛的题解打的版本比较清爽,后来改的基本雷同 ...

  9. HDU - 2819 Swap(二分匹配)

    题意:交换任意两行或两列,使主对角线全为1. 分析: 1.主对角线都为1,可知最终,第一行与第一列匹配,第二行与第二列匹配,……. 2.根据初始给定的矩阵,若Aij = 1,则说明第i行与第j列匹配, ...

随机推荐

  1. Cannot find class [org.apache.commons.dbcp.BasicDataSource]

    错误:Cannot find class [org.apache.commons.dbcp.BasicDataSource] 原因:缺少commons-dbcp.jar

  2. ubuntu显示桌面的快捷键,以及修改方法

    在ubuntu下面,快速显示桌面,你可以这样做. 1,ctrl+alt+d (默认的) 2,alt+tab 可以切换到桌面 但是我想把它修改成和windows一样的,我该怎么做呢? 其实很简单. 系统 ...

  3. mysql查询在一张表不在另外一张表的记录

    mysql查询在一张表不在另外一张表的记录   问题:    查询一个表(tb1)的字段记录不在另一个表(tb2)中      条件:tb1的字段key的值不在tbl2表中      -------- ...

  4. Hadoop Streaming例子(python)

    以前总是用java写一些MapReduce程序现举一个例子使用Python通过Hadoop Streaming来实现Mapreduce. 任务描述: HDFS上有两个目录/a和/b,里面数据均有3列, ...

  5. Don't make me think 摘录与读后感

    别让我思考 ——Krug可用性第一定律 点击多少次都没关系,只要每次点击都是无需思考,明确无误的选择.——Krug可用性第二定律 去掉每个页面上一般的文字, 然后把剩下的文字再去掉一半 ——Krug可 ...

  6. CSS3实现二十多种基本图形

    CSS3可以实现很多漂亮的图形,我收集了32种图形,在下面列出.直接用CSS3画出这些图形,要比贴图性能更好,体验更加,是一种非常好的网页美观方式. 这32种图形分别为圆形,椭圆形,三角形,倒三角形, ...

  7. ris'In App Purchase总结

    原地址:http://www.cocoachina.com/bbs/read.php?tid=38555&page=1 In App Purchase属于iPhone SDK3.0的新特性,用 ...

  8. PHP无限极分类实现

    简单版的PHP生成无限极分类代码.其中包括了数据库设计.以及输出分类HTML代码. SQL代码 CREATE TABLE `district` ( `id` int(10) unsigned NOT ...

  9. BZOJ 2818

    2818:GCD Description 给定整数$N$,求$1\le x,y\le N$且$\gcd{x,y}$为素数的数对$(x,y)$有多少对. Input $N$ Output RT Samp ...

  10. 【VirtualBox】VirtualBox的桥接网络模式,为啥网络不稳定?

    网桥模式访问外网非常慢,经常卡死,ping时断时续 七搞八搞,反复重启了几次 TMD  就好了,也不知道什么情况,VirtualBox还是不太好使啊..... 网桥模式 设置 如下: 参考资料: ht ...