您的位置:首页 > 其它

poj 1719 Shooting Contest

2015-09-12 11:28 281 查看
Shooting Contest

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 4127Accepted: 1516Special 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: