题目链接

把每一列能射的两行和这一列连边,然后跑一边匈牙利就行了。

#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 (二分图匹配)的更多相关文章

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

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

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

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

  3. poj 1719 Shooting Contest

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

  4. POJ 3057 Evacuation(二分图匹配+BFS)

    [题目链接] http://poj.org/problem?id=3057 [题目大意] 给出一个迷宫,D表示门,.表示人,X表示不可通行, 每个门每时间单位只允许一个人通过, 每个人移动一格的为一时 ...

  5. POJ 3041 Asteroids (二分图匹配)

    [题目链接] http://poj.org/problem?id=3041 [题目大意] 一个棋盘上放着一些棋子 每次操作可以拿走一行上所有的棋子或者一列上所有的棋子 问几次操作可以拿完所有的棋子 [ ...

  6. POJ 2724 Purifying Machine (二分图匹配)

    题意 给定m个长度为n的01串(*既表示0 or 1.如*01表示001和101).现在要把这些串都删除掉,删除的方法是:①一次删除任意指定的一个:②如果有两个串仅有一个字符不同,则可以同时删除这两个 ...

  7. poj 1486 Sorting Slides(二分图匹配的查找应用)

    Description Professor Clumsey is going to give an important talk this afternoon. Unfortunately, he i ...

  8. poj 2536 GopherII(二分图匹配)

    Description The gopher family, having averted the canine threat, must face a new predator. The are n ...

  9. poj 2594 Treasure Exploration 二分图匹配

    点击打开链接题目链接 Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 7215   ...

  10. POJ 2195 Going Home (带权二分图匹配)

    POJ 2195 Going Home (带权二分图匹配) Description On a grid map there are n little men and n houses. In each ...

随机推荐

  1. js学习日记-隐式转换相关的坑及知识

    隐式转换比较是js中绕不过去的坎,就算有几年经验的工程师也很有可能对这块知识不够熟悉.就算你知道使用===比较从而避免踩坑,但是团队其它成员不一定知道有这样或那样的坑,有后端语言经验的人常常会形成一个 ...

  2. Spring框架中ModelAndView、Model、ModelMap的区别

    转自:http://blog.csdn.net/liujiakunit/article/details/51733211 1. Model Model 是一个接口, 其实现类为ExtendedMode ...

  3. JavaScript RegExp 身份证、账号密码、email正则

    什么是正则表达式 正则表达式是构成搜索模式. 在文本中搜索数据时,可以使用此搜索模式来描述正在搜索的内容. 正则表达式可以是单个字符,也可以是更复杂的模式. 正则表达式可用于执行所有类型的文本搜索和文 ...

  4. popen和system问题

    popen和system问题 1. 问题描述 C的代码里面去调用命令启动一个shell脚本,分别使用了下面两个途径. 其中一个是: func1(cmd) { popen(cmd,type); pclo ...

  5. LeetCode 622——设计循环队列

    1. 题目 设计你的循环队列实现. 循环队列是一种线性数据结构,其操作表现基于 FIFO(先进先出)原则并且队尾被连接在队首之后以形成一个循环.它也被称为"环形缓冲器". 循环队列 ...

  6. 学习bash——环境配置

    一.环境配置文件的重要性 Bash在启动时直接读取这些配置文件,以规划好bash的操作环境. 即使注销bash,我们的设置仍然保存. 二.login shell 通过完整的登录流程取得的bash,称为 ...

  7. Spring Boot学习(二):配置文件

    目录 前言 方式1:通过配置绑定对象的方式 方式2:@Value("${blog.author}")的形式获取属性值 相关说明 注解@Value的说明 参考 前言 Spring B ...

  8. lintcode-111-爬楼梯

    111-爬楼梯 假设你正在爬楼梯,需要n步你才能到达顶部.但每次你只能爬一步或者两步,你能有多少种不同的方法爬到楼顶部? 样例 比如n=3,1+1+1=1+2=2+1=3,共有3中不同的方法 返回 3 ...

  9. vsCode怎么为一个前端项目配置ts的运行环境

    vsCode为一个前端项目配置ts的运行环境,ts文件保存的时候自动编译成js文件: 假设此前端项目名称为Web:文件结构如图 1. 在根目录中新建一个“.vscode”文件夹,里面建一个“tasks ...

  10. php array_merge和“+”的区别和使用《细说php2》

    php array_merge和“+”的区别和使用