题目链接

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

#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. 根据生产场景对Linux系统进行分区

    转自:http://oldboy.blog.51cto.com/2561410/629558 老鸟谈生产场景如何对linux系统进行分区? █  前言:    我们买房子时,会考虑1室1厅,2室1厅, ...

  2. EF使用报错说缺少引用

            在程序中已经引用了EF,也引用了System.Data,但是一起报这个错误:        在类前面也已经写了 using System.Data.Entity,百思不得其解,最后才发 ...

  3. jmeter处理响应结果中文乱码

    1. 在线程下面添加后置处理器BeanShell PostProcessor,增加script:prev.setDataEncoding("UTF-8"); 2. 在jmeter. ...

  4. 『AngularJS』ngShow

    原文 描述 ngShow指令显示或隐藏给定的基于标明ngShow属性的HTML元素.元素的显示或隐藏通过在元素上移除或添加ng-hide CSS类属性.".ng-hide"CSS类 ...

  5. Java IO学习--RandomAccessFile

    1.什么是 随机访问文件流 RandomAccessFile 这个类在很多资料上翻译成中文都是:随机访问文件,在中文里,随机是具有不确定的含义,指一会访问这里,一会访问那里的意思.如果以这种语义来解释 ...

  6. 可以完成99%的静态页面的HTML标签

    HTML:一套浏览器认知的规则HTML分为两个部分,头和身体.一个完整的网页相当于一个裸体的人,我们利用HTML给它穿上衣服,使它更好看.下面我将为大家介绍一下HTML一些基本的标签,而这些基本的标签 ...

  7. 企业级Nginx Web服务优化实战

    web优化一览总结表 优化类型 优化说明 优化方法 安全优化 隐藏nginx版本信息优化 修改nginx配置文件实现优化 server_tokens off: 修改nginx版本信息优化 修改ngin ...

  8. LeetCode 81——搜索旋转排序数组 II

    1. 题目 2. 解答 2.1. 方法一 基于 LeetCode 33--搜索旋转排序数组 中的方法二. 当 nums[mid] = nums[right] 时,比如 [1, 1, 2, 1, 1], ...

  9. node gyp的问题

    解决 binding.gyp not found (xxx/xxx/xxx) while trying to load binding.gyp 问题 在使用ccap图形验证码模块时遇到这个问题 Err ...

  10. c++调用Python基础功能

    c++调用Python首先安装Python,以win7为例,Python路径为:c:\Python35\,通过mingw编译c++代码.编写makefile文件,首先要添加包含路径:inc_path ...