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. 硬盘坏道 检测/修复 Windows

    1. 主要参看了:http://jingyan.baidu.com/article/2c8c281dfd93df0008252a9b.html 2. 2.1.hdtunepro.zip 是在 http ...

  2. poj3347Kadj Squares

    链接 这题其实与几何没太大关系,还不错的题目. 参考吴永辉的算法设计书. 用lefi.rigi分别表示正方形在x轴上的投影. 为了避免用小数,把边长都扩大sqrt(2)倍,这样lef1 = 0,rig ...

  3. js optiontransferselect

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  4. 20160808_Linux服务

    1. http://bbs.csdn.net/topics/370100269 2. http://blog.csdn.net/csfreebird/article/details/8239933 h ...

  5. jQuery数组的遍历 function的加载

    加载函数时会被覆盖在jQuery中给提供的方案有三种形式 js中只能绑定一个方法 如果多次绑定后者会覆盖前者 最常用的一种 在jQuery中数组的遍历 使用map遍历数组  会返回一个新的数组  如果 ...

  6. C# Socket编程(4)初识Socket和数据流

    经过前面基础知识作为背景,现在对Socket编程进行进一步的学习.在System.Net.Socket命名空间提供了Socket类,利用该类我们可以直接编写Socket的客户端和服务的的程序.但是直接 ...

  7. 如何部署Scrapy 到Scrapyd上?

    安装上传工具 1.上传工具 scrapyd-client 2.安装方法: pip install scrapyd-client 3.上传方法: python d:\Python27\Scripts\s ...

  8. Now直播应用的后台服务器性能测试实践

    版权声明:本文由Oliver原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/208 来源:腾云阁 https://www.q ...

  9. HTML5游戏实战(4): 20行代码实现FlappyBird

    这个系列很久没有更新了.几个月前有位读者调侃说,能不能一行代码做一个游戏呢.呵呵,接下来一段时间,我天天都在想这个问题,怎么能让GameBuilder+CanTK进一步简化游戏的开发呢.经过几个月的努 ...

  10. SAP中寄售处理

    寄售分两种: 1, 供应商提供货物,我们销售 2,我们提供货物,寄售商销售 [@more@] 1, 供应商提供货物,我们销售 创建PO,购买寄售货物,categories维护成K,然后收货即可. 2, ...