Swap
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3800 Accepted Submission(s): 1401
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
0 1
1 0
2
1 0
1 0
Sample Output
1
R 1 2
-1
R 1 2
-1
Source
按行交换。对行进行1-n编号,对列也进行编号。
若第i行的第j1、j2……列有1,则节点i与j1、j2……连边。
二分图跑最大匹配,为完美匹配方可。
matching数组保存每个点的匹配,模拟一下交换输出答案。
//2017-08-26
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ;
const int M = ;
int head[N], tot;
struct Edge{
int to, next;
}edge[M]; void init(){
tot = ;
memset(head, -, sizeof(head));
} void add_edge(int u, int v){
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++; edge[tot].to = u;
edge[tot].next = head[v];
head[v] = tot++;
} int n;
int matching[N];
int check[N];
bool dfs(int u){
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].to;
if(!check[v]){//要求不在交替路
check[v] = ;//放入交替路
if(matching[v] == - || dfs(matching[v])){
//如果是未匹配点,说明交替路为增广路,则交换路径,并返回成功
matching[u] = v;
matching[v] = u;
return true;
}
}
}
return false;//不存在增广路
} //hungarian: 二分图最大匹配匈牙利算法
//input: null
//output: ans 最大匹配数
int hungarian(){
int ans = ;
memset(matching, -, sizeof(matching));
for(int u = ; u <= n; u++){
if(matching[u] == -){
memset(check, , sizeof(check));
if(dfs(u))
ans++;
}
}
return ans;
} const int MAXID = ;
int a[N], b[N], cnt; int main()
{
std::ios::sync_with_stdio(false);
//freopen("inputE.txt", "r", stdin);
while(cin>>n){
init();
int v;
for(int i = ; i <= n; i++){
for(int j = ; j <= n; j++){
cin>>v;
if(v){
add_edge(i, j+MAXID);
}
}
}
int match = hungarian();
if(match != n)cout<<-<<endl;
else{
for(int i = ; i <= n; i++)
matching[i]-=;
cnt = ;
for(int i = ; i <= n; i++){
for(int j = ; j <= n; j++){
if(i == j)continue;
if(matching[j] == i){
swap(matching[i], matching[j]);
a[cnt] = i;
b[cnt++] = j;
}
}
}
cout<<cnt<<endl;
for(int i = ; i < cnt; i++)
cout<<"R "<<a[i]<<" "<<b[i]<<endl;
}
} return ;
}