poj 1719 Shooting Contest

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

题目大意:给你一个r*c的矩阵,然后这个矩阵每一列有两个格子是白色的,然后你在每列上开一枪,可以打这列的任意的白色格子,问你能否消除全部的行,如果包括所有行输出你在1-c列打抢的格子的行,当然这可以是不唯一的,记好!!!是不唯一!!!!

如果打不到就输出NO 题目首先给出有几组数据,对于每组数据第一行代表r和c,接下来c行有两个数,代表这行的哪两列是白色格子

 

解题思路:和以前那个经典的二分匹配差不多,就是在一行打一枪可以消灭所有行那个题目,很容易想到二分匹配,我们将白色格子的行指向列,然后求最大匹配,用行去匹配列,看是否能够得到的匹配数是r,如果是r的话则代表所有的行都可以被打到,然后每列对应的行,如果匹配数不是r就输出NO 当然有特例,如果r>c,这种情况开c抢根本不可能达到r行,所以直接输出NO

代码:

#include<stdio.h>
#include<string.h>

bool map[1005][1005];
bool used[1005];
int link[1005];
int n,m;

bool dfs(int u)//寻找增广路! 找到返回1,否则返回0!
{
	int i;
	for(i=1;i<=m;i++)
	{
		if(map[u][i]&&!used[i])])//与x相连点,并且没有遍历到  
		{
			used[i]=true;;//标记为遍历过 
			if(link[i]==-1||dfs(link[i]))//i 点 没有和另一部分匹配或 和i配对的点 没有匹配! 
			{
				link[i]=u;
				return true;
			}
		}
	}
	return false;
}

int main()
{
	int i,j,x,y,t,num;
	scanf("%d",&t);
	while(t--)
	{
		num=0;
		scanf("%d%d",&n,&m);
		memset(map,0,sizeof(map));
		memset(link,-1,sizeof(link));
		if(n>m)//行大于列 
		{
			printf("NO\n");
			continue;
		}
		for(i=1;i<=m;i++)//建图 
		{
			scanf("%d%d",&x,&y);
			map[x][i]=true;
			map[y][i]=true;
		}
		for(i=1;i<=n;i++)
		{
			memset(used,0,sizeof(used));
			if(dfs(i))
				num++;
		}
		if(num<n)//不是完美匹配 
		{
			printf("NO\n");
			continue;
		}
		else
		{
			for(i=1;i<=m;i++)
			{
				if(link[i]!=-1) 
					printf("%d ",link[i]);
				else
				{
					for(j=1;j<=n;j++)
						if(map[j][i])
						{
							printf("%d ",j);
							break;
						}
				}
			}
			printf("\n");
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值