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

Description

Welcome to the Annual Byteland Shooting Contest. Each competitor will shoot to a target which is a rectangular grid. The target consists of r*c squares located in r rows and c columns. The squares are coloured white or black. There are exactly two white squares and r-2 black squares in each column. Rows are consecutively labelled 1,..,r from top to bottom and columns are labelled 1,..,c from left to right. The shooter has c shots.

A volley of c shots is correct if exactly one white square is hit in each column and there is no row without white square being hit. Help the shooter to find a correct volley of hits if such a volley exists. 
Example 
Consider the following target: 

Volley of hits at white squares in rows 2, 3, 1, 4 in consecutive columns 1, 2, 3, 4 is correct. 
Write a program that: verifies whether any correct volley of hits exists and if so, finds one of them.

Input

The first line of the input contains the number of data blocks x, 1 <= x <= 5. The following lines constitute x blocks. The first block starts in the second line of the input file; each next block starts directly after the previous one.

The first line of each block contains two integers r and c separated by a single space, 2 <= r <= c <= 1000. These are the numbers of rows and columns, respectively. Each of the next c lines in the block contains two integers separated by a single space. The integers in the input line i + 1 in the block, 1 <= i <= c, are labels of rows with white squares in the i-th column.

Output

For the i-th block, 1 <= i <= x, your program should write to the i-th line of the standard output either a sequence of c row labels (separated by single spaces) forming a correct volley of hits at white squares in consecutive columns 1, 2, ..., c, or one word NO if such a volley does not exists.

Sample Input

2
4 4
2 4
3 4
1 3
1 4
5 5
1 5
2 4
3 4
2 4
2 3

Sample Output

2 3 1 4
NO

Source

 
    开始没弄清楚题意,WA了两次,题意是给出一个n*m的矩形,每一列有两格是白色的,要求匹配出每列一个,每行至少一个的匹配。不行输出NO。
    先正常进行最大匹配,输出结果时把没匹配到的列随便分配两个中的一个即可。
 //220K    110MS    C++    1341B    2014-06-03 15:44:38
//每列一个,每行至少一个
#include<iostream>
#include<vector>
#define N 1005
using namespace std;
vector<int>V[N];
int vis[N];
int match[N];
int p[N][];
int n,m;
int dfs(int u)
{
for(int i=;i<V[u].size();i++){
int v=V[u][i];
if(!vis[v]){
vis[v]=;
if(match[v]==- || dfs(match[v])){
match[v]=u;
return ;
}
}
}
return ;
}
int hungary()
{
memset(match,-,sizeof(match));
int ret=;
for(int i=;i<=m;i++){
memset(vis,,sizeof(vis));
ret+=dfs(i);
}
return ret;
}
int main(void)
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++) V[i].clear();
for(int i=;i<=m;i++){
scanf("%d%d",&p[i][],&p[i][]);
V[i].push_back(p[i][]);
V[i].push_back(p[i][]);
}
int prt[N]={};
if(hungary()==n){
for(int i=;i<=n;i++)
prt[match[i]]=i;
for(int i=;i<m;i++)
if(prt[i]==) printf("%d ",p[i][]);
else printf("%d ",prt[i]);
if(prt[m]==) printf("%d\n",p[m][]);
else printf("%d\n",prt[m]);
}else puts("NO");
}
return ;
}
 

poj 1719 Shooting Contest (二分匹配)的更多相关文章

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

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

  2. poj 1719 Shooting Contest

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

  3. poj 3057(bfs+二分匹配)

    题目链接:http://poj.org/problem?id=3057 题目大概意思是有一块区域组成的房间,房间的边缘有门和墙壁,'X'代表墙壁,'D'代表门,房间内部的' . '代表空区域,每个空区 ...

  4. POJ 1469(裸二分匹配)

    COURSES Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18993   Accepted: 7486 Descript ...

  5. poj 2584 T-Shirt Gumbo (二分匹配)

    T-Shirt Gumbo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2571   Accepted: 1202 Des ...

  6. poj 2239 Selecting Courses (二分匹配)

    Selecting Courses Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8316   Accepted: 3687 ...

  7. poj 2594 Treasure Exploration (二分匹配)

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 6558   Accepted: 2 ...

  8. poj 1325 Machine Schedule 二分匹配,可以用最大流来做

    题目大意:机器调度问题,同一个任务可以在A,B两台不同的机器上以不同的模式完成.机器的初始模式是mode_0,但从任何模式改变成另一个模式需要重启机器.求完成所有工作所需最少重启次数. ======= ...

  9. poj 2239 Selecting Courses(二分匹配简单模板)

    http://poj.org/problem?id=2239 这里要处理的是构图问题p (1 <= p <= 7), q (1 <= q <= 12)分别表示第i门课在一周的第 ...

随机推荐

  1. 单例模式之pymysql运用实例

    何为单例? 简单介绍一下下:单例是个什么鬼东西!!!! 单例模式含义] 单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例类的特殊类.通过单例模式可以保证系统中一个类只有一个实例而 ...

  2. Android 打印log 在logcat 看不到

    今天调试一个问题,因为是插件,只能通过打印log 定位问题. 但是打印了log 一直没有看到. 代码如下: Log.d("","aaaa24"); 后来发现是需 ...

  3. SpringBoot学习:整合shiro(rememberMe记住我后自动登录session失效解决办法)

    项目下载地址:http://download.csdn.NET/detail/aqsunkai/9805821 定义一个拦截器,判断用户是通过记住我登录时,查询数据库后台自动登录,同时把用户放入ses ...

  4. Linux命令应用大词典-第11章 Shell编程

    11.1 declare:显示或设置Shell变量 11.2 export:显示或设置环境变量 11.3 set:显示和设置Shell变量 11.4 unset:删除变量或函数 11.5 env:查看 ...

  5. Java 递归 反射 正则表达式

    一 递归 1. 就是函数自身调用自身 (就是在栈内存中不断的加载同一个函数) 2. 什么时候用递归呢? 当一个功能被重复使用 而每一次使用该功能时的参数不确定 都由上次的功能元素结果来确定 简单说: ...

  6. JavaScript基本概念(1)-声明提升

    声明提升: function > var > other var提升的时候,只是声明提升,但是赋值还是会在原来的位置. Javascript Hoisting:In javascript, ...

  7. Sql server在cmd下的使用

    方法一 在DOS提示符下,c:\>isql -U sa -P (此处输入SA密码) 注----大小写敏感 回车后会出现"1>"提示符,表明已成功,此时可在DOS下做任何 ...

  8. Python3 Tkinter-OptionMenu

    1.创建 from tkinter import * root=Tk() v=StringVar() v.set('xs') om=OptionMenu(root,v,'Python','PHP',' ...

  9. jquery datatable 常用例子

    在项目中经常用到DataTable,如果DataTable使用得当,不仅能使程序简洁实用,而且能够提高性能,达到事半功倍的效果,现对DataTable的使用技巧进行一下总结. 一.DataTable简 ...

  10. 十四:Using CGroups with YARN

        Cgroups可以控制linux 上应用程序的资源(内存.CPU)使用,yarn可以使用Cgroups来CPU使用.Cgroups的配置,在yarn-site.xml中设置: 1)启用Cgro ...