//
// main.cpp
// HDU 2819 二分匹配
//
// Created by 郑喆君 on 8/12/14.
// Copyright (c) 2014 itcast. All rights reserved.
//
#include<cstdio>
#include<cstring>
#include<iostream>
#include<iomanip>
#include<queue>
#include<cmath>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
const int int_max = 0x07777777;
const int int_min = 0x80000000;
const int maxn = 110;
int n,mymap[maxn][maxn],vis[maxn],lx[maxn];
int ret;
bool find (int x){
for(int i = 0; i < n; i++){
if(!vis[i] && mymap[x][i]){
vis[i] = 1;
if(lx[i]==-1 || find(lx[i])){
lx[i] = x;
return true;
}
}
}
return false;
}
void solve (){
ret = 0;
memset(lx, -1, sizeof(lx));
for(int i = 0; i < n; i++){
memset(vis, 0, sizeof(vis));
if(find(i)) ret++;
}
}
int main(int argc, const char * argv[])
{
while (scanf("%d", &n)!=EOF) {
memset(mymap, 0, sizeof(mymap));
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
int u;
scanf("%d", &u);
if(u) mymap[i][j] = 1;
}
}
solve();
if(ret==n){
vector<int> ret1,ret2;
int pos[maxn];
for(int i = 0; i < n; i++) pos[i] = i;
for(int i = 0; i < n; i++){
if(pos[i]==lx[i]) continue;
int j;
for(j = i+1; j < n; j++){
if(lx[i]==pos[j]) break;
}
ret1.push_back(i);
ret2.push_back(j);
pos[j] = pos[i];
}
printf("%d\n", (int)ret1.size());
for(int i = 0; i < ret1.size(); i++){
printf("R %d %d\n", ret1[i]+1, ret2[i]+1);
}
}else{
cout << -1 << endl;
}
}
}
HDU 2819 二分匹配
最新推荐文章于 2019-09-01 20:03:50 发布