Poj 1719 Shooting Contest

Source: Internet
Author: User

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;
}







































Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.