Hdu 1498 50 years, 50 colors (binary matching)

Source: Internet
Author: User

Question:
Here is a matrix of n * n, with s-colored balloons distributed in the matrix, and k bursts of balloons.
Each operation can break a line or a column of balloons of the same color. Q: k operations
The balloons of those colors cannot be completely broken.
Solution:
Use bipartite graph matching to find the maximum matching values for each color (the rows and columns are set A and Set B respectively; M [I, j] indicates A match ),
If the value is greater than k, "-1" is output; otherwise, the ascending sequence of colors is output.
Note: Find the least vertex in the bipartite graph, and associate each edge with at least one vertex. This is
The "minimum vertex overwrite" of a bipartite graph ".
[Csharp]
# Include "stdio. h"
# Include "string. h"
# Include "stdlib. h"
Int map [101] [101], mark [101];
Int v [101], link [101];
Int a [101], color [101];
Int k, n;
Int cmp (const void * a, const void * B)
{
Return * (int *) a-* (int *) B;
}
Int dfs (int I, int k)
{
Int j;
For (j = 1; j <= n; j ++)
{
If (map [I] [j] = k &&! V [j])
{
V [j] = 1;
If (link [j] = 0 | dfs (link [j], k ))
{
Link [j] = I;
Return 1;
}
}
}
Return 0;
}
Int main ()
{
Int I, j, t, tt, ans;
While (scanf ("% d", & n, & k )! =-1)
{
If (n = 0 & k = 0)
Break;
Memset (color, 0, sizeof (color ));
Memset (mark, 0, sizeof (mark ));
T = 0;
For (I = 1; I <= n; I ++)
{
For (j = 1; j <= n; j ++)
{
Scanf ("% d", & map [I] [j]);
If (! Mark [map [I] [j])
{
Mark [map [I] [j] = 1;
Color [t ++] = map [I] [j];
}
}
}
Tt = 0;
For (I = 0; I <t; I ++)
{
Ans = 0;
Memset (link, 0, sizeof (link ));
For (j = 1; j <= n; j ++)
{
Memset (v, 0, sizeof (v ));
If (dfs (j, color [I])
Ans ++;
}
If (ans> k)
A [tt ++] = color [I];
}
If (tt = 0)
Printf ("-1 \ n ");
Else www.2cto.com
{
Qsort (a, tt, sizeof (a [0]), cmp );
For (I = 0; I <TT-1; I ++)
Printf ("% d", a [I]);
Printf ("% d \ n", a [I]);
}
}
Return 0;
}
Author: yyf572132811

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.