HDU--2819 Swap(二分图)

HDU–2819 Swap(二分图)

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

#include<map>
#include<set>
#include<list>
#include<cmath>
#include<queue>
#include<stack>
#include<cstdio>
#include<string>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<fstream>
#include<iomanip>
#include<sstream>
#include<iostream>
#include<algorithm>
#define fcio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define lowbit(x) (x&-x)
#define debug cout<<"What fuck!!"<<endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn=1e3+10;
const int INF=0x3f3f3f3f;
const ll inf=0x3f3f3f3f3f3f3f3f;
int n,m;
int vis[maxn];
int man[maxn]; 
int love[maxn][maxn];
vector<pair<int,int> >g;
bool find(int x){
	for(int j=1;j<=n;++j){
		if(love[j][x]==1&&vis[j]==0){
		/*因为读的时候读的是行与列的
		关系,所以这里遍历数组的第
		一维来找列与行的关系*/
			vis[j]=1;
			if(man[j]==0||find(man[j])){
				man[j]=x;//与男生j匹配的为女生j; 
				return true;
			}
		}
	}
	return false;
}
int main(){
	fcio;
	while(cin>>n){
		memset(man,0,sizeof man);
		memset(love,0,sizeof love);
		int ans=0;
		for(int i=1;i<=n;++i)
			for(int j=1;j<=n;++j)
				cin>>love[i][j];
		for(int i=1;i<=n;++i){
			memset(vis,0,sizeof vis);
			if(find(i))
				ans++;
		}
		//cout<<ans<<endl;
		if(ans!=n){
			cout<<-1<<endl;
			continue;
		}
		g.clear();
		int num=0;
		for(int i=1;i<=n;++i){
			if(man[i]!=i){
				num++;
				for(int j=i+1;j<=n;++j){
					if(man[j]==i){
						g.push_back(make_pair(i,j));
						swap(man[j],man[i]);
						break;
					}
				}
			}
		} 
		cout<<num<<endl;
		for(int i=0;i<num;++i){
			cout<<"R "<<g[i].first<<" "<<g[i].second<<endl;
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值