HDU 2819 Swap (二分匹配+破输出)
题意:给定上一个01矩阵,让你变成一个对角全是 1 的矩阵。
析:二分匹配,把行和列看成两个集合,用匈牙利算法就可以解决,主要是在输出解,在比赛时一紧张不知道怎么输出了。
输出应该是要把 match[i] = i 这样的输出,然后再改掉后面那个,真是个大傻逼输出,气死了。。。。。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 200 + 10;
const int mod = 10000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} int match[maxn];
bool vis[maxn];
vector<int> G[maxn]; void add(int u, int v){
G[u].push_back(v);
G[v].push_back(u);
} bool dfs(int u){
vis[u] = true;
for(int i = 0; i < G[u].size(); ++i){
int v = G[u][i];
int w = match[v];
if(w == -1 || (!vis[w] && dfs(w))){
match[u] = v;
match[v] = u;
return true;
}
}
return false;
} int main(){
while(scanf("%d", &n) == 1){
for(int i = 0; i < n * 2; ++i) G[i].clear();
for(int i = 0; i < n; ++i)
for(int j = 0; j < n; ++j){
int x;
scanf("%d", &x);
if(x == 1) add(i, j+n);
} memset(match, -1, sizeof match);
int ans = 0;
n <<= 1;
for(int i = 0; i < n; ++i) if(match[i] == -1){
memset(vis, 0, sizeof vis);
if(dfs(i)) ++ans;
}
if(ans * 2 != n){ printf("-1\n"); continue; }
int t = n / 2;
for(int i = 0; i < t; ++i) match[i] -= t;
printf("%d\n", t);
for(int i = 0; i < t; ++i){
printf("C %d %d\n", i+1, match[i]+1);
for(int j = i+1; j < t; ++j)
if(match[j] == i){ match[j] = match[i]; break; }
}
}
return 0;
}
HDU 2819 Swap (二分匹配+破输出)的更多相关文章
- HDU 2819 — Swap 二分匹配
Swap Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 2819 Swap (行列匹配+输出解)
题意:是否能使对角线上全是1 ,这个简单直接按行列匹配.难在路径的输出,我们知道X,Y左右匹配完了之后,不一定是1–1,2–2,3–3--这种匹配.可能是1–3,2–1,3–2,我们要把他们交换成前一 ...
- HDU - 2819 Swap (二分图匹配-匈牙利算法)
题意:一个N*N的01矩阵,行与行.列与列之间可以互换.要求变换出一个对角线元素全为1的矩阵,给出互换的行号或列号. 分析:首先一个矩阵若能构成对角线元素全为1,那么矩阵的秩为N,秩小于N的情况无解. ...
- 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——————【最大匹配、利用linker数组、邻接表方式】
Swap Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- hdu 1281棋盘游戏(二分匹配)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1281 Problem Description 小希和Gardon在玩一个游戏:对一个N*M的棋盘, ...
- hdu-2819.swap(二分匹配 + 矩阵的秩基本定理)
Swap Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 3468 BFS+二分匹配
九野的博客,转载请注明出处 http://blog.csdn.net/acmmmm/article/details/10966383 开始建图打搓了,参考了大牛的题解打的版本比较清爽,后来改的基本雷同 ...
- HDU - 2819 Swap(二分匹配)
题意:交换任意两行或两列,使主对角线全为1. 分析: 1.主对角线都为1,可知最终,第一行与第一列匹配,第二行与第二列匹配,……. 2.根据初始给定的矩阵,若Aij = 1,则说明第i行与第j列匹配, ...
随机推荐
- vue-cli 脚手架项目-package.json
使用vue-cli脚手架新建的项目中,含有package.json. package.json是npm的配置文件,里面设定了脚本以及项目依赖的库. npm run dev 这样的命令就写在packag ...
- CodeForces - 682E: Alyona and Triangles(旋转卡壳求最大三角形)
You are given n points with integer coordinates on the plane. Points are given in a way such that th ...
- Python 列表的切片和连接
一.定义一个list >>> a = [1, 3, 4, 5, 'a', 's'] >>> a [1, 3, 4, 5, 'a', 's'] 二.获取列表中前3个元 ...
- 如果有多个集合的迭代处理情况【使用MAP】
在SQL开发过程中,动态构建In集合条件查询是比较常见的用法,在Mybatis中提供了foreach功能,该功能比较强大,它允许你指定一个集合,声明集合项和索引变量,它们可以用在元素体内.它也允许你指 ...
- 差分IO标准
差分标准 和单端IO不同的是,差分电平使用两根信号线来传达信号,这两根信号线在传输过程中如果遇到同样的噪声源(共模噪声)干扰,在接收端,这样的共模噪声会在两个信号相减时消除,这样并不会给接收电平造成影 ...
- boot asio 非阻塞同步编程---非阻塞的accept和receive。
boot asio 非阻塞同步编程---非阻塞的accept和receive. 客户端编程: #include<boost/timer.hpp> #include <iostream ...
- 机器学习:模型泛化(LASSO 回归)
一.基础理解 LASSO 回归(Least Absolute Shrinkage and Selection Operator Regression)是模型正则化的一定方式: 功能:与岭回归一样,解决 ...
- Spark Streaming之一:整体介绍
提到Spark Streaming,我们不得不说一下BDAS(Berkeley Data Analytics Stack),这个伯克利大学提出的关于数据分析的软件栈.从它的视角来看,目前的大数据处理可 ...
- Beautiful Soup 4.2.0
Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式 快速开始 pip install beaut ...
- node install webpack -cli webpack4.xxxx
webpack 4.xx 版本 分离了 webpack-cli ; 安装webpack4.xx 需要再安装webpack-cli;