HDU2819(二分图匹配,记录过程)
Swap
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2236 Accepted Submission(s): 801
Special Judge
If it is impossible to make all the diagonal entries equal to 1, output only one one containing “-1”.
/*
ID: LinKArftc
PROG: 2819.cpp
LANG: C++
*/ #include <map>
#include <set>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <utility>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define eps 1e-8
#define randin srand((unsigned int)time(NULL))
#define input freopen("input.txt","r",stdin)
#define debug(s) cout << "s = " << s << endl;
#define outstars cout << "*************" << endl;
const double PI = acos(-1.0);
const double e = exp(1.0);
const int inf = 0x3f3f3f3f;
const int INF = 0x7fffffff;
typedef long long ll; const int maxn = ; int mp[maxn][maxn];
int linker[maxn];
bool vis[maxn];
int n; bool dfs(int u) {
for (int v = ; v <= n; v ++) {
if (!vis[v] && mp[u][v]) {
vis[v] = true;
if (linker[v] == - || dfs(linker[v])) {
linker[v] = u;
return true;
}
}
}
return false;
} int hungry() {
memset(linker, -, sizeof(linker));
int ret = ;
for (int i = ; i <= n; i ++) {
memset(vis, , sizeof(vis));
if (dfs(i)) ret ++;
}
return ret;
} int a[maxn], b[maxn]; int main() {
//input;
while (~scanf("%d", &n)) {
int tmp;
memset(mp, , sizeof(mp));
for (int i = ; i <= n; i ++) {
for (int j = ; j <= n; j ++) {
scanf("%d", &tmp);
if (tmp) mp[i][j] = ;
}
}
int ans = hungry();
if (ans < n) printf("-1\n");
else {
int cnt = ;
for (int i = ; i <= n; i ++) {
int j;
for (j = i; j <= n; j ++) {
if (linker[j] == i) break;
}
if (j != i) {
cnt ++;
a[cnt] = i; b[cnt] = j;
swap(linker[i], linker[j]);
}
}
printf("%d\n", cnt);
for (int i = ; i <= cnt; i ++) printf("C %d %d\n", a[i], b[i]);
}
} return ;
}
HDU2819(二分图匹配,记录过程)的更多相关文章
- hdu2819二分图匹配
Given an N*N matrix with each entry equal to 0 or 1. You can swap any two rows or any two columns. C ...
- HDU5090--Game with Pearls 二分图匹配 (匈牙利算法)
题意:给N个容器,每个容器里有一定数目的珍珠,现在Jerry开始在管子上面再放一些珍珠,放上的珍珠数必须是K的倍数,可以不放.最后将容器排序,如果可以做到第i个容器上面有i个珍珠,则Jerry胜出,反 ...
- 1027 姓名与ID[二分图匹配(匈牙利)]
1027 姓名与ID 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 查看运行结果 题目描述 Description 有N个人,各自有一 ...
- (转)二分图匹配匈牙利算法与KM算法
匈牙利算法转自于: https://blog.csdn.net/dark_scope/article/details/8880547 匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名 ...
- 经典网络流题目模板(P3376 + P2756 + P3381 : 最大流 + 二分图匹配 + 最小费用最大流)
题目来源 P3376 [模板]网络最大流 P2756 飞行员配对方案问题 P3381 [模板]最小费用最大流 最大流 最大流问题是网络流的经典类型之一,用处广泛,个人认为网络流问题最具特点的操作就是建 ...
- UVA 12549 - 二分图匹配
题意:给定一个Y行X列的网格,网格种有重要位置和障碍物.要求用最少的机器人看守所有重要的位置,每个机器人放在一个格子里,面朝上下左右四个方向之一发出激光直到射到障碍物为止,沿途都是看守范围.机器人不会 ...
- POJ 1274 裸二分图匹配
题意:每头奶牛都只愿意在她们喜欢的那些牛栏中产奶,告诉每头奶牛愿意产奶的牛棚编号,求出最多能分配到的牛栏的数量. 分析:直接二分图匹配: #include<stdio.h> #includ ...
- UVa 二分图匹配 Examples
这些都是刘汝佳的算法训练指南上的例题,基本包括了常见的几种二分图匹配的算法. 二分图是这样一个图,顶点分成两个不相交的集合X , Y中,其中同一个集合中没有边,所有的边关联在两个集合中. 给定一个二分 ...
- 博弈论(二分图匹配):NOI 2011 兔兔与蛋蛋游戏
Description Input 输入的第一行包含两个正整数 n.m. 接下来 n行描述初始棋盘.其中第i 行包含 m个字符,每个字符都是大写英文字母"X".大写英文字母&quo ...
- POJ2584 T-Shirt Gumbo 二分图匹配(网络流)
#include <cstdio> #include <cstring> #include <algorithm> const int inf=0x3f3f3f3f ...
随机推荐
- 第二篇 Python初识别及变量名定义规范
第一个Python程序 可以打开notepad或者其他文本编辑器,输入:print("Hello Python!"),将文件保存到任意盘符下,后缀名是 .py 两种python程 ...
- 【Java】Map转换器
描述: 在控制层接收参数时候, 往往会出现Json格式需要转换为Bean. 通常一两个字段可以用new去save pojo, 但字段多的情况呢? 以下就是为了解决这个尴尬情况, 自己写一个转换工具类 ...
- 孤荷凌寒自学python第七十二天开始写Python的第一个爬虫2
孤荷凌寒自学python第七十二天开始写Python的第一个爬虫2 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 直接上代码.详细过程见文末屏幕录像 ...
- Visual Studio 2012安装包
点击下载
- Chrome 与 Firefox-Dev 的 DevTools
不管是做爬虫还是写 Web App,Chrome 和 Firefox 的 DevTools 都是超常用的,但是经常发现别人的截图有什么字段我找不到,别人的什么功能我的 Chrome 没有,仔细一搜索才 ...
- 【EasyNetQ】- 发布
EasyNetQ支持的最简单的消息传递模式是发布/ 订阅.这种模式是消除消费者信息提供者的绝佳方式.出版商简单地向全世界说,“这已经发生了”或“我现在有了这些信息”.它不关心是否有人正在倾听,他们可能 ...
- JavaScript内置对象常用
Math 提供了数学中常用的属性和方法,使用时直接用Math.属性/方法,而不需要new一个Math对象 Date 使用Date对象来对日期和时间进行操作.使用时,必须用new创建一个实例 windo ...
- JavaScript选择打开手机网站还是电脑网站
现在手机网站越来越普遍,类似京东.淘宝.新浪等等大家都推出了wap版,一种简单的方法判断,JavaScript选择打开手机网站还是电脑网站,如果是手机网站就让网页跳转到手机网址.如果是电脑网站,打开电 ...
- warning LNK4070的解决办法
原文链接地址:http://blog.csdn.net/clever101/article/details/5898073#comments 重命名了一个MFC常规DLL ...
- [Leetcode] plus one 加一
Given a number represented as an array of digits, plus one to the number. 题意:给定数以数组的形式存储,然后计算该数加1的值. ...