Swap

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2236    Accepted Submission(s): 801
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
 
很好的一道题,值得深思。
题意:可交换任意两行或任意两列,最终是主对角线上全为1,输出交换过程
如果可行的话,一定可以只交换列或只交换行得到,因为不管怎么交换,原先在同一行的始终在同一行,原先在同一列的始终在同一列。想到这,构图也就出来了,对行和列构图,如果mp[i][j]==1则在i和j之间加边
/*
ID: LinKArftc
PROG: 2819.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const double e = exp(1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ; int mp[maxn][maxn];
int linker[maxn];
bool vis[maxn];
int n; bool dfs(int u) {
for (int v = ; v <= n; v ++) {
if (!vis[v] && mp[u][v]) {
vis[v] = true;
if (linker[v] == - || dfs(linker[v])) {
linker[v] = u;
return true;
}
}
}
return false;
} int hungry() {
memset(linker, -, sizeof(linker));
int ret = ;
for (int i = ; i <= n; i ++) {
memset(vis, , sizeof(vis));
if (dfs(i)) ret ++;
}
return ret;
} int a[maxn], b[maxn]; int main() {
//input;
while (~scanf("%d", &n)) {
int tmp;
memset(mp, , sizeof(mp));
for (int i = ; i <= n; i ++) {
for (int j = ; j <= n; j ++) {
scanf("%d", &tmp);
if (tmp) mp[i][j] = ;
}
}
int ans = hungry();
if (ans < n) printf("-1\n");
else {
int cnt = ;
for (int i = ; i <= n; i ++) {
int j;
for (j = i; j <= n; j ++) {
if (linker[j] == i) break;
}
if (j != i) {
cnt ++;
a[cnt] = i; b[cnt] = j;
swap(linker[i], linker[j]);
}
}
printf("%d\n", cnt);
for (int i = ; i <= cnt; i ++) printf("C %d %d\n", a[i], b[i]);
}
} return ;
}

HDU2819(二分图匹配,记录过程)的更多相关文章

  1. hdu2819二分图匹配

    Given an N*N matrix with each entry equal to 0 or 1. You can swap any two rows or any two columns. C ...

  2. HDU5090--Game with Pearls 二分图匹配 (匈牙利算法)

    题意:给N个容器,每个容器里有一定数目的珍珠,现在Jerry开始在管子上面再放一些珍珠,放上的珍珠数必须是K的倍数,可以不放.最后将容器排序,如果可以做到第i个容器上面有i个珍珠,则Jerry胜出,反 ...

  3. 1027 姓名与ID[二分图匹配(匈牙利)]

    1027 姓名与ID  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解  查看运行结果       题目描述 Description 有N个人,各自有一 ...

  4. (转)二分图匹配匈牙利算法与KM算法

    匈牙利算法转自于: https://blog.csdn.net/dark_scope/article/details/8880547 匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名 ...

  5. 经典网络流题目模板(P3376 + P2756 + P3381 : 最大流 + 二分图匹配 + 最小费用最大流)

    题目来源 P3376 [模板]网络最大流 P2756 飞行员配对方案问题 P3381 [模板]最小费用最大流 最大流 最大流问题是网络流的经典类型之一,用处广泛,个人认为网络流问题最具特点的操作就是建 ...

  6. UVA 12549 - 二分图匹配

    题意:给定一个Y行X列的网格,网格种有重要位置和障碍物.要求用最少的机器人看守所有重要的位置,每个机器人放在一个格子里,面朝上下左右四个方向之一发出激光直到射到障碍物为止,沿途都是看守范围.机器人不会 ...

  7. POJ 1274 裸二分图匹配

    题意:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶,告诉每头奶牛愿意产奶的牛棚编号,求出最多能分配到的牛栏的数量. 分析:直接二分图匹配: #include<stdio.h> #includ ...

  8. UVa 二分图匹配 Examples

    这些都是刘汝佳的算法训练指南上的例题,基本包括了常见的几种二分图匹配的算法. 二分图是这样一个图,顶点分成两个不相交的集合X , Y中,其中同一个集合中没有边,所有的边关联在两个集合中. 给定一个二分 ...

  9. 博弈论(二分图匹配):NOI 2011 兔兔与蛋蛋游戏

    Description Input 输入的第一行包含两个正整数 n.m. 接下来 n行描述初始棋盘.其中第i 行包含 m个字符,每个字符都是大写英文字母"X".大写英文字母&quo ...

  10. POJ2584 T-Shirt Gumbo 二分图匹配(网络流)

    #include <cstdio> #include <cstring> #include <algorithm> const int inf=0x3f3f3f3f ...

随机推荐

  1. 自动化测试--封装JDBCUnit

    在进行测试的时候,经常需要对数据库进行操作.我们知道,通过代码与数据库交互,需要以下几步: 1.加载驱动 之前有盆友问我,为什么Selenium操作浏览器的时候,非要下载浏览器驱动?为啥对数据库进行操 ...

  2. TortoiseSVN的安装使用

    下面分享一篇关于TortoiseSVN的安装以及使用 1.运行TortoiseSVN-1.6.6.17493-win32-svn-1.6.6.msi程序, 开始安装 2.点击Next, 下一步 3.选 ...

  3. cocos2d-x 粒子系统

    粒子系统是模拟自然界中的一些粒子的物理运动的效果,如烟雾,下雪,下雨,火,爆炸等. 粒子发射模式 粒子系统的发射模式的时候有两种方式:重力模式和半径模式. 粒子系统属性  属性名  行为  模式  d ...

  4. laxcus的新功能:支持表跨数据库操作

    关系数据库的层次结构,是账号.数据库.表,一个账号下可以有多个数据库,每个数据库有多个表,但是不同数据库下的表是不能够互相操作的.例如:"select a.*, b.* from Title ...

  5. (原创)最小生成树之Prim(普里姆)算法+代码详解,最懂你的讲解

    Prim算法 (哈欠)在创建最小生成树之前,让我们回忆一下什么是最小生成树.最小生成树即在一个待权值的图(即网结构)中用一个七拐八绕的折线串连起所有的点,最小嘛,顾名思义,要权值相加起来最小,你当然可 ...

  6. Linux编译安装opencv

    参考https://blog.csdn.net/huang826336127/article/details/78760885 一.下载opencv源码包 下载地址:https://opencv.or ...

  7. 测试理论-selenium的工作原理

  8. 平面最近点对(HDU 1007)

    题解:点击 #include <stdio.h> #include <string.h> #include <algorithm> #include <ios ...

  9. lintcode-103-带环链表 II

    带环链表 II 给定一个链表,如果链表中存在环,则返回到链表中环的起始节点的值,如果没有环,返回null. 样例 给出 -21->10->4->5, tail connects to ...

  10. python的三种控制流

    什么是控制流 >>控制代码执行顺序的语句 >>python中有哪些控制流 >>顺序结构 >>> a = 7 >>> print( ...