题意:是否能使对角线上全是1 ,这个简单直接按行列匹配。难在路径的输出,我们知道X,Y左右匹配完了之后,不一定是1–1,2–2,3–3……这种匹配。可能是1–3,2–1,3–2,我们要把他们交换成前一种的匹配形式,也就是路径的答案,再有矩阵的一些关于秩的性质。行变换和列变换是等价的。


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<vector>
#include<cstdlib>
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define cl(a,b) memset(a,b,sizeof(a));
#define LL long long
#define P pair<int,int>
#define X first
#define Y second
#define pb push_back
#define fread(zcc) freopen(zcc,"r",stdin)
#define fwrite(zcc) freopen(zcc,"w",stdout)
using namespace std;
const int maxn=105;
const int inf=999999; vector<int> G[maxn];
int matching[maxn];
bool vis[maxn];
int Nx;
int dfs(int u){
int N=G[u].size();
for(int i=0;i<N;i++){
int v=G[u][i];
if(vis[v])continue;
vis[v]=true;
if(matching[v]==-1||dfs(matching[v])){
matching[v]=u;
return 1;
}
}
return 0;
}
int hungar(){
int ans=0;
cl(matching,-1);
for(int i=0;i<Nx;i++){
cl(vis,false);
ans+=dfs(i);
}
return ans;
}
int Left[maxn],Right[maxn];
int main(){ int n;
while(~scanf("%d",&n)){
for(int i=0;i<maxn;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)G[i].pb(j);
}
}
Nx=n;
int ans=hungar();
if(ans<n){
puts("-1");
continue;
} int num=0;
for(int i=0;i<n;i++){//查找路径
int j;
for(j=i;j<n;j++)if(matching[j]==i)break;
if(i!=j){
Left[num]=i;Right[num++]=j;
swap(matching[i],matching[j]);
}
}
printf("%d\n",num);
for(int i=0;i<num;i++){
printf("C %d %d\n",Left[i]+1,Right[i]+1);
}
}
return 0;
}

HDU 2819 Swap (行列匹配+输出解)的更多相关文章

  1. HDU 2819 Swap (二分匹配+破输出)

    题意:给定上一个01矩阵,让你变成一个对角全是 1 的矩阵. 析:二分匹配,把行和列看成两个集合,用匈牙利算法就可以解决,主要是在输出解,在比赛时一紧张不知道怎么输出了. 输出应该是要把 match[ ...

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

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

  3. HDU 2819 — Swap 二分匹配

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

  4. HDU 2819 ——Swap——————【最大匹配、利用linker数组、邻接表方式】

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

  5. HDU 2819 Swap(二分图匹配)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=2819 [题目大意] 给出一个棋盘,由白格子和黑格子组成,可以交换棋盘的行列, 使得其主对角线为黑格 ...

  6. E - Swap - hdu 2819(简单二分图匹配)

    题意:如果可以交换行列,问主对角线能不能全为1 分析:要想主对角线全为1很明显要有N个行列不想同的点就行了,可以用二分图匹配计算出来多能有几个.如果小与N就不能.输出要是对的就行,不必和答案一样 ** ...

  7. HDU - 2819 Swap(二分匹配)

    题意:交换任意两行或两列,使主对角线全为1. 分析: 1.主对角线都为1,可知最终,第一行与第一列匹配,第二行与第二列匹配,……. 2.根据初始给定的矩阵,若Aij = 1,则说明第i行与第j列匹配, ...

  8. hdu 2819 Swap

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

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

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

随机推荐

  1. 1.tornado实现高并发爬虫

    from pyquery import PyQuery as pq from tornado import ioloop, gen, httpclient, queues from urllib.pa ...

  2. Asp.Net Core 项目实战参考

    Asp.Net Core 项目实战链接 http://www.cnblogs.com/fonour/p/5904530.html

  3. Android 用AsyncTask下载网络图片并显示百分比

    1.添加布局文件:activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/a ...

  4. 架设Hmailserver+webmail邮件服务器

    架设Hmailserver+webmail邮件服务器   在安裝Hmailserver前先安裝Apache.php.mysql,如果你想懶點直接到http://www.phpnow.org下載phpn ...

  5. tomcat并发优化

    配置参考 <Connector port="9027" protocol="HTTP/1.1" maxHttpHeaderSize="8192& ...

  6. SnowNLP:一个处理中文文本的 Python 类库[转]

    SnowNLP是一个python写的类库,可以方便的处理中文文本内容,是受到了TextBlob的启发而写的,由于现在大部分的自然语言处理库基本都是针对英文的,于是写了一个方便处理中文的类库,并且和Te ...

  7. (11)python 模块和包

    一.导入模块和包 模块相当于一个.py文件,包相当于带有个__init__.py一个文件夹,既可按模块导入也可按包导入. 1.导入模块或包 import 包名或模块名 (as 别名),包名或模块名 ( ...

  8. Linux环境安装python3

    linux 安装Python3 1.python下载 请在终端输入如下命令: cd /home wget http://cdn.npm.taobao.org/dist/python/3.6.5/Pyt ...

  9. Codeforces 863F - Almost Permutation

    863F - Almost Permutation 题意 给出每个位置可以放的数字的范围,定义 \(cost = \sum_{i=1}^{n}(cnt(i))^2\) ,其中 \(cnt(i)\) 为 ...

  10. 二分+Dfs【p1902】刺杀大使

    题目描述--->p1902 刺杀大使 题意概括: 找一条路径,使得从第1行到第n行路径的最大值最小. 分析: 题目概括出来,很容易想到二分. 求最大值最小,因此我们可以对最大伤害值进行二分. 如 ...