POJ 1719 Shooting Contest(二分图匹配)
POJ 1719 Shooting Contest
id=1719" target="_blank" style="">题目链接
题意:给定一个矩阵,每列有两个白点,其它都是黑点,如今要求每列选一个白点,使得每一行至少包括一个白点被选中
思路:二分图匹配,白点的位置行列建边,然后跑匹配,假设匹配数不等于行数,就是是无解,然后输出方案的时候注意,假设有位置是没匹配的,说明这个位置是多余的,可是还是要随意选一个白点来输出
代码:
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std; const int N = 1005; int t, r, c;
vector<int> g[N];
int left[N], vis[N]; bool dfs(int u) {
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (vis[v]) continue;
vis[v] = 1;
if (!left[v] || dfs(left[v])) {
left[v] = u;
return true;
}
}
return false;
} int save[N]; void hungary() {
int ans = 0;
memset(left, 0, sizeof(left));
for (int i = 1; i <= r; i++) {
memset(vis, 0, sizeof(vis));
if (dfs(i)) ans++;
}
if (ans != r) {
printf("NO\n");
return;
}
for (int i = 1; i <= c; i++)
printf("%d%c", left[i] ? left[i] : save[i], i == c ? '\n' : ' ');
} int main() {
scanf("%d", &t);
while (t--) {
scanf("%d%d", &r, &c);
for (int i = 1; i <= r; i++)
g[i].clear();
int tmp;
for (int i = 1; i <= c; i++) {
for (int j = 0; j < 2; j++) {
scanf("%d", &tmp);
save[i] = tmp;
g[tmp].push_back(i);
}
}
hungary();
}
return 0;
}
POJ 1719 Shooting Contest(二分图匹配)的更多相关文章
- poj 1719 Shooting Contest (二分匹配)
Shooting Contest Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3812 Accepted: 1389 ...
- poj 1719 Shooting Contest
http://poj.org/problem?id=1719 Shooting Contest Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- POJ 3057 Evacuation(二分图匹配+BFS)
[题目链接] http://poj.org/problem?id=3057 [题目大意] 给出一个迷宫,D表示门,.表示人,X表示不可通行, 每个门每时间单位只允许一个人通过, 每个人移动一格的为一时 ...
- POJ 3041 Asteroids (二分图匹配)
[题目链接] http://poj.org/problem?id=3041 [题目大意] 一个棋盘上放着一些棋子 每次操作可以拿走一行上所有的棋子或者一列上所有的棋子 问几次操作可以拿完所有的棋子 [ ...
- POJ 2724 Purifying Machine (二分图匹配)
题意 给定m个长度为n的01串(*既表示0 or 1.如*01表示001和101).现在要把这些串都删除掉,删除的方法是:①一次删除任意指定的一个:②如果有两个串仅有一个字符不同,则可以同时删除这两个 ...
- poj 1486 Sorting Slides(二分图匹配的查找应用)
Description Professor Clumsey is going to give an important talk this afternoon. Unfortunately, he i ...
- poj 2536 GopherII(二分图匹配)
Description The gopher family, having averted the canine threat, must face a new predator. The are n ...
- poj 2594 Treasure Exploration 二分图匹配
点击打开链接题目链接 Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 7215 ...
- POJ 2195 Going Home (带权二分图匹配)
POJ 2195 Going Home (带权二分图匹配) Description On a grid map there are n little men and n houses. In each ...
随机推荐
- String、Date和Timestamp的互转
begin 2018年8月17日19:09:49 String.Date和Timestamp的互转 String和Date的互转 关于String和Date的互转,在java8后会有不同.因为java ...
- WebSocket In ASP.NET Core(一)
.NET-Core Series Server in ASP.NET-Core DI in ASP.NET-Core Routing in ASP.NET-Core Error Handling in ...
- Redis高可用集群-哨兵模式(Redis-Sentinel)搭建配置教程【Windows环境】
No cross,no crown . 不经历风雨,怎么见彩虹. Redis哨兵模式,用现在流行的话可以说就是一个"哨兵机器人",给"哨兵机器人"进行相应的配置 ...
- ubuntu下安装flash player,浏览器观看视频,本人ubuntu版本14.04
首先去官网下载flash player安装包:flash_player_npapi_linux.x86_64,下载地址:https://get.adobe.com/cn/flashplayer/ 解压 ...
- python3 开发面试题(面向对象)6.6
""" 封装.继承.多态 1. 谈谈你对面向对象的理解? 2. Python面向对象中的继承有什么特点? 3. 面向对象深度优先和广度优先是什么? 4. 面向对象中sup ...
- HNOI 越狱
题目描述 监狱有连续编号为 1…N的 N 个房间,每个房间关押一个犯人,有 M种宗教,每个犯人可能信仰其中一种.如果相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱. 输入输出格式 ...
- LPC43xx Asymmetric Dual Core : Cortex-M0 and Cortex-M4
- LPC18xx/43xx SWD/JTAG Debug Connector
- STM32F4, USB HS with ULPI and Suspend/Wakeup
Hi guys,I am in need of your help, unfortunately STs documentation is lacking some information here. ...
- ElasticSearch-.net平台下c#操作ElasticSearch详解
ElasticSearch系列学习 ElasticSearch第一步-环境配置 ElasticSearch第二步-CRUD之Sense ElasticSearch第三步-中文分词 ElasticSea ...