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?

InputThere 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.OutputFor 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
题意:给一个只有0 1的矩阵,是否能通过交换两行或者两列使对角线全为1
题解:二分图匹配x和y,输出方法是关键,两遍循环取对应的xy不相同的进行交换记录交换的行或者列(由矩阵知识可知行和列交换一种就行了)
刚开始因为but M should be more than 1000. 这句话我非要作死输出1000个,话说题目能不能不要瞎写啊!!!
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define mod 1000000007 using namespace std; const int N=+,maxn=+,inf=0x3f3f3f3f; int color[N],n;
bool used[N],ok[N][N]; bool match(int x)
{
for(int i=;i<=n;i++)
{
if(ok[x][i]&&!used[i])
{
used[i]=;
if(color[i]==||match(color[i]))
{
color[i]=x;
return ;
}
}
}
return ;
}
int solve()
{
int ans=;
memset(color,,sizeof color);
for(int i=;i<=n;i++)
{
memset(used,,sizeof used);
ans+=match(i);
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie();
while(cin>>n){
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
cin>>ok[i][j];
if(solve()!=n)cout<<-<<endl;
else
{
// for(int i=1;i<=n;i++)cout<<color[i]<<endl;
memset(used,,sizeof used);
queue<pair<int,int> >q;
for(int i=;i<=n;i++)
{
if(i==color[i])continue;
for(int j=i+;j<=n;j++)
{
if(j==color[j])continue;
if(i==color[j])
{
q.push(make_pair(i,j));
swap(color[i],color[j]);
}
}
}
cout<<q.size()<<endl;
while(!q.empty()){
cout<<"C "<<q.front().first<<" "<<q.front().second<<endl;
q.pop();
}
}
}
return ;
}

hdu2819二分图匹配的更多相关文章

  1. UVA 12549 - 二分图匹配

    题意:给定一个Y行X列的网格,网格种有重要位置和障碍物.要求用最少的机器人看守所有重要的位置,每个机器人放在一个格子里,面朝上下左右四个方向之一发出激光直到射到障碍物为止,沿途都是看守范围.机器人不会 ...

  2. POJ 1274 裸二分图匹配

    题意:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶,告诉每头奶牛愿意产奶的牛棚编号,求出最多能分配到的牛栏的数量. 分析:直接二分图匹配: #include<stdio.h> #includ ...

  3. BZOJ1433 ZJOI2009 假期的宿舍 二分图匹配

    1433: [ZJOI2009]假期的宿舍 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2375  Solved: 1005[Submit][Sta ...

  4. HDU1281-棋盘游戏-二分图匹配

    先跑一个二分图匹配,然后一一删去匹配上的边,看能不能达到最大匹配数,不能这条边就是重要边 /*----------------------------------------------------- ...

  5. HDU 1083 网络流之二分图匹配

    http://acm.hdu.edu.cn/showproblem.php?pid=1083 二分图匹配用得很多 这道题只需要简化的二分匹配 #include<iostream> #inc ...

  6. hdu 5727 Necklace dfs+二分图匹配

    Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...

  7. BZOJ 1059 & 二分图匹配

    题意: 判断一个黑白染色的棋盘能否通过交换行或列使对角线上都是黑色. SOL: 真是有点醉...这种问题要么很神要么很水...第一眼感觉很水但就是不造怎么做...想了10分钟怎么感觉就是判断个数够不够 ...

  8. 【POJ 3020】Antenna Placement(二分图匹配)

    相当于用1*2的板覆盖给定的h*w的格子里的点,求最少的板.可以把格子相邻的分成两个集合,如下图,0为一个集合,1的为一个,也就是(行数+列数)为奇数的是一个集合,为偶数的为另一个集合.1010101 ...

  9. BZOJ-1143&&BZOJ-2718 祭祀river&&毕业旅行 最长反链(Floyed传递闭包+二分图匹配)

    蛋蛋安利的双倍经验题 1143: [CTSC2008]祭祀river Time Limit: 10 Sec Memory Limit: 162 MB Submit: 1901 Solved: 951 ...

随机推荐

  1. 分享自己使用CSS的public

    body,ol,ul,h1,h2,h3,h4,h5,h6,p,th,td,dl,dd,form,fieldset,legend,input,textarea,select,td,figure{marg ...

  2. Struts2之标签使用

    上一篇我们一起探讨了Struts2中的OGNL表达式的知识,本篇我们一起来学习一下关于Struts2标签的使用,包括:基础标签:property.set.bean.include:判断标签:if el ...

  3. Halloc内存分配器

    MAX_NSBS 8192:默认的最大superblocks数量 SB_SET_SZ(MAX_NSBS / WORD_SZ):superblock set的大小,每个set32个superblcoks ...

  4. nmap安装过程

    nmap是一个网络扫描和主机检测工具. 功能:1.扫描目标主机开放的端口 2.扫描目标主机特定端口是否关闭 3.路由跟踪(到目标主机所经过的网络节点及其通过时间) 4.扫描一个网段下的所有IP 5.探 ...

  5. 解决mac os下mcss命令报错:env: node\r: No such file or directory

    标题无“转载”即原创文章,版权所有.转载请注明来源:http://besteam.im/blogs/article/31/. 我一直对字符界面有抵触感,即使会用vim,我的linux脚本(python ...

  6. 云片-语音验证码--短信验证码 - PHP接口

    ///////////////////////语音验证///////////////////////////// /**  * 发送短信(语音验证码)云片语音服务  * @param  $apikey ...

  7. bootstrap file input 官方文档翻译

    file Input官方文档 中文翻译 file input 特性 1.这个插件会把简单的html文件变成一个更好用的文件选择输入控件,通过一个html的文件输入框,能兼容那些不支持jquery或js ...

  8. 机器学习:Python中如何使用最小二乘法

    之所以说"使用"而不是"实现",是因为python的相关类库已经帮我们实现了具体算法,而我们只要学会使用就可以了.随着对技术的逐渐掌握及积累,当类库中的算法已经 ...

  9. 【Egret】WebSocket 的使用说明

    在Egret里可以使用WebSocket ,也可以使用socket.io 首先先深入了解一下 WebSocket 在Egret里的机制,看这篇文章: 主要讲解Egret里使用WebSocket和pro ...

  10. 老李案例分享:MAT分析应用程序服务出现内存溢出过程

    老李案例分享:MAT分析应用程序服务出现内存溢出过程   poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.在poptest的loa ...