HDU 2819 ——Swap——————【最大匹配、利用linker数组、邻接表方式】
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
Input
Output
If it is impossible to make all the diagonal entries equal to 1, output only one one containing “-1”.
Sample Input
Sample Output
#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数组、邻接表方式】的更多相关文章
- 三种邻接表存图模板: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< ...
- 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 1874 畅通工程(spfa 邻接矩阵 邻接表)
题目链接 畅通工程,可以用dijkstra算法实现. 听说spfa很好用,来水一发 邻接矩阵实现: #include <stdio.h> #include <algorithm> ...
- hdu 4707 Pet(DFS && 邻接表)
Pet Time Limit: 4000/2000 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. ...
- 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 http://acm.hdu.edu.cn/showproblem.php?pid=2819 Special Judge Problem Description Given an N*N m ...
- HDU - 2819 Swap (二分图匹配-匈牙利算法)
题意:一个N*N的01矩阵,行与行.列与列之间可以互换.要求变换出一个对角线元素全为1的矩阵,给出互换的行号或列号. 分析:首先一个矩阵若能构成对角线元素全为1,那么矩阵的秩为N,秩小于N的情况无解. ...
- HDU 1281——棋盘游戏——————【最大匹配、枚举删点、邻接表方式】
棋盘游戏 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
随机推荐
- js 值传递,引用传递
参考:http://www.cnblogs.com/lcngu/p/5876273.html JS的基本类型,是按值传递的. 对象类型按共享传递的(call by sharing,也叫按对象传递.按对 ...
- Linux curl 常用命令
命令:curl在Linux中curl是一个利用URL规则在命令行下工作的文件传输工具,可以说是一款很强大的http命令行工具.它支持文件的上传和下载,是综合传输工具,但按传统,习惯称url为下载工具. ...
- c#操作word类,进行html和word文档的互相转换
实例引用:http://www.7es.cn/Software_development/171.shtml using Microsoft.Office.Core;using Word = Micro ...
- vmware vmware esxi5.x安装方法及解决错误
vmware Esxi5.x安装指南 vmware Esxi5文档中心官网地址: http://pubs.vmware.com/vsphere-55/index.jsp?topic=%2Fcom.vm ...
- Django之后台管理一
所有的网站都有一个管理后台来对所有的网站数据进行管理.那么Django的后台管理是如何进行的.在网页中输入http://127.0.0.1:8001/admin.得到如下的登录界面 在这里可以看到管理 ...
- hdu4622(后缀自动机模板)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意: 先输入一个长度为 n 的字符串, 然后有 q 个形如 l, r 的询问, 对于每个询问 ...
- CF671A Recycling Bottles 计算几何
It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can ...
- PAT天梯赛 L1-050 倒数第N个字符串
题目链接:点击打开链接 给定一个完全由小写英文字母组成的字符串等差递增序列,该序列中的每个字符串的长度固定为 L,从 L 个 a 开始,以 1 为步长递增.例如当 L 为 3 时,序列为 { aaa, ...
- liunx一次安装多个软件包
https://blog.csdn.net/finded/article/details/44955953 编写shell脚本程序 一次安装多个软件,主要用于一些软件依赖环境配置. 1.shell脚本 ...
- 2-32 while
do while