Swap

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3371    Accepted Submission(s): 1235
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   |   We have carefully selected several similar problems for you:  2818 2821 2817 2825 2824 
 

——————————————————————————————————

题目的意思是给一个01矩阵,随意交换行和列,问是否能使左上到右下的对角线变为全1

思路:先进行行列二分匹配,如果可以在根据匹配的关系进行列调整(只需调列或调行就可以了)

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <queue>
  8. #include <vector>
  9. #include <set>
  10. #include <stack>
  11. #include <map>
  12. #include <climits>
  13. using namespace std;
  14.  
  15. #define LL long long
  16. const int INF = 0x3f3f3f3f;
  17. const int MAXN=1005;
  18. int uN,vN; //u,v数目
  19. int g[MAXN][MAXN];
  20. int linker[MAXN];
  21. bool used[MAXN];
  22. int link[MAXN];
  23. struct node
  24. {
  25. int u,v;
  26. }ans[100005];
  27. bool dfs(int u)
  28. {
  29. int v;
  30. for(v=1; v<=vN; v++)
  31. if(g[u][v]&&!used[v])
  32. {
  33. used[v]=true;
  34. if(linker[v]==-1||dfs(linker[v]))
  35. {
  36. linker[v]=u;
  37. return true;
  38. }
  39. }
  40. return false;
  41. }
  42.  
  43. int hungary()
  44. {
  45. int res=0;
  46. int u;
  47. memset(linker,-1,sizeof(linker));
  48. for(u=1; u<=uN; u++)
  49. {
  50. memset(used,0,sizeof(used));
  51. if(dfs(u)) res++;
  52. }
  53. return res;
  54. }
  55.  
  56. int main()
  57. {
  58. int m,n,k,x,y,T;
  59. while(~scanf("%d",&n))
  60. {
  61. for(int i=1;i<=n;i++)
  62. for(int j=1;j<=n;j++)
  63. scanf("%d",&g[i][j]);
  64. uN=vN=n;
  65. if(hungary()<n)
  66. printf("-1\n");
  67. else
  68. {
  69. int cnt=0;
  70. for(int i=1;i<=n;i++)
  71. {
  72. while(linker[i]!=i)
  73. {
  74. ans[cnt].u=i,ans[cnt++].v=linker[i];
  75. swap(linker[i],linker[linker[i]]);
  76. }
  77. }
  78. printf("%d\n",cnt);
  79. for(int i=0;i<cnt;i++)
  80. {
  81. printf("C %d %d\n",ans[i].u,ans[i].v);
  82. }
  83.  
  84. }
  85. }
  86. return 0;
  87. }

  

Hdu2819 Swap的更多相关文章

  1. HDU2819 Swap —— 二分图最大匹配

    题目链接:https://vjudge.net/problem/HDU-2819 Swap Time Limit: 2000/1000 MS (Java/Others)    Memory Limit ...

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

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

  3. hdu2819 Swap 最大匹配(难题)

    题目大意: 给定一个元素的值只有1或者0的矩阵,每次可以交换两行(列),问有没有方案使得对角线上的值都是1.题目没有限制需要交换多少次,也没限制行交换或者列交换,也没限制是主对角线还是副对角线.虽然没 ...

  4. hdu1281+hdu2819(最大匹配数)

    分析:将行和列缩点,即行对应二分图的X部,列对应二分图的Y部,然后交点为连接该行和该列的一条边.匹配时每点都会把整行整列占了,因此就不会出现冲突了. 传送门:hdu1281 棋盘游戏 #include ...

  5. Swap[HDU2819]

    SwapTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission ...

  6. HDU2819:Swap(二分图匹配)

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

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

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

  8. 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题

    背景起因: 记起以前的另一次也是关于内存的调优分享下   有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...

  9. LVM 管理减少swap分区空间增加到根分区

    简介 LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现 ...

随机推荐

  1. django项目创建启动 ORM操作

    . HTTP协议消息的格式: . 请求(request) 请求方法 路径 HTTP/1.1\r\n k1:v1\r\n ...\r\n \r\n 请求体 <-- 可以有,可以没有 . 响应(re ...

  2. Android.ApplicationCrash

    1. 如何调试分析Android中发生的tombstone http://www.360doc.com/content/12/1017/10/7580194_241974419.shtml tombs ...

  3. git的命令详解

    # git三个区 + 工作区: 写代码的地方 + 暂存区: 暂时存储代码 + 仓库区: 代码提交到了仓库区,就生成一条历史记录(版本) 工作区===> 暂存区 ===> 仓库区 # git ...

  4. JSR 规范目录

    JSR 规范目录 一.Servlet 规范 1.1 Servlet 2.x 规范 1.2 Servlet 3.x 规范 - 注解和异步请求规范 每天用心记录一点点.内容也许不重要,但习惯很重要!

  5. MD5加密及Hash长度拓展攻击【通俗易懂】

    先放一个简单点的利用了Hash长度拓展攻击的题目 if($COOKIE["getmein"] === md5($secret . urldecode($username . $pa ...

  6. java通过接口扩展枚举

    package com.hra.riskprice; import com.hra.riskprice.SysEnum.Factor_Type; import com.hra.riskprice.po ...

  7. nginx gzip on 无效

    优化页面的时候,使用nginx开启gzip ,发现并没有什么反映~ 在nginx.conf中的配置如下: gzip on; gzip_min_length 1k; gzip_buffers 16k; ...

  8. fastcgi vc6.0demo

    #include <WinSock2.h> #include <stdio.h> #pragma comment(lib, "ws2_32.lib") ty ...

  9. m序列c语言实现

    演示,不是算法 void m4() { int a[4]={1,0,0,1}; int m[15]; int temp; for(int i=0;i<15;i++){ m[i] = a[0]; ...

  10. IOS初级:UIAlertController

    - (IBAction)signOutAction:(id)sender { //初始化,StyleActionSheet是对话框的样式 UIAlertController *alert = [UIA ...