HDU [P2819] swap
二分图行列匹配+输出路径
经典题,当且仅当一行匹配一列的时候,符合题意。
本题的难点在于如何输出路径,我们发现这个移动的过程就是将所有匹配选择排序,在选择排序时输出路径即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int init(){
int rv=0,fh=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-') fh=-1;
c=getchar();
}
while(c>='0'&&c<='9'){
rv=(rv<<1)+(rv<<3)+c-'0';
c=getchar();
}
return fh*rv;
}
int n,ma[105][105],g[105][105],match[105];
bool f[105];
bool hungarian(int u){
for(int i=1;i<=g[u][0];i++){
int v=g[u][i];
if(!f[v]){
f[v]=1;
if(!match[v]||hungarian(match[v])){
match[v]=u;
return 1;
}
}
}
return 0;
}
int main(){
while(~scanf("%d",&n)){
memset(ma,0,sizeof(ma));
memset(match,0,sizeof(match));
memset(g,0,sizeof(g));
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
ma[i][j]=init();
if(ma[i][j]) g[i][++g[i][0]]=j;
}
}
int ans=0;
for(int i=1;i<=n;i++){
memset(f,0,sizeof(f));
if(hungarian(i)) ans++;
}
if(ans!=n) printf("-1\n");
else{
int cnt=0;
int p1[105],p2[105];
memset(p1,0,sizeof(p1));
memset(p2,0,sizeof(p2));
for(int i=1;i<=n;i++){
int k=i;
for(int j=i;j<=n;j++){
if(match[j]<match[k]){
k=j;
}
}
if(k!=i){
cnt++;
p1[cnt]=i;p2[cnt]=k;
swap(match[i],match[k]);
}
}
printf("%d\n",cnt);
for(int i=1;i<=cnt;i++){
printf("C %d %d\n",p1[i],p2[i]);
}
}
}
return 0;
}
HDU [P2819] swap的更多相关文章
- hdu 2819 Swap
Swap http://acm.hdu.edu.cn/showproblem.php?pid=2819 Special Judge Problem Description Given an N*N m ...
- HDU 2819 Swap(行列式性质+最大匹配)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2819 题目大意:给你一个n*n的01矩阵,问是否可以通过任意交换整行或者整列使得正对角线上都是1. ...
- HDU 2819 ——Swap——————【最大匹配、利用linker数组、邻接表方式】
Swap Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- HDU 2819 — Swap 二分匹配
Swap Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 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 ...
- HDU 2819 - Swap - [二分图建模+最大匹配]
题目链接:https://cn.vjudge.net/problem/HDU-2819 Given an N*N matrix with each entry equal to 0 or 1. You ...
- HDU - 2819 Swap (二分图匹配-匈牙利算法)
题意:一个N*N的01矩阵,行与行.列与列之间可以互换.要求变换出一个对角线元素全为1的矩阵,给出互换的行号或列号. 分析:首先一个矩阵若能构成对角线元素全为1,那么矩阵的秩为N,秩小于N的情况无解. ...
- HDU 2819 Swap(二分图匹配)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=2819 [题目大意] 给出一个棋盘,由白格子和黑格子组成,可以交换棋盘的行列, 使得其主对角线为黑格 ...
- HDU 2819 Swap (行列匹配+输出解)
题意:是否能使对角线上全是1 ,这个简单直接按行列匹配.难在路径的输出,我们知道X,Y左右匹配完了之后,不一定是1–1,2–2,3–3--这种匹配.可能是1–3,2–1,3–2,我们要把他们交换成前一 ...
随机推荐
- Shortest path of the king
必须要抄袭一下这个代码 The king is left alone on the chessboard. In spite of this loneliness, he doesn't lose h ...
- See you~(二维树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1892 See you~ Time Limit: 5000/3000 MS (Java/Others) ...
- Centos7安装和卸载Mongodb数据库
MongoDB是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. MongoDB MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非 ...
- android手机安全卫士、Kotlin漫画、支付宝动画、沉浸状态栏等源码
Android精选源码 轻量级底部导航栏 android手机卫士源码 android实现高仿今日头条源码 一个用Kotlin写的简单漫画App源码 android吐槽项目完整源码 ...
- TypeScript笔记 1--环境搭建
TypeScript是什么 Typescript是JavaScript的超集,支持ES6特性并且提供了类型系统,可以编译成Javascript.是微软开发且已经在github上开源. ES6(ES20 ...
- MLlib--PIC算法
转载请标明出处http://www.cnblogs.com/haozhengfei/p/82c3ef86303321055eb10f7e100eb84b.html PIC算法 幂迭代聚类 ...
- Oracle_事务
Oracle_事务 -事物管理 create table account( id number, money number ); --实现转账操作 update ...
- CentOS 6.*通过yum安装 MySQL-5.5
转自https://www.cnblogs.com/wsl222000/p/5445768.html 1.安装mysql-5.5的yum源 rpm -ivh http://repo.mysql.com ...
- PHP headers_sent() 函数
PHP HTTP 函数 定义和用法 headers_sent() 函数检查 HTTP 标头是否已被发送以及在哪里被发送. 如果报头已发送,则返回 true,否则返回 false. 语法 headers ...
- 本地如何使用phpstudy环境搭建多站点
http://jingyan.baidu.com/article/e52e36154227ef40c70c5147.html 平时在开发项目的时候, 多个项目同时开发的时候会遇到都得放到根目录才能正常 ...