POJ 1719 Shooting Contest(KM,4级)

I - Shooting Contest
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u
Appoint description: 

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 棋盘,每一行得打一个白的,每一列也得打一个白的。打C次。C>R ,做完最大匹配后,只需满足列即可。

#include<cstdio>
#include<cstring>
#include<iostream>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
using namespace std;
const int mm=2230;
class Edge
{
  public:int v,next;
}e[mm*16];
int head[mm],edge,p[mm];
int n,m;
void init()
{
  clr(head,-1);edge=0;
}
void add(int u,int v)
{ p[u]=edge;
  e[edge].v=v;e[edge].next=head[u];head[u]=edge++;
}
int X[mm];bool T[mm];
bool dfs(int u)
{
  for(int i=head[u];~i;i=e[i].next)
  {
    int v=e[i].v;
    if(T[v])continue;
    T[v]=1;
    if(X[v]==-1||dfs(X[v]))
    {
      X[v]=u;return 1;
    }
  }
  return 0;
}
void getans()
{ int ret=0;
  clr(X,-1);
  FOR(i,1,n)
  {
   clr(T,0);
   if(dfs(i))
    ++ret;
  }
  if(ret<n)puts("NO");
  else
  {
    FOR(i,1,m)
    if(X[i]!=-1)
    printf("%d%c",X[i],i==m?'\n':' ');
    else printf("%d%c",e[ p[i+n] ].v,i==m?'\n':' ');
  }
}
int main()
{ int a,b,cas;
  while(~scanf("%d",&cas))
  {
    while(cas--)
    {
      init();
      scanf("%d%d",&n,&m);
      FOR(i,1,m)
      {
        scanf("%d%d",&a,&b);
        add(a,i);add(b,i);
        add(i+n,a);///保证每一列有一个。当n<m
      }
      if(m<n)
      {puts("NO");continue;}
      getans();
    }

  }
}



posted @ 2013-10-12 14:10  剑不飞  阅读(201)  评论(0编辑  收藏  举报