Bzoj 1191: [hnoi2006] Superhero hero [bipartite graph matching]

Source: Internet
Author: User

The model of the Bipartite Graph is an adaptation of a classic question. As shown in a graph theory book, there are n people and M positions, and each position can only be provided to one person, while each person can only be qualified for a limited position due to limited ability, ask if there is a way to make everyone have a job, and if not, how many people can provide a job at most. If you have read this classic question, the idea of this question is as follows: Consider n questions as N points belonging to X parts of the Bipartite Graph, think of M tips as M Points belonging to y parts. Use the Hungary algorithm to match the two parts. When a point cannot match, the algorithm ends.

Because of the characteristics of the two-part graph, each vertex in Part X has only two edges connected to part y, using this feature, we can greatly optimize this algorithm (the program below is a brainless Hungary, and it is useless .... )

# Include <iostream>

# Include <cstdio>

# Include <math. h>

# Include <string. h>

Using namespace STD;

Intmatch [1009], visit [1009] = {0}, N, map [1009] [1009] = {0 };

Int DFS (int K)

{

For (INT I = 0; I <= n-1; I ++)

{

If (Map [k] [I] = 1 & visit [I] = 0)

{

Visit [I] = 1;

If (Match [I] =-1) | (DFS (Match [I]) = 1 ))

{

Match [I] = K;

Return 1;

}

}

}

Return 0;

}

Int main ()

{

Int M, ANS = 0, x, y;

Scanf ("% d", & N, & M );

For (INT I = 1; I <= m; I ++)

{

Scanf ("% d", & X, & Y );

Map [I] [x] = 1;

Map [I] [Y] = 1;

}

Memset (match, 255, sizeof (MATCH ));

For (INTI = 1; I <= m; I ++)

{

Memset (visit, 0, sizeof (visit ));

If (DFS (I) = 1) ans ++; else break;

}

Printf ("% d", ANS );

Scanf ("% d", & N, & M );

Return0;

}

Bzoj 1191: [hnoi2006] Superhero hero [bipartite graph matching]

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.