POJ题目1719 Shooting Contest(二分图)

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

CEOI 1997

题意:给定一个矩阵,每列有两个白点,其他都是黑点,现在要求每列选一个白点,使得每一行至少包含一个白点被选中

思路:二分图匹配,白点的位置行列建边,然后跑匹配,如果匹配数不等于行数,就是是无解,然后输出方案的时候注意,如果有位置是没匹配的,说明这个位置是多余的,但是还是要任意选一个白点来输出

ac代码

#include<stdio.h>
#include<string.h>
#include<vector>
#include<iostream>
using namespace std;
int r,c;
vector<int>vt[1010];
int save[1010],link[1010],vis[1010];
void init()
{
	int i;
	for(i=0;i<=r;i++)
	{
		vt[i].clear();
	}
}
int dfs(int x)
{
	int i,j;
	for(i=0;i<vt[x].size();i++)
	{
		int v=vt[x][i];
		if(!vis[v])
		{
			vis[v]=1;
			if(link[v]==-1||dfs(link[v]))
			{
				link[v]=x;
				return 1;
			}
		}
	}
	return 0;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int i;
		scanf("%d%d",&r,&c);
		init();
		for(i=1;i<=c;i++)
		{
			int x,y;
			scanf("%d%d",&x,&y);
			vt[x].push_back(i);
			vt[y].push_back(i);
			save[i]=y;
		}
		memset(link,-1,sizeof(link));
		int ans=0;
		for(i=1;i<=r;i++)
		{
			memset(vis,0,sizeof(vis));
			if(dfs(i))
				ans++;
		}
		if(r!=ans)
		{
			printf("NO\n");
			continue;
		}
		for(i=1;i<=c;i++)
		{
			if(i==1)
				printf("%d",link[i]==-1?save[i]:link[i]);
			else
				printf(" %d",link[i]==-1?save[i]:link[i]);
		}
		printf("\n");
	}
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值