Shooting Contest
Time Limit: 1000MS | Memory Limit: 10000K | |||
Total Submissions: 4649 | Accepted: 1705 | 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
题目大意 : 输入一个N * M的矩阵, 每一列保证只有两个白色方块, 要求每一行必须得填充一个白色方块, 问你是否存在一种方案能够满足, 如果能 输出任意一种
思路 : 一开始没读懂题意一直WA, 后来发现他每一行其实可以放多个方块的。。。那就直接按行列存边, 看最后最大匹配数是否等于 N, 如果是, 按照匹配完的保存的边输出, 如果有一条边少了,那么任意输出一条边就行
Accepted code
#include<iostream>
#include<algorithm>
#include<functional>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<queue>
using namespace std;
#define sc scanf
#define ls rt << 1
#define rs ls | 1
#define Min(x, y) x = min(x, y)
#define Max(x, y) x = max(x, y)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define MEM(x, b) memset(x, b, sizeof(x))
#define lowbit(x) ((x) & (-x))
#define P2(x) ((x) * (x))
typedef long long ll;
const int MOD = 1e9 + 7;
const int MAXN = 2e3 + 100;
const int INF = 0x3f3f3f3f;
inline ll fpow(ll a, ll b){ ll r = 1, t = a; while (b){ if (b & 1)r = (r*t) % MOD; b >>= 1; t = (t*t) % MOD; }return r; }
vector <int> e[MAXN << 1];
int pre[MAXN], n, m, T;
bool vis[MAXN], mp[MAXN][MAXN];
void init() {
for (int i = 1; i <= m; i++) e[i].clear();
MEM(pre, 0); MEM(vis, 0); MEM(mp, 0);
}
bool find_(int x) {
for (int i = 0; i < SZ(e[x]); i++) {
int vi = e[x][i];
if (!vis[vi]) {
vis[vi] = true;
if (!pre[vi] || find_(pre[vi])) {
pre[vi] = x;
return true;
}
}
}
return false;
}
int main()
{
cin >> T;
while (T--) {
sc("%d %d", &n, &m); init();
for (int i = 1; i <= m; i++) {
int ui, vi;
sc("%d %d", &ui, &vi);
e[ui].push_back(i);
e[vi].push_back(i);
mp[i][ui] = mp[i][vi] = true; // 防止没有边可以任意输出一个
}
if (m < n) { cout << "NO" << endl; continue; } // 一定达不到N
int ans = 0;
for (int i = 1; i <= n; i++) {
MEM(vis, 0);
if (find_(i)) ans++;
}
if (ans != n) { cout << "NO" << endl; continue; }
for (int i = 1; i <= m; i++) {
if (pre[i]) cout << pre[i] << " "; // 输出匹配到的
else {
for (int j = 1; j <= n; j++) { // 任意输出
if (mp[i][j]) { cout << j << " "; break; }
}
}
}
cout << endl;
}
return 0;
}