博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU2819(KB10-E 二分图最大匹配)
阅读量:4335 次
发布时间:2019-06-07

本文共 3200 字,大约阅读时间需要 10 分钟。

Swap

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3800    Accepted Submission(s): 1401
Special Judge

Problem Description

Given 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?
 

 

Input

There 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.
 

 

Output

For 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”. 
 

 

Sample Input

2 0 1 1 0 2 1 0 1 0
 

 

Sample Output

1 R 1 2 -1
 

 

Source

 
按行交换。对行进行1-n编号,对列也进行编号。
若第i行的第j1、j2……列有1,则节点i与j1、j2……连边。
二分图跑最大匹配,为完美匹配方可。
matching数组保存每个点的匹配,模拟一下交换输出答案。
1 //2017-08-26  2 #include 
3 #include
4 #include
5 #include
6 7 using namespace std; 8 9 const int N = 100000; 10 const int M = 5000000; 11 int head[N], tot; 12 struct Edge{ 13 int to, next; 14 }edge[M]; 15 16 void init(){ 17 tot = 0; 18 memset(head, -1, sizeof(head)); 19 } 20 21 void add_edge(int u, int v){ 22 edge[tot].to = v; 23 edge[tot].next = head[u]; 24 head[u] = tot++; 25 26 edge[tot].to = u; 27 edge[tot].next = head[v]; 28 head[v] = tot++; 29 } 30 31 int n; 32 int matching[N]; 33 int check[N]; 34 bool dfs(int u){ 35 for(int i = head[u]; i != -1; i = edge[i].next){ 36 int v = edge[i].to; 37 if(!check[v]){
//要求不在交替路 38 check[v] = 1;//放入交替路 39 if(matching[v] == -1 || dfs(matching[v])){ 40 //如果是未匹配点,说明交替路为增广路,则交换路径,并返回成功 41 matching[u] = v; 42 matching[v] = u; 43 return true; 44 } 45 } 46 } 47 return false;//不存在增广路 48 } 49 50 //hungarian: 二分图最大匹配匈牙利算法 51 //input: null 52 //output: ans 最大匹配数 53 int hungarian(){ 54 int ans = 0; 55 memset(matching, -1, sizeof(matching)); 56 for(int u = 1; u <= n; u++){ 57 if(matching[u] == -1){ 58 memset(check, 0, sizeof(check)); 59 if(dfs(u)) 60 ans++; 61 } 62 } 63 return ans; 64 } 65 66 const int MAXID = 100; 67 int a[N], b[N], cnt; 68 69 int main() 70 { 71 std::ios::sync_with_stdio(false); 72 //freopen("inputE.txt", "r", stdin); 73 while(cin>>n){ 74 init(); 75 int v; 76 for(int i = 1; i <= n; i++){ 77 for(int j = 1; j <= n; j++){ 78 cin>>v; 79 if(v){ 80 add_edge(i, j+MAXID); 81 } 82 } 83 } 84 int match = hungarian(); 85 if(match != n)cout<<-1<

 

转载于:https://www.cnblogs.com/Penn000/p/7435303.html

你可能感兴趣的文章
微信测试账户
查看>>
Android ListView上拉获取下一页
查看>>
算法练习题
查看>>
学习使用Django一 安装虚拟环境
查看>>
Hibernate视频学习笔记(8)Lazy策略
查看>>
CSS3 结构性伪类选择器(1)
查看>>
IOS 杂笔-14(被人遗忘的owner)
查看>>
自动测试用工具
查看>>
前端基础之BOM和DOM
查看>>
[T-ARA/筷子兄弟][Little Apple]
查看>>
编译Libgdiplus遇到的问题
查看>>
【NOIP 模拟赛】Evensgn 剪树枝 树形dp
查看>>
java学习笔记④MySql数据库--01/02 database table 数据的增删改
查看>>
两台电脑如何实现共享文件
查看>>
组合模式Composite
查看>>
程序员最想得到的十大证件,你最想得到哪个?
查看>>
我的第一篇CBBLOGS博客
查看>>
【MyBean调试笔记】接口的使用和清理
查看>>
07 js自定义函数
查看>>
jQueru中数据交换格式XML和JSON对比
查看>>