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

  题意:给定一个n*n的01矩阵,要求通过若干次行交换或列交换来满足主对角线上的数字均为1。

  首先能确定的一点,只用行交换或只用列交换就能满足。一开始看到跟覆盖有关,还在想会不会是舞蹈链什么的,后来发现并不需要。可以把对角线上某一点为1理解为行与列的匹配,这样行和列就正好构成了一个二分图。如果这个二分图的最大匹配为n的话就存在方案,此时,根据匈牙利算法的匹配数组就能找到解法。

#include <stdio.h>
#include <string.h> int a[], b[];
class Hungary {
#define Hungary_MAX_Node 105
#define Hungary_MAX_Edge 10005
public:
struct EDGE {
int v;
int next;
} edge[Hungary_MAX_Edge];
int head[Hungary_MAX_Node];
int Left[Hungary_MAX_Node];
bool vis[Hungary_MAX_Node];
int N, M;
Hungary() {
clear();
}
void clear() {
N = M = ;
memset(Left, , sizeof(Left));
memset(head, -, sizeof(head));
}
void addEdge(int a, int b) {
edge[M].v = b;
edge[M].next = head[a];
head[a] = M++;
}
bool dfs(int u) {
for (int e = head[u]; e != -; e = edge[e].next) {
int v = edge[e].v;
if (!vis[v]) {
vis[v] = true;
if (!Left[v] || dfs(Left[v])) {
Left[v] = u;
return true;
}
}
}
return false;
}
int Max_Match() {
int ret = ;
for (int i = ; i <= N; i++) {
memset(vis, , sizeof(vis));
if (dfs(i)) {
ret++;
}
}
return ret;
}
};
Hungary hungary;
int main() {
int N;
while (~scanf("%d", &N)) {
hungary.clear();
hungary.N = N;
int x, ans;
for (int i = ; i <= N; i++)
for (int j = ; j <= N; j++) {
scanf("%d", &x);
if (x) {
hungary.addEdge(i, j);
}
}
ans = hungary.Max_Match();
if (ans < N) {
printf("-1\n");
continue;
}
int tot = , j;
for (int i = ; i <= N; i++) {
for (j = ; j <= N && hungary.Left[j] != i; j++);
if (i != j) {
a[tot] = i;
b[tot] = j;
tot++;
int t = hungary.Left[i];
hungary.Left[i] = hungary.Left[j];
hungary.Left[j] = t;
}
}
printf("%d\n", tot);
for (int i = ; i < tot; i++) {
printf("C %d %d\n", a[i], b[i]);
}
}
return ;
}

Swap[HDU2819]的更多相关文章

  1. HDU2819 Swap —— 二分图最大匹配

    题目链接:https://vjudge.net/problem/HDU-2819 Swap Time Limit: 2000/1000 MS (Java/Others)    Memory Limit ...

  2. hdu-2819.swap(二分匹配 + 矩阵的秩基本定理)

    Swap Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. Hdu2819 Swap

    Swap Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  4. HDU2819:Swap(二分图匹配)

    Swap Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. hdu2819 Swap 最大匹配(难题)

    题目大意: 给定一个元素的值只有1或者0的矩阵,每次可以交换两行(列),问有没有方案使得对角线上的值都是1.题目没有限制需要交换多少次,也没限制行交换或者列交换,也没限制是主对角线还是副对角线.虽然没 ...

  6. hdu1281+hdu2819(最大匹配数)

    分析:将行和列缩点,即行对应二分图的X部,列对应二分图的Y部,然后交点为连接该行和该列的一条边.匹配时每点都会把整行整列占了,因此就不会出现冲突了. 传送门:hdu1281 棋盘游戏 #include ...

  7. HDU 2819 - Swap - [二分图建模+最大匹配]

    题目链接:https://cn.vjudge.net/problem/HDU-2819 Given an N*N matrix with each entry equal to 0 or 1. You ...

  8. 故障重现(内存篇2),JAVA内存不足导致频繁回收和swap引起的性能问题

    背景起因: 记起以前的另一次也是关于内存的调优分享下   有个系统平时运行非常稳定运行(没经历过大并发考验),然而在一次活动后,人数并发一上来后,系统开始卡. 我按经验开始调优,在每个关键步骤的加入如 ...

  9. LVM 管理减少swap分区空间增加到根分区

    简介 LVM是 Logical Volume Manager(逻辑卷管理)的简写,它是Linux环境下对磁盘分区进行管理的一种机制,它由Heinz Mauelshagen在Linux 2.4内核上实现 ...

随机推荐

  1. CSRF 攻击的应对之道

    转载自imb文库 CSRF(Cross Site Request Forgery, 跨站域请求伪造)是一种网络的攻击方式,该攻击可以在受害者毫不知情的情况下以受害者名义伪造请求发送给受攻击站点,从而在 ...

  2. CI框架多目录设置

    1,设置目的,前台与后台实现独立目录管理 2.通过http://www.myci.com  访问前台,通过http://www.myci.com/admin 访问后台,   多目录的意思是指在同一个网 ...

  3. java数据类型学习

    java数据类型基本分为两类: 一类为基本数据类型: 数值类型: 整数类型:byte.short.int.long 浮点类型:float.double 字符类型:char 布尔类型:boolean 一 ...

  4. phpcms(4) V9 栏目管理

    phpcms V9框架系统后台管理之栏目管理,请参见下文的源码分析(添加栏目和修改栏目): 参照添加栏目的界面图示,便于对源代码的理解: <?php   // 文件路径:phpcms/modul ...

  5. 《vi中的替换艺术》-linux命令五分钟系列之十一

    vi方面的内容不知道分类到哪里好,就放到<Linux命令五分钟系列>里吧! 今天编程,关于栈的一个小例子,其间我需要把”S.”替换为”S->”(替换不包括双引号). 其实这个不难,不 ...

  6. 自定义Excel导出简易组件

    1.组件原理 excel的数据存储是以xml格式存储的,所以导出Excel文件可以通过生成XML来实现.当然XML必须符合一定的格式要求. 2.组件实现 (1)新建类库文件“MyExcel” (2)添 ...

  7. 微博输入相关js 代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. Python自动化运维之28、Django(二)

    一.FORM 1.概述 django表单系统中,所有的表单类都作为django.forms.Form的子类创建,包括ModelForm 关于django的表单系统,主要分两种 基于django.for ...

  9. arp欺骗

    arp欺骗原理 某机器A要向主机B发送报文,会查询本地的ARP缓存表,找到B的IP地址对应的MAC地址后,就会进行数据传输.如果未找到,则广播A一个ARP请求报文(携带主机A的IP地址Ia——物理地址 ...

  10. Ruby自学笔记(六)— 循环

    循环结构在编程语言中是不可或缺的,所以Ruby中的循环也有其自定义的规则. 而我们关注循环结构,要知道两个因素:1) 循环的条件:2) 循环执行的内容 Ruby有一些方式来实现循环结构体: 1. ti ...