HDU-2819

Swap

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1800    Accepted Submission(s): 606
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
/**
          题意:给出一个n*n的矩阵,然后进行行列变换,使得矩形的对角线上都是1
          做法:二分图匹配匈牙利
**/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
#define maxn 110
int g[maxn][maxn];
int linker[maxn];
bool vis[maxn];
vector<int>G[maxn];
int n;
int dfs(int u)
{
          for(int i=0;i<(int)G[u].size();i++)
          {
                    int v = G[u][i];
                    if(!vis[v])
                    {
                              vis[v] = true;
                              if(linker[v] == -1 || dfs(linker[v]))
                              {
                                        linker[v] = u;
                                        return 1;
                              }
                    }
          }
          return 0;
}
int hungary()
{
          int res  =0;
          memset(linker,-1,sizeof(linker));
          for(int i=0;i<n;i++)
          {
                    memset(vis,false,sizeof(vis));
                    res += dfs(i);
          }
          return res;
}
int main()
{
#ifndef ONLINE_JUDGE
          freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
          while(~scanf("%d",&n))
          {
                 for (int i = 0; i <= n; i++) G[i].clear();
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
                scanf("%d", &g[i][j]);
                if (g[i][j]) G[i].push_back(j);
            }
        }
                   int res = hungary();
                    if(res == n)
                    {
                              printf("%d
",res);
                              for(int i=0;i<n;i++)
                              {
                                        printf("R %d %d
",linker[i]+1,i+1);
                                        for(int j=0;j<n;j++)
                                        {
                                                  if(linker[j] == i ) linker[j] = linker[i];
                                        }
                              }
                    }
                    else printf("-1
");
          }
          return 0;
}
原文地址:https://www.cnblogs.com/chenyang920/p/4392665.html