Hdu 1498 Bipartite Graph-minimum vertex coverage

Source: Internet
Author: User

/*

There are 1-50 balloons in the matrix with n * n, and only one row or column can be destroyed at a time. K. After this operation, the balloons cannot be destroyed.

Solution: For each given color, when map [I] [j] = color, we can consider that there is an I-j path, so that we can build a bipartite graph, the two points in the bipartite graph are the horizontal and vertical coordinates of the color balloon, and the number of times required to destroy all the balloons of the colo r is the minimum point coverage of the Bipartite Graph.

What is vertex coverage


*/


[Cpp]
# Include <stdio. h>
# Include <string. h>
# Define M 105
# Define N 55
Int map [M] [M], link [M], flag [M], ans [N], n, k; // note: the length of the link and flag arrays should be equal to the number of columns in the matrix;
Int find (int I, int color)
{
Int j;
For (j = 1; j <= n; j ++ ){
If (map [I] [j] = color & flag [j] = 0 ){
Flag [j] = 1;
If (link [j] = 0 | find (link [j], color) = 1 ){
Link [j] = I;
Return 1;
}
}
}
Return 0;
}
Int getnum (int color)
{
Int I, sum;
Memset (link, 0, sizeof (link ));
For (I = 1, sum = 0; I <= n; I ++ ){
Memset (flag, 0, sizeof (flag ));
If (find (I, color) = 1)
Sum ++;
}
Return sum;
}
Int main ()
{
Int I, j, t;
While (scanf ("% d", & n, & k), n | k ){
Memset (map, 0, sizeof (map ));
Memset (ans, 0, sizeof (ans ));
For (I = 1; I <= n; I ++ ){
For (j = 1; j <= n; j ++ ){
Scanf ("% d", & map [I] [j]);
}
}
For (I = 1, t = 0; I <= 50; I ++) {// note that each color is traversed here, so the number of cycles equals to the number of all colors
If (getnum (I)> k)
Ans [t ++] = I;
}
If (t = 0)
Printf ("-1 \ n ");
Else {
For (I = 0; I <t; I ++ ){
Printf (I = 0? "% D": "% d", ans [I]);
}
Printf ("\ n ");
}
}
Return 0;
}

# Include <stdio. h>
# Include <string. h>
# Define M 105
# Define N 55
Int map [M] [M], link [M], flag [M], ans [N], n, k; // note: the length of the link and flag arrays should be equal to the number of columns in the matrix;
Int find (int I, int color)
{
Int j;
For (j = 1; j <= n; j ++ ){
If (map [I] [j] = color & flag [j] = 0 ){
Flag [j] = 1;
If (link [j] = 0 | find (link [j], color) = 1 ){
Link [j] = I;
Return 1;
}
}
}
Return 0;
}
Int getnum (int color)
{
Int I, sum;
Memset (link, 0, sizeof (link ));
For (I = 1, sum = 0; I <= n; I ++ ){
Memset (flag, 0, sizeof (flag ));
If (find (I, color) = 1)
Sum ++;
}
Return sum;
}
Int main ()
{
Int I, j, t;
While (scanf ("% d", & n, & k), n | k ){
Memset (map, 0, sizeof (map ));
Memset (ans, 0, sizeof (ans ));
For (I = 1; I <= n; I ++ ){
For (j = 1; j <= n; j ++ ){
Scanf ("% d", & map [I] [j]);
}
}
For (I = 1, t = 0; I <= 50; I ++) {// note that each color is traversed here, so the number of cycles equals to the number of all colors
If (getnum (I)> k)
Ans [t ++] = I;
}
If (t = 0)
Printf ("-1 \ n ");
Else {
For (I = 0; I <t; I ++ ){
Printf (I = 0? "% D": "% d", ans [I]);
}
Printf ("\ n ");
}
}
Return 0;
}

 

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.