poj 1719 Shooting Contest
http://poj.org/problem?id=1719
Time Limit: 1000MS | Memory Limit: 10000K | |||
Total Submissions: 4135 | Accepted: 1521 | Special Judge |
Description
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 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
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的矩阵,矩阵由白格子和黑格子组成,每一列有两个格子是白色的剩下的为黑色,每一列射击一发子弹击中白色格子,问是否所有行都有白色格子被击中 数据分析:
2//数据组数
4 4//行r 列c
2 4//第1列的第2行和第4行是白色格子
3 4//第2列的第3行和第4行是白色格子
1 3//第3列的第1行和第3行是白色格子
1 4// ...
1.如果 r > c , c列射击完后仍会有行没有被射击过;
2.要从每一列开始射击并射中白色格子,即将行r和列c作为X,Y集合,白色格子部分进行匹配,得到最大匹配值ans
1>如果ans==r
(1)如果每一列都有匹配(即都能射中某一行的白色格子)就输出与该列匹配的行(即该列击中的的白色格子所在的行)
(2)如果某一列没有找到匹配的格子,那个只要在该行任意选择一个白色的格子就可以了,输出该白色格子所在的行
2>如果ans>r或者ans<r都无法满足每行都被射击过
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<algorithm>
#define INF 0x3f3f3f3f
#define N 1010 using namespace std; int G[N][N], vis[N], used[N];
int r, c; bool Find(int u)
{
int i;
for(i = ; i <= c ; i++)
{
if(!vis[i] && G[u][i])
{
vis[i] = ;
if(!used[i] || Find(used[i]))
{
used[i] = u;
return true;
}
}
}
return false;
}//匈牙利 int main()
{
int a, b, i, t;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &r, &c);
if(r > c)
{
printf("NO\n");
continue;
}//如果 r > c , c列射击完后仍会有行没有被射击过
memset(G, , sizeof(G));
for(i = ; i <= c ; i++)
{
scanf("%d%d", &a, &b);
G[a][i] = G[b][i] = ;//第i列个第a行和第b行是白色格子
}
memset(used, , sizeof(used));
int ans = ;
for(i = ; i <= r ; i++)
{
memset(vis, , sizeof(vis));
if(Find(i))
ans++;
}
if(ans == r)
{
for(i = ; i <= c ; i++)
{
if(used[i] != )//如果每一列都有匹配(即都能射中某一行的白色格子)就输出与该列匹配的行(即该列击中的的白色格子所在的行)
printf("%d ", used[i]);
else
{
for(int j = ; j <= r ; j++)
{
if(G[j][i])
{
printf("%d ", j);
break;
}
}
}//如果某一列没有找到匹配的格子,那个只要在该行任意选择一个白色的格子就可以了,输出该白色格子所在的行;
}
printf("\n");
}
else//如果ans>r或者ans<r都无法满足每行都被射击过
printf("NO\n");
}
return ;
}
poj 1719 Shooting Contest的更多相关文章
- POJ 1719 Shooting Contest(二分图匹配)
POJ 1719 Shooting Contest id=1719" target="_blank" style="">题目链接 题意:给定一个 ...
- poj 1719 Shooting Contest (二分匹配)
Shooting Contest Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 3812 Accepted: 1389 ...
- poj 2187 Beauty Contest(凸包求解多节点的之间的最大距离)
/* poj 2187 Beauty Contest 凸包:寻找每两点之间距离的最大值 这个最大值一定是在凸包的边缘上的! 求凸包的算法: Andrew算法! */ #include<iostr ...
- POJ 1719 二分图最大匹配(记录路径)
Shooting Contest Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4097 Accepted: 1499 ...
- Shooting Contest 射击比赛 [POJ1719] [CEOI1997] [一题多解]
Description(下有中文题意) Welcome to the Annual Byteland Shooting Contest. Each competitor will shoot to a ...
- POJ 3660 Cow Contest / HUST 1037 Cow Contest / HRBUST 1018 Cow Contest(图论,传递闭包)
POJ 3660 Cow Contest / HUST 1037 Cow Contest / HRBUST 1018 Cow Contest(图论,传递闭包) Description N (1 ≤ N ...
- 【POJ 1719】 Shooting Contest (二分图匹配)
题目链接 把每一列能射的两行和这一列连边,然后跑一边匈牙利就行了. #include <cstdio> #include <cstring> #include <algo ...
- poj 2187 Beauty Contest (凸包暴力求最远点对+旋转卡壳)
链接:http://poj.org/problem?id=2187 Description Bessie, Farmer John's prize cow, has just won first pl ...
- POJ 3660 Cow Contest
题目链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
随机推荐
- 用HTML5 Canvas为网页添加动态波浪背景
查看所有代码请去Github 本文出自 “UED” 博客:http://5344794.blog.51cto.com/5334794/1430877 <!DOCTYPE html> < ...
- HDU 1394 (逆序数) Minimum Inversion Number
原来求逆序数还可以用线段树,涨姿势了. 首先求出原始序列的逆序数,然后递推每一个序列的逆序数. #include <cstdio> #include <cstring> #in ...
- Codeforces 435 B Pasha Maximizes【贪心】
题意:给出一串数字,给出k次交换,每次交换只能交换相邻的两个数,问最多经过k次交换,能够得到的最大的一串数字 从第一个数字往后找k个位置,找出最大的,往前面交换 有思路,可是没有写出代码来---sad ...
- VIM Ctrl-V Conflict with Windows Paste
/************************************************************************************** * VIM Ctrl-V ...
- 20160205.CCPP体系详解(0015天)
程序片段(01):01.杨辉三角.c 内容概要:杨辉三角 #include <stdio.h> #include <stdlib.h> #define N 10 //01.杨辉 ...
- 【C#学习笔记】改变颜色
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- PS4破解
1.输入序列号: # 序列号: # 1330-1082-3503-2270-3738-6738# 1330-1776-8671-6289-7706-2916# 1330-1567-6599-8775- ...
- 正则表达式 java版
众所周知,在程序开发中,难免会遇到需要匹配.查找.替换.判断字符串的情况发生,而这些情况有时又比较复杂,如果用纯编码方式解决,往往会浪费程序员的时间及精力.因此,学习及使用正则表达式,便成了解决这一矛 ...
- MySQL5.6 ALTER TABLE 分析和测试
在MySQL5.5和之前版本,在运行的生产环境对大表(超过数百万纪录)执行Alter操作是一件很困难的事情.因为将重建表和锁表,影响用户者的使用.因此知道Alter操作何时结束对我们是非常重要的.甚至 ...
- 剑指offer-第二章排序之年龄排序
题目:对某个公司的人的年龄(0-99)进行排序,该公司的总人数为几万人.要求时间复杂度为O(n),可以辅助O(n)的空间. 思路:实现函数为void SortAge(int ages[],int le ...