题目链接

题意:给定一个矩阵,每列有两个白点,其它都是黑点,如今要求每列选一个白点,使得每一行至少包括一个白点被选中

思路:利用白点所在的位置用行指向列建图,用行去匹配列,最大匹配数假设不等于行数的话,就无解。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm> using namespace std; const int MAXN = 1005; vector<int> g[MAXN];
int linker[MAXN];
bool used[MAXN];
int r, c; bool dfs(int u) {
for (int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
if (!used[v]) {
used[v] = true;
if (linker[v] == -1 || dfs(linker[v])) {
linker[v] = u;
return true;
}
}
}
return false;
} int save[MAXN]; void hungary() {
int res = 0;
memset(linker, -1, sizeof(linker));
for (int u = 1; u <= r; u++) {
memset(used, false, sizeof(used));
if (dfs(u)) res++;
}
if (res != r) {
printf("NO\n");
return;
}
for (int i = 1; i <= c; i++)
printf("%d%c", linker[i] != -1 ? linker[i] : save[i], i == c ? '\n' : ' ');
} int main() {
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%d%d", &r, &c);
for (int i = 1; i <= r; i++) g[i].clear(); int pos;
for (int i = 1; i <= c; i++) {
for (int j = 0; j < 2; j++) {
scanf("%d", &pos);
save[i] = pos;
g[pos].push_back(i);
}
}
hungary();
}
return 0;
}

POJ1719- Shooting Contest(二分图最大匹配)的更多相关文章

  1. Shooting Contest 射击比赛 [POJ1719] [CEOI1997] [一题多解]

    Description(下有中文题意) Welcome to the Annual Byteland Shooting Contest. Each competitor will shoot to a ...

  2. POJ 1719 Shooting Contest(二分图匹配)

    POJ 1719 Shooting Contest id=1719" target="_blank" style="">题目链接 题意:给定一个 ...

  3. HDU1045(KB10-A 二分图最大匹配)

    Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  4. poj 1719 Shooting Contest

    http://poj.org/problem?id=1719 Shooting Contest Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  5. HDU2389(KB10-F 二分图最大匹配Hopcroft_Karp)

    Rain on your Parade Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 655350/165535 K (Java/Ot ...

  6. HDU2819(KB10-E 二分图最大匹配)

    Swap Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  7. HDU4185(KB10-G 二分图最大匹配)

    Oil Skimming Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  8. HDU 1045 - Fire Net - [DFS][二分图最大匹配][匈牙利算法模板][最大流求二分图最大匹配]

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1045 Time Limit: 2000/1000 MS (Java/Others) Mem ...

  9. poj 1719 Shooting Contest (二分匹配)

    Shooting Contest Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3812   Accepted: 1389 ...

随机推荐

  1. EasyUI禁用控制方法常采用

    EasyUI禁用控制方法常采用: 1.validatebox使用可以使用:前两个适用于个人validatebox;   第三适用于整个form内箱;    <1>.$("#id& ...

  2. 64位操作系统下用Microsoft.Jet.OLEDB.4.0出现未注册错误

    在WIN7 64位下用Microsoft.Jet.OLEDB.4.0方法访问数据库Access,出现未注册错误 如果是要建立64位的应用程序 1.Microsoft Access Database E ...

  3. Swift伟大的编程语言数据采集

    Swift 2048 https://github.com/austinzheng/swift-2048 苹果官方Swift文档<The Swift Programming Language&g ...

  4. NuttX 介绍

    (嵌入式 实时操作系统 rtos nuttx 7.1) NuttX 介绍 转载请注明出处:http://blog.csdn.net/zhumaill/article/details/24197637 ...

  5. SQL Server高可用——日志传送(4-2)——部署

    原文:SQL Server高可用--日志传送(4-2)--部署 前文再续,书接上一回.本章演示一下日志传送的具体过程 准备工作: 由于时间关系,已经装好了3台虚拟机,且同在一个域里面: SQL01:主 ...

  6. SQL Server 连接问题-TCP/IP

    原文:SQL Server 连接问题-TCP/IP 出自:http://blogs.msdn.com/b/apgcdsd/archive/2012/02/24/ms-sql-server-tcp-ip ...

  7. Script:SQL调优健康检查脚本

    Script:SQL调优健康检查脚本 http://www.askmaclean.com/archives/sql-tuning-health-check-script.html 以下脚本可以用于收集 ...

  8. ios 串

    1.是推断包括字符串 if( [str rangeOfString:@"hello"].location != NSNotFound) { NSLog(@"yes&quo ...

  9. 关于接收POST请求 $GLOBALS['HTTP_RAW_POST_DATA']

    总是产生变量包含有原始的 POST 数据.否则,此变量仅在碰到未识别 MIME 类型的数据时产生.不过,访问原始 POST 数据的更好方法是 php://input.$HTTP_RAW_POST_DA ...

  10. Ubuntu 14.04 grub2 温馨提示

    昨天win7在...的基础上,刚装几天发布Ubuntu14.04.Ubuntu14.04还是很不错的.但是,今天想去下一个Win7,但没有发现平时的开机显示grub2选项,直接进了Ubuntu! 我感 ...