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. Qt Qwdget 汽车仪表知识点拆解1 速度表示

    先贴上效果图,注意,没有写逻辑,所以这些都是乱动的 这里线主要说一下中间显示速度的显示制作的方式,在这里,自己专门写了一个数字的仪表 考虑的一般的汽车是没有办法把瞬时速度提升到四位数的,所以我这里就放 ...

  2. Appium iOS万能的定位方式--Predicate(iOSNsPredicate)

    所谓Predicate定位即Java-Client -5.0.版本以及Appium-Python-Client 0.31版本更新后增加的新的定位方式: 举个例子: JAVA代码: //输入账号和密码 ...

  3. 数论初步——Eratosthenes筛法

    具体内容见紫书p312-p313 一.用Eratosthenes筛法构造1~n的素数表 思想:对于不超过n的每个非负整数p,删除2p,3p,4p…,当处理完所有的数后,还没有被删除的就是素数. 代码: ...

  4. Daily Scrum02 12.01

    今天是2013年12月的第一天,希望大家都有一个新的开始,一起努力!     Member Today's Task Tomorrow's Task 李孟 Task 856: 熟悉单元测试方法熟悉单元 ...

  5. lintcode-123-单词搜索

    123-单词搜索 给出一个二维的字母板和一个单词,寻找字母板网格中是否存在这个单词. 单词可以由按顺序的相邻单元的字母组成,其中相邻单元指的是水平或者垂直方向相邻.每个单元中的字母最多只能使用一次. ...

  6. 【EasyNetQ】- 简介

    EasyNetQ是一个简单易用的,稳定的的RabbitMQ .NET API . 如果您只想尽快启动并运行,请转到“ 快速开始”指南. EasyNetQ的目标是提供一个库,使得在.NET中使用Rabb ...

  7. 【ZJ选讲·字符串折叠】

    给一个字符串(len<=100) 把这个字符串折叠(就是压缩) 记 X(子串) 表示重复 X次该子串 比如 3(orz)  orzorzorz  来点神奇例子: AAAAAAAAAA ...

  8. 【BZOJ 3195 】[Jxoi2012]奇怪的道路 装压dp

    受惯性思维的影响自动把二进制状态认为是连与不连......... 我们这里二进制状态表示的是奇偶,这样的话我们f[i][j][k]表示的就是前i个城市用了j个边他前k个城市的奇偶状态,然后想想怎么转移 ...

  9. iOS 屏幕适配,autoResizing autoLayout和sizeClass图文详解

    === 1. autoResizing autoresizing是苹果早期的ui布局适配的解决办法,iOS6之前完全可以胜任了,因为苹果手机只有3.5寸的屏幕,在加上手机app很少支持横屏,所以iOS ...

  10. HTML5 canvas流体力学效果

    某人用Java搞了一个流体力学的演示:http://grantkot.com/MPM/Liquid.html. 下面是 HTML 5版的流体力学演示(推荐使用Chrome浏览器浏览): 效果演示 &l ...