Swap---hdu2819(最大匹配)
题意:通过交换行或者列来实现对角线(左上角到右下角)上都是1,
首先,如果某行全是0或者某列全是0必然不满足情况输出-1,如果能转换的话,那么必然可以通过全由行(列)变换得到;
还有就是对角线上的N个1,它们各自在不同的行中出现至少一次才可以,
比如样例2中,虽然有两个1,但是它们总是处在同一列,仍然不满足要求,
很明显不能一个行对应两个列,或者说,每一行都应该有至少一个与自己对应的列才能满足条件;
那么将行作为X集合,列作为Y集合,如果map[i][j]==1,那么Xi->Yj连边,求最大匹配,这样的话没有任何一个行被两个列匹配,也就满足了我们的要求,
如果最大匹配==N,那么必然有解,否则必然无解;
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<stack>
#include<map>
using namespace std;
#define N 1100
#define INF 0x3f3f3f3f
#define met(a, b) memset(a, b, sizeof(a)) int used[N], G[N][N], vis[N], n; int Find(int u)
{
for(int i=; i<=n; i++)
{
if(!vis[i] && G[u][i])
{
vis[i] = ;
if(!used[i] || Find(used[i]))
{
used[i] = u;
return ;
}
}
}
return ;
} int main()
{
while(scanf("%d", &n) != EOF)
{
met(G, ); for(int i=; i<=n; i++)
{
for(int j=; j<=n; j++)
{
int op;
scanf("%d", &op);
G[i][j] = op;
}
}
int ans = ;
met(used, );
for(int i=; i<=n; i++)
{
met(vis, );
ans += Find(i);
}
if(ans < n)
{
printf("-1\n");
continue;
}
int cnt = , a[N]={}, b[N]={}; ///相当于是给一个数组used排序的过程,使得交换的次数最少;
///每次把i位置上的数,放到应该放的位置,直到当前位置是对应的数为止;
for(int i=; i<=n; i++)
{
while(i != used[i])
{
a[cnt] = i;
b[cnt] = used[i];
swap(used[i], used[used[i]]);
cnt ++;
}
} printf("%d\n", cnt);
for(int i=; i<cnt; i++)
printf("C %d %d\n", a[i], b[i]);
}
return ;
}
/*
3
0 1 1
0 0 1
1 0 0
*/
Swap---hdu2819(最大匹配)的更多相关文章
- HDU2819 Swap —— 二分图最大匹配
题目链接:https://vjudge.net/problem/HDU-2819 Swap Time Limit: 2000/1000 MS (Java/Others) Memory Limit ...
- Swap[HDU2819]
SwapTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission ...
- 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——————【最大匹配、利用linker数组、邻接表方式】
Swap Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- hdu2819 Swap 最大匹配(难题)
题目大意: 给定一个元素的值只有1或者0的矩阵,每次可以交换两行(列),问有没有方案使得对角线上的值都是1.题目没有限制需要交换多少次,也没限制行交换或者列交换,也没限制是主对角线还是副对角线.虽然没 ...
- 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(二分匹配 + 矩阵的秩基本定理)
Swap Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- hdu1281+hdu2819(最大匹配数)
分析:将行和列缩点,即行对应二分图的X部,列对应二分图的Y部,然后交点为连接该行和该列的一条边.匹配时每点都会把整行整列占了,因此就不会出现冲突了. 传送门:hdu1281 棋盘游戏 #include ...
- HDU2819(KB10-E 二分图最大匹配)
Swap Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- HDU 2819 Swap(行列式性质+最大匹配)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2819 题目大意:给你一个n*n的01矩阵,问是否可以通过任意交换整行或者整列使得正对角线上都是1. ...
随机推荐
- razor使用注意点........
使用三元运算符时记得加括号.... 如: @Convert.ToInt32(Request.QueryString["type"])==0?true:false :这是错误的写法 ...
- bootstrap+PHP表单验证
来源:http://www.sucaihuo.com/php/1814.html demo http://www.sucaihuo.com/jquery/18/1814/demo/
- informatica中的workflow连接远程数据库
如果是远程oracle这样写 名称随便起,方便自己记住,后面用户名密码你都知道,再加上数据库的地址:端口/SID就可以了. 如10.33.2.208:1521/torcl
- oracle 中 cursor 与refcursor及sys_refcursor的区别 (转载)
http://blog.csdn.net/gyflyx/article/details/6889028 引用一.显式cursor 显式是相对与隐式cursor而言的,就是有一个明确的声明的cursor ...
- .net多线程,线程异步,线程同步,并发问题---1---ShinePans
申请线程,输出线程状态: using System; using System.Collections.Generic; using System.Linq; using System.Text; u ...
- POJ 1160 Post Office(区间DP)
Description There is a straight highway with villages alongside the highway. The highway is represen ...
- swift - UIToolbar 的用法
代码如下: 1.声明及初始化 var toolsBar = UIToolbar() toolsBar.frame = CGRect(x:, y:, width:SCREEN_WIDTH, height ...
- 设置Android应用程序横竖屏显示
设置Android应用程序横竖屏显示有2中方式: 1.在mainfest中增加android:screenOrientation属性 <?xml version="1.0" ...
- Android权限全记录(转)
常用权限: 读写存储卡装载和卸载文件系统 android.permission.WRITE_EXTERNAL_STORAGE android.permission.READ_EXTERNAL_STOR ...
- isdigit()
isdigit() 是字符串的一个方法,用来判断这个字符串是否是纯数字的字符串 In [1]: str = 'hello' In [2]: str.isdigit() Out[2]: False In ...