HDU-2819
Swap
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1800 Accepted Submission(s): 606
Special Judge
an N*N matrix with each entry equal to 0 or 1. You can swap any two
rows or any two columns. Can you find a way to make all the diagonal
entries equal to 1?
are several test cases in the input. The first line of each test case
is an integer N (1 <= N <= 100). Then N lines follow, each
contains N numbers (0 or 1), separating by space, indicating the N*N
matrix.
each test case, the first line contain the number of swaps M. Then M
lines follow, whose format is “R a b” or “C a b”, indicating swapping
the row a and row b, or swapping the column a and column b. (1 <= a, b
<= N). Any correct answer will be accepted, but M should be more
than 1000.
If it is impossible to make all the diagonal entries equal to 1, output only one one containing “-1”.
/**
题意:给出一个n*n的矩阵,然后进行行列变换,使得矩形的对角线上都是1
做法:二分图匹配匈牙利
**/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
#define maxn 110
int g[maxn][maxn];
int linker[maxn];
bool vis[maxn];
vector<int>G[maxn];
int n;
int dfs(int u)
{
for(int i=;i<(int)G[u].size();i++)
{
int v = G[u][i];
if(!vis[v])
{
vis[v] = true;
if(linker[v] == - || dfs(linker[v]))
{
linker[v] = u;
return ;
}
}
}
return ;
}
int hungary()
{
int res =;
memset(linker,-,sizeof(linker));
for(int i=;i<n;i++)
{
memset(vis,false,sizeof(vis));
res += dfs(i);
}
return res;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
while(~scanf("%d",&n))
{
for (int i = ; i <= n; i++) G[i].clear();
for (int i = ; i < n; i++)
{
for (int j = ; j < n; j++)
{
scanf("%d", &g[i][j]);
if (g[i][j]) G[i].push_back(j);
}
}
int res = hungary();
if(res == n)
{
printf("%d\n",res);
for(int i=;i<n;i++)
{
printf("R %d %d\n",linker[i]+,i+);
for(int j=;j<n;j++)
{
if(linker[j] == i ) linker[j] = linker[i];
}
}
}
else printf("-1\n");
}
return ;
}
HDU-2819的更多相关文章
- HDU 2819 ——Swap——————【最大匹配、利用linker数组、邻接表方式】
Swap Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- HDU 2819 隐式二分图匹配
http://acm.hdu.edu.cn/showproblem.php?pid=2819 这道题乍一看是矩阵变换题,估计用矩阵之类的也可以做 但是分析一下就可以知道 要凑成对角线都是1,题目允许行 ...
- 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(二分图匹配)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=2819 [题目大意] 给出一个棋盘,由白格子和黑格子组成,可以交换棋盘的行列, 使得其主对角线为黑格 ...
- HDU 2819 — Swap 二分匹配
Swap Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- E - Swap - hdu 2819(简单二分图匹配)
题意:如果可以交换行列,问主对角线能不能全为1 分析:要想主对角线全为1很明显要有N个行列不想同的点就行了,可以用二分图匹配计算出来多能有几个.如果小与N就不能.输出要是对的就行,不必和答案一样 ** ...
- 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 ...
- Swap HDU - 2819 (有关矩阵的二分匹配)
题意见大佬:https://www.cnblogs.com/gj-Acit/archive/2013/08/17/3265502.html 题目大意很明确,交换图的某些行或者是某些列(可以都换),使得 ...
- HDU 2819 - Swap - [二分图建模+最大匹配]
题目链接:https://cn.vjudge.net/problem/HDU-2819 Given an N*N matrix with each entry equal to 0 or 1. You ...
随机推荐
- POJ.1287 Networking (Prim)
POJ.1287 Networking (Prim) 题意分析 可能有重边,注意选择最小的边. 编号依旧从1开始. 直接跑prim即可. 代码总览 #include <cstdio> #i ...
- bzoj1024: [SCOI2009]生日快乐(DFS)
dfs(x,y,n)表示长为x,宽为y,切n块 每次砍的一定是x/n的倍数或者y/n的倍数 #include<bits/stdc++.h> using namespace std; con ...
- MongoDB入门(3)- MongoDB备份与恢复
1. 备份 MongoDB提供了备份工具,mongodump.exe,在bin目录下,其用法如下: mongodump.exe -h localhost -d database_name -o d:\ ...
- LightOJ 1013 - Love Calculator LCS
题意:找一个串使给出的两个串都是它的子串,要求最短,求出最短长度,以及种类数. 思路:可以想到,当两个子串a,b拥有最长的公共子串为LCS时,那么可以求出的最短的串为lena+lenb-LCS. 那么 ...
- ZOJ3874 Permutation Graph
Time Limit: 2 Seconds Memory Limit: 65536 KB Edward has a permutation {a1, a2, … an}. He finds ...
- mac系统用docker安装oracle数据库
oracle没有mac可用的版本,最好的办法是通过docker安装 一.下载docker 1.通过brew下载 brew cask install docker 2.手动下载(需要vpn) https ...
- Fetch-新一代Ajax API
AJAX半遮半掩的底层API是饱受诟病的一件事情. XMLHttpRequest 并不是专为Ajax而设计的. 虽然各种框架对 XHR 的封装已经足够好用, 但我们可以做得更好. window.fet ...
- ribbon设置url级别的超时时间
序 ribbon的超时设置,只能按转发的serviceId来分的,无法像nginx那样直接在每个转发的链接里头设置超时时间.这里hack一下,实现url基本的ribbon超时时间设置.具体的思路就是重 ...
- 常见网络命令之traceroute命令一起其他常用命令
备注:任何命令+/?就可以显示命令帮助,比如:ipconfig /?. traceroute命令 traceroute是UNIX系统中的名字,用来跟踪一个分组从源点到终点的路径.在Windows系统中 ...
- 从零开始PHP攻略(001)——Bob的汽车零部件商店
1.创建订单表单 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> < ...