1. 程式人生 > >Shooting Contest(POJ-1719)(二分最大匹配)

Shooting Contest(POJ-1719)(二分最大匹配)

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

題目大意:

現在給出一個矩陣,每列兩個白點其餘都是黑點,要求每列只能選擇一個白點,每行至少選擇一個白點。

題目分析:

二分圖匹配,白點的位置行列建邊,然後跑匹配,如果匹配數不等於行數,就是是無解,然後輸出方案的時候注意,如果有位置是沒匹配的,說明這個位置是多餘的,但是還是要任意選一個白點來輸出。

程式碼:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
typedef long long LL;
const int N=1111;
bool book[N];
int match[N],num[N];
vector <int> close[N]; //用行指向列存邊
bool f(int x)
{
    int m=close[x].size();
    for(int i=0 ; i < m ; i++)
    {
        int g=close[x][i];
        if(book[g]==false)
        {
            book[g]=true;
            if(match[g]==-1||f(match[g]))
            {
                match[g]=x;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    int n,m,sum;
    int x,y,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(match,-1,sizeof(match));
        memset(num,0,sizeof(num));
        for(int i=0; i<=m; i++)    close[i].clear();
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&x,&y);
            num[i]=x; //儲存白點
            close[x].push_back(i);
            close[y].push_back(i);
        }
        sum=0;
        for(int i=1; i<=n; i++)
        {
            memset(book,false,sizeof(book));
            if(f(i)) sum++;
        }
        if(sum!=n || n>m) //如果匹配數不等於行數 無解
        {
            printf("NO\n");
            continue;
        }
        for(int i=1; i<=m; i++)
        {
            if(i==1)//如果未匹配則隨意輸出一個白點
                printf("%d",match[i]==-1 ? num[i]:match[i]);
            else
                printf(" %d",match[i]==-1 ? num[i]:match[i]);
        }
        printf("\n");

    }
    return 0;
}