Shooting Contest
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4097   Accepted: 1499   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

 
题目意思:
有n*m的格子,每个格子要么是白色要么是黑色。题目给定每列有2个白色的格子,其他的格子为黑色。每列选出一个白色的格子,使得每一行至少有一个白色格子被选出,若能满足条件则输出每列选出白色格子的行数,否则输出NO。
 
思路:
列标号放在左边,行标号放在右边,若该列a可选择第b行和第c行,则a-b连边,a-c连边。
在增广过程中记录to[i]和from[j]即第i列选择的行的标号和第j行被选择的列的标号。
init:to 0;from -1
1、若n>m,一定不可满足条件
2、若增广结束后存在from[i]=-1,则第i行一定不能被选择,输出NO
3、若增广结束后存在to[i]=0,那么第i列选择a和b其中一个即可。
 
代码:
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 1005
#define inf 999999999 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int from[N];
int to[N];
bool visited[N];
int n, m;
vector<int>ve[N]; int march(int u){
int i, v;
for(i=;i<ve[u].size();i++){
v=ve[u][i];
if(!visited[v]){
visited[v]=true;
if(from[v]==-||march(from[v])){
from[v]=u;
to[u]=v;
return ;
}
}
}
return ;
} main()
{
int t, i, j, k, u, v;
cin>>t;
while(t--){
scanf("%d %d",&n,&m);
for(i=;i<=m;i++) ve[i].clear();
for(i=;i<=m;i++){
scanf("%d %d",&u,&v);
ve[i].push_back(u);
ve[i].push_back(v);
}
if(n>m) {
printf("NO\n");continue;
}
memset(from,-,sizeof(from));
int num=;
for(i=;i<=m;i++){
memset(visited,false,sizeof(visited));
march(i);
}
int f=;
for(i=;i<=n;i++){
if(from[i]==-){
f=;break;
}
}
if(!f){
printf("NO\n");continue;
}
for(i=;i<=m;i++){
if(!to[i]){
to[i]=ve[i][];
}
}
printf("%d",to[]);
for(i=;i<=m;i++) printf(" %d",to[i]);
cout<<endl;
}
}

POJ 1719 二分图最大匹配(记录路径)的更多相关文章

  1. POJ 2226二分图最大匹配

    匈牙利算法是由匈牙利数学家Edmonds于1965年提出,因而得名.匈牙利算法是基于Hall定理中充分性证明的思想,它是二部图匹配最常见的算法,该算法的核心就是寻找增广路径,它是一种用增广路径求二分图 ...

  2. poj 2239 二分图最大匹配,基础题

    1.poj 2239   Selecting Courses   二分图最大匹配问题 2.总结:看到一个题解,直接用三维数组做的,很巧妙,很暴力.. 题意:N种课,给出时间,每种课在星期几的第几节课上 ...

  3. POJ 3984 迷宫问题 记录路径的广搜

    主要是学一下如何在广搜中记录路径:每找到一个点我就记录下这个点是由那个点得来的,这样我找到最后个点后,我就可以通过回溯得到我走过的路径,具体看代码吧~ #include<cstdio> # ...

  4. POJ Evacuation /// 二分图最大匹配

    题目大意: 在一个n*m的房间中 ‘X’为墙 ‘D’为门 ‘.’为人 门只存在与外围 人每秒钟只能向四连通区域走一步 门比较狭窄 每秒钟只能通过一个人 求所有人逃脱的最短时间 如果不可能则输出impo ...

  5. Pots POJ - 3414 (搜索+记录路径)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22688   Accepted: 9626   Special J ...

  6. poj 2724 二分图最大匹配

    题意: 会给出M个串,我们要做的就是将这M个串给清除了.对于任意两个串,若二进制形式只有一位不一样,那么这两个串可以在一次操作消除,否则每个操作只能消除一个串. 3 3 *01 100 011 可以代 ...

  7. Asteroids - poj 3041(二分图最大匹配问题)

      Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17258   Accepted: 9386 Description Be ...

  8. POJ 1422 二分图(最小路径覆盖)

    Air Raid Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7278   Accepted: 4318 Descript ...

  9. poj 2446 二分图最大匹配

    思路:由(i+j)为偶数的点向(i+j)为奇数的点建边.求一次最大匹配,若正好为空格数(不包含洞)的一半,即输出YES. #include<iostream> #include<cs ...

随机推荐

  1. python 将页面保存为word

    将博客或者留言页面保存为word文档 -----------2016-5-11 14:40:04-- source:http://blog.csdn.net/how8586/article/detai ...

  2. angularjs探秘<一>

    首先聊聊angularjs是啥. 首先AngularJS 是一个 JavaScript 框架.(PS:其实就是外部引用的js文件) 所以AngularJS的使用依然是外部引用js文件. 附上引用地址 ...

  3. NullPointerException异常的原因??

    所谓空指针异常,是因为用空(null)去调用属性或方法.   null表示没有这个对象,既然没有这个对象,那么去调用他的属性和方法,就会报异常.   <--主要有以下几种原因:   1.使用了未 ...

  4. GIT过滤

    git 创建 .gitignore 文件 建立项目过滤规则 创建 .gitignore 随意设置想跟踪哪些文件 和不跟踪哪些文件. 1.在项目根目录下建立 .gitignore 文件 2.   .gi ...

  5. oracle对象类型

    Oracle的对象类型 对象类型 在PL/SQL中,面向对象的程序设计师基于对象类型来完成的.对象类型是用户自定义的一种复合数据类型,它封装了数据结构和用于操纵这些数据结构的过程和函数. 数据库的对象 ...

  6. 转:strcat与strcpy与strcmp与strlen

    转自:http://blog.chinaunix.net/uid-24194439-id-90782.html strcat 原型:extern char *strcat(char *dest,cha ...

  7. Eclipse上安装springsource-tool-suite(zhuan)

    http://jingyan.baidu.com/article/1612d5005fd087e20f1eee10.html *********************************** s ...

  8. C# Web中Session的使用

    1. 关于使用之前要注意的: 在使用session之前,一定要注意继承自System.Web.UI.Page,或在使用session时加上System.Web.HttpContext.Current: ...

  9. AD组策略添加本地账号、设置允许ping回显

    AD组策略添加本地账号 1. 管理工具--组策略管理--选择相应GPO(编辑)----首选项--控制面板设置--本地用户和组--右键添加账号 2.域成员计算机刷新组策略(gpupdate/force) ...

  10. ASP.NET Core文档中Work with Data章节的翻译目录

    作为初学者看了相关的教程,遇到的问题有: 1. 教程不是针对初学者,往往在某一方面教的较深,但并不系统,不适合初学者: 2. 虽然翻译的很顺畅,但是谈了自己较多的开发体会,初学者看着困难,尤其是TOM ...