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 ...
随机推荐
- 爆款PHP面试题
$a = 3; $b = 6; if ($a = 4 || $b = 4) { $a++; $b++; } echo $a; //输出 1 echo $b; //输出 7 逛鸟哥博客,看评论区有个新手 ...
- AtCoder Regular Contest 081 E - Don't Be a Subsequence(字符串DP)
引用自:onion_cyc 字符串DP一直不是强项...以后没思路的题就想DP和网络流23333333 f[i]表示从i开始的后缀非子序列的最短长度 pos[i][j]表示从i开始的j字符最早出现位 ...
- bzoj1014: [JSOI2008]火星人prefix(splay+hash+二分)
题目大意:一个字符串三个操作:①求两个后缀的LCP②插入一个字符③修改一个字符. 前几天刚学了hash+二分求lcp,就看到这题. 原来splay还能这么用?!原来splay模板这么好写?我以前写的s ...
- Codeforces Round #331 (Div. 2) A
A. Wilbur and Swimming Pool time limit per test 1 second memory limit per test 256 megabytes input s ...
- select和epoll概念
关于linux的I/O复用接口select和epoll,下列说法错误的是() select调用时会进行线性遍历,epoll采用回调函数机制,不需要线性遍历 select的最大连接数为FD_SETSIZ ...
- Qt -------- 容器类
QVector(数组).QLinkedList(链表).QMap(映射表).QHash(哈希表).QQueue(队列) QHash遍历举例: 法1: QThread& ThreadHandle ...
- RabbitMQ基础概念(消息、队列、交换机)
1.消息的确认 RabbitMQ需要对每一条发送的消息进行确认.消费者必须通过AMQP的basic.ack命令显式地向RabbitMQ发送一个确认,或者在订阅到队列的时候就将auto_ack参数设置为 ...
- sql获取当前时间
sql读取系统日期和时间的方法如下:--获取当前日期(如:yyyymmdd) select CONVERT (nvarchar(12),GETDATE(),112) --获取当前日期(如:yyyymm ...
- vijos 1004 伊甸园日历游戏 博弈+打表找规律
描述 Adam和Eve玩一个游戏,他们先从1900.1.1到2001.11.4这个日期之间随意抽取一个日期出来.然后他们轮流对这个日期进行操作: 1 : 把日期的天数加1,例如1900.1.1变到19 ...
- linux 下用 c 实现 ls -l 命令
#include <stdio.h> #include <sys/types.h> #include <dirent.h> #include <sys/sta ...