Poj 1719 Shooting Contest
// This question is intended for an r * c matrix. Each column has two white
// Select the c position. Each row must have at least one white square.
// You can select only one column.
// Use a binary match to obtain the maximum match. If the maximum match is r, the condition is met.
// At least one white grid is selected for each row
// Pay attention to c> r
# Include
# Include
# Include
Using namespace std;
Const int maxn = 1010;
Int line [maxn] [maxn];
Int match [maxn];
Int match_ans [maxn];
Int vis [maxn];
Int r, c;
Int find (int start)
{
Int I;
For (I = 1; I <= r; I ++)
{
If (line [start] [I] &! Vis [I])
{
Vis [I] = 1;
If (match [I] =-1 | find (match [I])
{
Match [I] = start;
Match_ans [start] = I;
Return 1;
}
}
}
Return 0;
}
Void Match ()
{
Int I;
Memset (match,-1, sizeof (match ));
Int ans = 0;
For (I = 1; I <= c; I ++)
{
Memset (vis, 0, sizeof (vis ));
If (find (I ))
Ans ++;
}
If (ans = r)
{
For (I = 1; I <= c; I ++)
{
If (! Match_ans [I])
{
For (int j = 1; j <= r; j ++)
If (line [I] [j])
{
Printf ("% d % c", j, I = c? '\ N ':'');
Break;
}
}
Else
Printf ("% d % c", match_ans [I], I = c? '\ N ':'');
}
}
Else
Printf ("NO \ n ");
}
Int main ()
{
// Freopen ("in.txt", "r", stdin );
Int T;
Scanf ("% d", & T );
While (T --)
{
Memset (line, 0, sizeof (line ));
Memset (match_ans, 0, sizeof (match_ans ));
Scanf ("% d", & r, & c );
Int a, B;
For (int I = 1; I <= c; I ++)
{
Scanf ("% d", & a, & B );
Line [I] [a] = line [I] [B] = 1;
}
Match ();
}
Return 1;
}