HDU 2819 — Swap 二分匹配

Swap

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2174    Accepted Submission(s): 774
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

2009 Multi-University Training Contest 1 - Host by TJU

题意:给定一个n*n的01矩阵;通过行或列的变换使得主对角线上都为1;

题解:

 1.第i行放到第j行可以使得第j行的主对角线为1;

2.第j列放到第i列可以使得第j列的主对角线为1;

那么将行作为X集合,列作为Y集合,如果map[i][j]==1,那么Xi->Yj连边,求最大匹配,这样的话没有任何一个行被两个列匹配,

倘若最大匹配为n,即满足题意;

///1085422276
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <typeinfo>
#include <map>
typedef long long ll;
using namespace std;
#define inf 10000000
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<‘0‘||ch>‘9‘)
    {
        if(ch==‘-‘)f=-1;
        ch=getchar();
    }
    while(ch>=‘0‘&&ch<=‘9‘)
    {
        x=x*10+ch-‘0‘;
        ch=getchar();
    }
    return x*f;
}
//***************************************************************
int lk[101],vis[101],n;
char mp[101][101];
bool dfs(int x){
   for(int i=1;i<=n;i++){
        if(mp[x][i]==‘1‘&&!vis[i])
        {
            vis[i]=1;
            if(lk[i]==0||dfs(lk[i]))
            {
                lk[i]=x;
                return 1;
            }
        }
   }
   return 0;

}
int main()
{

    while(scanf("%d",&n)!=EOF)
    {int x;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                scanf("%d",&x);
                mp[i][j]=x+‘0‘;
            }
        }
        memset(lk,0,sizeof(lk));
        int ans=0;
        for(int i=1;i<=n;i++){
            memset(vis,0,sizeof(vis));
            if(dfs(i))ans++;
        }
        int flag;
        if(ans<n)printf("-1\n");
        else {
                cout<<n<<endl;
            //for(int i=1;i<=n;i++) cout<<i<<" "<<lk[i]<<endl;
           // for(int i=1;i<=n;i++)flk[lk[i]]=i;
            for(int i=1;i<=n;i++)
            {
               for(int j=i;j<=n;j++)
               {
                   if(lk[j]==i){
                    flag=j;break;
                   }
               }
               lk[flag]=lk[i];
               cout<<"C "<<i<<" "<<flag<<endl;
            }
        }
    }
    return 0;
}

代码

时间: 2024-11-10 13:25:02

HDU 2819 — Swap 二分匹配的相关文章

HDU 2819 Swap (二分匹配+破输出)

题意:给定上一个01矩阵,让你变成一个对角全是 1 的矩阵. 析:二分匹配,把行和列看成两个集合,用匈牙利算法就可以解决,主要是在输出解,在比赛时一紧张不知道怎么输出了. 输出应该是要把 match[i] = i 这样的输出,然后再改掉后面那个,真是个大傻逼输出,气死了..... 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <strin

hdu 2819 Swap(二分图匹配)

hdu 2819 Swap 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 li

Hdu 1045 二分匹配

题目链接 Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6282    Accepted Submission(s): 3551 Problem Description Suppose that we have a square city with straight streets. A map of a city i

hdu 3081 【二分匹配+并查集+删边||最大路+并查集+二分枚举】

Marriage Match II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2307    Accepted Submission(s): 792 Problem Description Presumably, you all have known the question of stable marriage match. A

HDU - 2819 Swap(二分匹配)

题意:交换任意两行或两列,使主对角线全为1. 分析: 1.主对角线都为1,可知最终,第一行与第一列匹配,第二行与第二列匹配,……. 2.根据初始给定的矩阵,若Aij = 1,则说明第i行与第j列匹配,据此求最大匹配数cnt,若cnt==N,才可通过交换使主对角线都为1. 3.交换时,可只交换行或只交换列.如:只交换列,行不变(顺序为1,2,3,……,n),那么对于列,只需要根据选择排序,将每行一开始匹配的列的顺序最终也变成1,2,3,……,n即可,因为是选择排序,每次选择第i小的交换到第i个位置

hdu 2819 Swap【完美二分匹配】

Swap Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2514    Accepted Submission(s): 900 Special Judge Problem Description Given an N*N matrix with each entry equal to 0 or 1. You can swap any

E - Swap - hdu 2819(简单二分图匹配)

题意:如果可以交换行列,问主对角线能不能全为1 分析:要想主对角线全为1很明显要有N个行列不想同的点就行了,可以用二分图匹配计算出来多能有几个.如果小与N就不能.输出要是对的就行,不必和答案一样 ************************************************************************ #include<stdio.h>#include<algorithm>#include<string.h>using namesp

Hdu 1281 棋盘游戏 (二分匹配)

题目链接: Hdu 1281 棋盘游戏 题目描述: 题目汉语,只说一点,国际象棋中车的攻击范围就像n皇后问题中的皇后一样,同行和同列的车都会相互攻击. 解题思路: 求出来的最大匹配数目==最多能放几个车.计算有几个格子是重要点的时候只需要算出最大的匹配数目,然后枚举每个点不能放置棋子时候的最大匹配数目.最大匹配数目小于原匹配数目的时候,当前枚举点就是重要点. 1 #include <cstdio> 2 #include <cstring> 3 #include <iostre

hdu 2119 Matrix(二分匹配)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2119 Matrix Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2205    Accepted Submission(s): 975 Problem Description Give you a matrix(only contains