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. unity3d c# http 请求json数据解析

    using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Net ...

  2. linux文件链接

    我的github,欢迎关注,分享知识与技术 链接:一种在共享文件和访问它的用户的若干目录项之间建立联系的一种方法. Linux中包括两种链接:硬链接(HardLink)和软链接(Soft Link), ...

  3. utf-8-BOM删除bom

    utf-8  bom,去除bom //开始 function file_bom($wenjian,$remove = true) { //读取文件,将文件写入字符串    $contents = fi ...

  4. poj2417(Baby-Step Giant-Step)

    题目链接:http://poj.org/problem?id=2417 题意:求满足给出 P, N, B, 求满足条件 BL == N (mod P) 的最小 L, 若不存在则输出 no soluti ...

  5. ipad中icon与launchimage的size

    图标(AppIcon)与启动图(LaunchImage)是开发iOS应用程序必不可少的内容,但是在网络上对于这部分的内容讲解的并不详细,所以花些时间写了这篇文章,希望有需要的朋友可以随时查看 想知道A ...

  6. luogu1891 疯狂lcm ??欧拉反演?

    link 给定正整数N,求LCM(1,N)+LCM(2,N)+...+LCM(N,N). 多组询问,1≤T≤300000,1≤N≤1000000 \(\sum_{i=1}^nlcm(i,n)\) \( ...

  7. 轻量级编辑器透彻指南--Notepad++

    Notepad++是Windows环境下的一款编辑器.比起VSCode等现代编辑器,Notepad++同样具备很多功能.Notepad++一个特点就是轻巧,方便在Windows环境中使用,且编辑功能强 ...

  8. 07. 如何实现移动端rem适配

    如何实现移动端rem适配 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...

  9. Educational Codeforces Round 7 B

    Description You are given the current time in 24-hour format hh:mm. Find and print the time after a  ...

  10. sqlserver 事务日志

    预写式日志(Write-Ahead Logging (WAL)) --在数据写入到数据库之前,先写入到日志. 1.”Begin Tran”记录  -> 缓冲区 2. 日志             ...