【二分图匹配+路径输出】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

 

题目大意:通过交换行或者交换列,实现对角线全为1;(输入的图只能是0或1)

思路:想象一下,如果第 i 行第 j 个为1,那么就将line[i][j]标为1,然后用匈牙利算法(匈牙利算法自带记录路径)

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<math.h>
#include<map>
#include<vector>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N=115;

int m,n;
int mark[N],line[N][N],r[N];
int mp[N][N];

int found(int a)
{
    int i;
    for(i=1; i<=n; i++)
    {
        if(line[a][i]&&!mark[i])
        {
            mark[i]=1;
            if(!r[i]|found(r[i]))
            {
                r[i]=a;
                return 1;
            }
        }
    }
    return 0;
}

int main()
{
    while(~scanf("%d",&n))
    {
        int v,i,j,ans=0,s=0;
        memset(line,0,sizeof(line));
        memset(mp,0,sizeof(mp));
        memset(r,0,sizeof(r));
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
            {
                scanf("%d",&v);
                if(v)
                    line[i][j]=1;
            }
        for(i=1; i<=n; i++)
        {
            memset(mark,0,sizeof(mark));
            if(found(i))
                ans++;
        }
        if(ans!=n)//如果最大匹配不等于1,那么肯定是错的,输出-1
            printf("-1\n");//因为这是二分图最大匹配的结果,要让对角线全为1
        else
        {
            int r1[N],r2[N];
            for(i=1; i<=n; i++)
                r1[r[i]]=i;
            memcpy(r2,r1,sizeof(r2));
            for(i=1; i<=n; i++)
            {
                if(r1[i]!=i)
                    for(j=i+1; j<=n; j++)
                        if(r1[j]==i)
                        {
                            swap(r1[i],r1[j]);
                            s++;
                        }
            }
            printf("%d\n",s);
            for(i=1; i<=n; i++)
            {
                if(r2[i]!=i)
                    for(j=i+1; j<=n; j++)
                        if(r2[j]==i)
                        {
                            printf("R %d %d\n",i,j);
                            swap(r2[i],r2[j]);
                        }
            }
        }
    }
    return 0;
}

 

题目描述 给出一个由 $n$ 个数排成的序列,你可以进行如下操作: 交换相邻的两个数。 将该序列中的第 $k$ 个数改为 $x$。 你的任务是将该序列变成一个等差数列。 输入格式 第一行包含两个整数 $n$ 和 $m$,表示序列长度和操作次数。 第二行包含 $n$ 个整数,表示初始序列。 接下来 $m$ 行,每行描述一个操作,格式如下: 1. swap x y:表示交换原序列中下标为 $x$ 和 $y$ 的数。 2. set x y:表示将原序列中下标为 $x$ 的数修改为 $y$。 输出格式 如果无法将序列变成等差数列,输出 Impossible。 否则,第一行输出 Yes。 第二行输出变换后的序列。 数据范围 $1 \leq n \leq 5000$ $1 \leq m \leq 10^5$ $1 \leq |a_i|, |x|, |y| \leq 10^9$ 输入样例1: 5 3 5 4 3 2 1 set 2 6 swap 1 2 set 4 1 输出样例1: Yes 6 4 3 1 2 输入样例2: 5 3 5 4 3 2 1 set 2 6 swap 1 2 set 4 10 输出样例2: Impossible 算法1 (二分图匹配) $O(n^3)$ 由于每次可以交换相邻的两个数,可以通过这个操作来使序列中的某个数字移动到另一个位置,因此可以考虑每个数字在序列中的位置之间建立一条边,如果两个数字之间可以通过交换相邻数变成等差数列,则在它们之间连一条边。 具体而言,如果有两个数字 $a,b$,假设它们当前的位置分别为 $i,j$,那么它们之间就可以连一条边,如果它们能够使得序列变成等差数列。 对于 $set$ 操作,可以考虑将该位置对应的点与其他点之间的边全部删除,然后重新建立连接。 最后,判断是否存在一个匹配,使得所有点都与另一个点进行匹配,如果存在,则说明可以通过交换相邻数的方式使得序列变成等差数列。 时间复杂度 - 建图 $O(n^2)$ - 匈牙利算法 $O(n^3)$ C++ 代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值