Swap

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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的矩阵,里面只有0、1,问你需要几步可以让主对角线上全是1。如果不能实现,输出-1.
 
解题思路:如果想一想,画一画,可以发现,如果有行或者列全是0或1的话,那么结果肯定是-1。否则一定有解。按照常规的行列分部,在有1的行列进行连边。我们跑一遍最大匹配。如果最大匹配小于n,说明无解。否则利用匹配数组linker。这里linker[i]表示第i列跟第linker[i]行交点有数字1。那么我们枚举第i列。如果linker[i] != i,说明这个位置需要调换,然后暴力枚举linker[j] == i的j,交换linker[i],linker[j],同时记录路径即可。
 
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn = 1100;
struct Edge{
int from, to, dist, next;
Edge(){}
Edge(int _from,int _to,int _next):from(_from),to(_to),next(_next){}
}edges[maxn*maxn*3]; //direction
struct Swap{
int x,y;
}swaps[maxn*maxn];
int tot , head[maxn];
int linker[3*maxn], used[3*maxn], c[maxn];
void init(){
tot = 0;
memset(head,-1,sizeof(head));
}
void AddEdge(int _u,int _v){
edges[tot] = Edge(_u,_v,head[_u]);
head[_u] = tot++;
}
bool dfs(int u,int _n){
for(int e = head[u]; e != -1; e = edges[e].next){
int v = edges[e].to;
if(!used[v]){
used[v] = u;
if(linker[v] == -1 || dfs(linker[v],_n)){
linker[v] = u;
return true;
}
}
}
return false;
}
int hungary(int p, int n){
int ret = 0;
memset(linker,-1,sizeof(linker));
for(int i = 1; i <= p; i++){
memset(used,0,sizeof(used));
if(dfs(i,n))
ret++;
}
return ret;
}
int main(){
int n, m, T, p, k, cas = 0;
while(scanf("%d",&n)!=EOF){
int a,b;
init();
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
scanf("%d",&a);
if(a == 1){
AddEdge(i,j);
}
}
}
int ans = hungary(n,m);
if(ans < n ){
puts("-1"); continue;
}
int num = 0;
for(int i = 1; i <= n; i++){
if(linker[i] != i){
for(int j = i+1; j <= n; j++){
if(linker[j] == i){
swap(linker[i],linker[j]);
num++;
swaps[num].x = i; swaps[num].y = j;
}
}
}
}
printf("%d\n",num);
for(int i = 1; i <= num; i++){
printf("C %d %d\n",swaps[i].x,swaps[i].y);
}
}
return 0;
}

  

 
 
 
 
 
 

HDU 2819 ——Swap——————【最大匹配、利用linker数组、邻接表方式】的更多相关文章

  1. 三种邻接表存图模板:vector邻接表、数组邻接表、链式前向星

    vector邻接表: ; struct Edge{ int u,v,w; Edge(int _u=0,int _v=0,int _w=0){u=_u,v=_v,w=_w;} }; vector< ...

  2. HDU - 2819 Swap(二分图最大匹配)

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

  3. hdu 1874 畅通工程(spfa 邻接矩阵 邻接表)

    题目链接 畅通工程,可以用dijkstra算法实现. 听说spfa很好用,来水一发 邻接矩阵实现: #include <stdio.h> #include <algorithm> ...

  4. hdu 4707 Pet(DFS &amp;&amp; 邻接表)

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

  5. HDU 2819 Swap(行列式性质+最大匹配)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2819 题目大意:给你一个n*n的01矩阵,问是否可以通过任意交换整行或者整列使得正对角线上都是1. ...

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

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

  7. hdu 2819 Swap

    Swap http://acm.hdu.edu.cn/showproblem.php?pid=2819 Special Judge Problem Description Given an N*N m ...

  8. HDU - 2819 Swap (二分图匹配-匈牙利算法)

    题意:一个N*N的01矩阵,行与行.列与列之间可以互换.要求变换出一个对角线元素全为1的矩阵,给出互换的行号或列号. 分析:首先一个矩阵若能构成对角线元素全为1,那么矩阵的秩为N,秩小于N的情况无解. ...

  9. HDU 1281——棋盘游戏——————【最大匹配、枚举删点、邻接表方式】

     棋盘游戏 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status ...

随机推荐

  1. C++: STL迭代器及迭代器失效问题

    转载至:http://blog.csdn.net/wangshihui512/article/details/9791517 迭代器失效: 典型的迭代器失效. 首先对于vector而言,添加和删除操作 ...

  2. Quartz.Net分布式运用

    Quartz.Net的集群部署详解 标签(空格分隔): Quartz.Net Job 最近工作上要用Job,公司的job有些不满足个人的使用,于是就想自己搞一个Job站练练手,网上看了一下,发现Qua ...

  3. onmouseover和onmouseout在GridView中应用 Ver2

    第一个版本,可以参考:http://www.cnblogs.com/insus/archive/2009/03/13/1411057.html 以前的版本,是在Gridview的OnRowCreate ...

  4. ubuntu - 14.04,安装Eclipse(开源开发工具)

    一,安装JDK:Eclipse必须有JDK才能运行,所以首先我们确定系统是否已经安装了JDK,我们在shell里面输入:“java -version”,如果已经安装了,就会打印出来当前JDK版本信息, ...

  5. 「BZOJ 1831」「AHOI 2008」逆序对「贪心」

    题意 给定一个长度为\(n\),值域为\([1,k]\),某些位置不确定的数组,求最小的逆序对.\(n\leq 10^4, k \leq 100\) 题解 这题有人用前缀和优化\(dp\)过了,但是这 ...

  6. [MOOC程序设计与算法二] 递归二

    1.表达式计算 输入为四则运算表达式,仅由整数.+.-.* ./ .(.) 组成,没有空格,要求求其值.假设运算符结果都是整数 ."/"结果也是整数 表达式也是递归的定义: 表达式 ...

  7. 树形DP【洛谷P3047】 [USACO12FEB]附近的牛Nearby Cows

    P3047 [USACO12FEB]附近的牛Nearby Cows 农民约翰已经注意到他的奶牛经常在附近的田野之间移动.考虑到这一点,他想在每一块土地上种上足够的草,不仅是为了最初在这片土地上的奶牛, ...

  8. c#随笔-正则

  9. Python Web开发中的WSGI协议简介

    在Python Web开发中,我们一般使用Flask.Django等web框架来开发应用程序,生产环境中将应用部署到Apache.Nginx等web服务器时,还需要uWSGI或者Gunicorn.一个 ...

  10. 对DeepLung数据预处理部分的详细展示

    之前有解释预处理部分的函数,不过觉得还不够详细,同时文字解释还不够直观,所以现在想一步步运行下,打印输出 首先读取原始数据,包括相应的注释(即结节标签)[注意]注释文件中的标签是按x,y,z的顺序给的 ...