POJ 1719 Shooting Contest(二分图匹配)
来源:互联网 发布:韩国母亲剧情解析知乎 编辑:程序博客网 时间:2024/06/02 13:29
POJ 1719 Shooting Contest
题目链接
题意:给定一个矩阵,每列有两个白点,其他都是黑点,现在要求每列选一个白点,使得每一行至少包含一个白点被选中
思路:二分图匹配,白点的位置行列建边,然后跑匹配,如果匹配数不等于行数,就是是无解,然后输出方案的时候注意,如果有位置是没匹配的,说明这个位置是多余的,但是还是要任意选一个白点来输出
代码:
#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;}
1 0
- POJ 1719 Shooting Contest(二分图匹配)
- poj 1719 Shooting Contest 二分匹配
- [二分匹配]poj 1719 Shooting Contest
- POJ 1719 Shooting Contest 二分匹配
- POJ 1719 Shooting Contest 二分匹配
- poj 1719 Shooting Contest 二分匹配 匈牙利
- POJ 1719 Shooting Contest 二分图最大匹配
- POJ - 1719 Shooting Contest 二分图最大匹配
- Shooting Contest(POJ-1719)(二分最大匹配)
- POJ题目1719 Shooting Contest(二分图)
- poj 1719 Shooting Contest 二分图匹配的一种特殊情况
- POJ1719- Shooting Contest(二分图最大匹配)
- POJ--1719[Shooting Contest] 有源汇上下界可行流或二分匹配
- POJ 1719 Shooting Contest
- poj 1719 Shooting Contest
- poj 1719 Shooting Contest
- poj 1719Shooting Contest
- poj 1719 Shooting Contest
- iOS: (库) 图片异步下载/缓存-SDWebImage的使用
- PHP递归调用数组值并用其执行指定函数
- 浅析Java中CountDownLatch用法
- Multipart传递参数request收不到解决方法
- 数据模型 (LP32 ILP32 LP64 LLP64 ILP64 )
- POJ 1719 Shooting Contest(二分图匹配)
- java类加载机制
- 查看执行计划的6种方法
- struts2-json-plugin 使用
- 自己学习整理的Android端请求URL的方法
- 几种改变执行计划的方法
- 有趣的面试智力题
- Java类的加载,链接,初始化
- C++ STL体系结构、 编程方法及存在的问题