【POJ 1719】 Shooting Contest (二分图匹配)
把每一列能射的两行和这一列连边,然后跑一边匈牙利就行了。
#include <cstdio>
#include <cstring>
#include <algorithm>
using std::swap;
#define Open(s) freopen(s".in","r",stdin);freopen(s".out","w",stdout);
#define Close fclose(stdin);fclose(stdout);
inline int read(){
int s = 0;
char ch = getchar();
while(ch < '0' || ch > '9') ch = getchar();
while(ch >= '0' && ch <= '9'){ s = s * 10 + ch - '0'; ch = getchar(); }
return s;
}
const int MAXN = 2010;
int T, n, m, a[MAXN], b[MAXN], flag[MAXN], match[MAXN], c[MAXN], ans;
struct Edge{
int next, to;
}e[MAXN << 1];
int head[MAXN], num;
inline void Add(int from, int to){
e[++num].to = to;
e[num].next = head[from];
head[from] = num;
}
void print(){
for(int i = 1; i <= m; ++i){
if(!match[i]) match[i] = a[i];
printf("%d ", match[i]);
}
puts("");
}
int find(int x){
for(int i = head[x]; i; i = e[i].next)
if(!flag[e[i].to]){
flag[e[i].to] = 1;
if(!match[e[i].to] || find(match[e[i].to])){
match[e[i].to] = x;
return 1;
}
}
return 0;
}
int Judge(){
for(int i = 1; i <= m; ++i)
c[match[i]] = T;
for(int i = 1; i <= n; ++i)
if(c[i] != T)
return 1;
return 0;
}
int main(){
Open("she");
T = read() + 1;
while(--T){
num = 0; for(int i = 1; i <= (m << 1); ++i) head[i] = 0;
n = read(); m = read();
for(int i = 1; i <= m; ++i){
a[i] = read(); b[i] = read();
Add(a[i], i); Add(b[i], i);
match[i] = 0;
}
for(int i = 1; i <= n; ++i){
for(int i = 1; i <= m; ++i)
flag[i] = 0;
find(i);
}
if(Judge()) printf("NO\n");
else print();
}
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(二分图匹配)
POJ 1719 Shooting Contest id=1719" target="_blank" style="">题目链接 题意:给定一个 ...
- 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 ...
随机推荐
- Ubuntu 手机 app开发学习0
# 相关网址 http://developer.ubuntu.com/zh-cn/apps/sdk/ 0. 环境搭建 首选需要一个Ubuntu 14.04操作系统.没啥好讲的,直接安装了一个虚拟机. ...
- 【LoadRunner】解决LR11无法录制Chrome浏览器脚本问题
LoadRunner录制脚本时,遇到高版本的IE.FireFox,或者Chrome浏览器,会出现无法录制脚本的问题,下面就来讲一下如何利用LR自带的wplus_init_wsock.exe插件进行脚本 ...
- java设计模式之观察者模式以及在java中作用
观察者模式是对象的行为模式,又叫发布-订阅(Publish/Subscribe)模式.模型-视图(Model/View)模式.源-监听器(Source/Listener)模式或从属者(Dependen ...
- Hibernate 查询,返回结果设置到DTO
1:原生sql的查询,返回结果设置到DTO: Query query = sessionFactoryRtData.getCurrentSession().createSQLQuery(hql.toS ...
- Pro Git - 笔记3
Git Branching Branches in a Nutshell Branches in a Nutshell let’s assume that you have a directory c ...
- POJ 2168 Joke with Turtles(DP)
Description There is a famous joke-riddle for children: Three turtles are crawling along a road. One ...
- Daily Scrum02 11.29
今天大家都已经开始了进行第二轮迭代的工作!相比第一轮迭代,每个人都已经有了一定开发经验,这次做起来顺手很多.薛神和肖犇的挑战最大,他们需要实现好友功能,手机间的通信.服务器的搭建都是难点,但他们的热情 ...
- Sql Express数据备份和还原
参考文章:在SQL Server Express版本中没有代理功能如何自动备份数据库 首先用以下脚本,生成可以自动备份数据库的存储过程: USE [master] GO SET ANSI_NULLS ...
- Linux arm64的虚拟内存布局
原创翻译,转载请注明出处. 页表转换arm64在硬件体系结构上支持4级的每页大小为4K的页表转换,也支持3级的页大小64KB的页表转换.在linux arm64中,如果页的大小为4KB,使用3级页表转 ...
- PHPExcel 导出包含图片excel
<?php // 这里用的PHPExcel版本号为1.8.0 // 下载地址https://github.com/PHPOffice/PHPExcel 下载ZIP压缩包 // 下载后将Class ...