题目链接

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

#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. 从装机到配置-CentOS6.5

    L006课程结束后的总结 首先:系统(cat /etc/redhat-release):CentOS release 6.5 (Final) 版本(uname -r):2.6.32-431.el6.x ...

  2. Python 3基础教程31-urllib模块

    本文介绍Python里的urllib模块,这个urllib主要处理web服务的,如果需要做接口测试,或者写Python的网络爬虫,这个urllib就是最底层的库.需要用到里面的请求方法等. 1. 先看 ...

  3. Java IO学习--File类

    一.File类 File类具备一定的误导性,可能容易认为它指代的是文件,实际并非如此,它既能代表一个特定文件的名称,又能表示一个目录下一组文件的名称.简而言之,File类是文件或者目录路径名的抽象表示 ...

  4. 九度OJ--1163(C++)

    #include <iostream>#include <vector> using namespace std; int main() { int n; while(cin& ...

  5. MVC项目用Windsor注入

    第一步创建controler 注入类 public class ApiControllersInstaller : IWindsorInstaller {  public void Install(I ...

  6. [leetcode-655-Print Binary Tree]

    Print a binary tree in an m*n 2D string array following these rules: The row number m should be equa ...

  7. w命令集合

    startx:在命令行模式下输入会进入图形界面 exit:注销Linux(以login shell登录会注销账号,以non-login shell登录会退出终端) data:显示日期和时间 data ...

  8. 望岳物业App开发过程记录

    望岳物业APP开发过程记录 ——杜冰青 1.小组讨论,决定模块功能. 2.开始做“社区活动”界面,完成主页面.分享界面.内容界面,但是分享功能暂时没有完成. 3.接着做“一键开门”界面,因为硬件设施跟 ...

  9. mysql与hive2.1.1安装和配置

    1.mysql安装 这个安装很简单,是在线安装,只需要按顺序执行一下几个命令就ok了. (1)sudo apt-get install mysql-server (2)sudo apt-get ins ...

  10. springmvc项目搭建五-postgresql+easyui的数据显示

    上一篇虽然完成了页面的显示,但是是假数据,本篇添加了postgresql的数据库,将登陆的校验和数据的显示都通过数据库来完成. 我是在本地搭建了一个postgre的数据库,就先新建两张表吧,一个用于用 ...