【HDU2819】Swap

Swap

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1713    Accepted Submission(s): 574
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   |   We have carefully selected several similar problems for you:   2818  2821  2817  2825  2824


这道题注意两点:

1.当最大匹配数为N时。那么一定可以构造出来。如果可以构造出来,那么最大匹配数为N。

2.行和列互换 可以仅仅由列或者行互换得到。

#include <cstring>
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <map>
#include <queue>
using namespace std;

const int maxn = 110;
int N;
int g[maxn][maxn];
int used[maxn];
int mat[maxn];
int l[maxn];
int r[maxn];


bool hunary(int x)
{
    for(int i=0;i<N;i++)
    {
        if(!used[i] && g[x][i])
        {
            if(mat[i] == -1 || hunary(mat[i]))
            {
                mat[i] = x;
                return true;
            }
        }
    }
    return false;
}
int match()
{
    int matches = 0;
    memset(mat,-1,sizeof(mat));
    for(int i=0;i<N;i++)
    {
        memset(used,0,sizeof(used));
        if(hunary(i))
        {
            matches ++;
        }
    }

    return matches;
}
int main()
{
    freopen("1.txt","r",stdin);
    while(scanf("%d",&N) != EOF)
    {
        int u,v;
        for(int i=0;i<N;i++)
        {
            for(int j=0;j<N;j++)
            {
                //scanf("%d%d",&u,&v);
                scanf("%d",&g[i][j]);
            }
        }

        int matches = match();
        //printf("");
      //  cout << matches << endl;
        if(matches < N )
        {
            printf("-1\n");
            continue;
        }
        int i,j;
        int ansnum = 0;
        for( i=0;i<N;i++)
        {
            for( j=0;j<N;j++)
            {
                if(mat[j] == i)
                    break;
            }

            if(i != j)
            {
        l[ansnum] = i;
        r[ansnum++] = j;

        int tmpi = mat[i];
        int tmpj = mat[j];

        mat[i] = tmpj;
        mat[j] = tmpi;
            }

        }

        printf("%d\n",ansnum);
        for(int i=0;i<ansnum;i++)
        {
            printf("C %d %d\n",l[i]+1,r[i]+1);
        }
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值