POJ-3041 row and column matching diagram + minimum vertex coverage

Source: Internet
Author: User

Given a grid, some grid coordinates now contain an minor. A weapon can penetrate one row or one column of the minor. How many times can this weapon be used to destroy all the minor planets.

Solution: as long as each vertex is overwritten by rows or columns, you can match the rows and columns where a certain vertex is located, and the problem is converted to a minimum vertex overwrite, this ensures that each edge has a vertex in the vertex set. That is, each minor can be overwritten by its row or column.

The Code is as follows:

# Include <iostream> # include <cstring> # include <cstdio> # include <cstdlib> # include <algorithm> using namespace STD; int n, m; char G [505] [505], vis [505]; int match [505]; int path (int u) {for (INT I = 1; I <= N; ++ I) {If (! G [u] [I] | Vis [I]) continue; vis [I] = 1; if (! Match [I] | path (Match [I]) {match [I] = u; return 1 ;}} return 0 ;} int query () {int ret = 0; memset (match, 0, sizeof (MATCH); For (INT I = 1; I <= N; ++ I) {memset (VIS, 0, sizeof (VIS); RET + = path (I);} return ret;} int main () {int A, B; while (scanf ("% d", & N, & M )! = EOF) {for (INT I = 0; I <m; ++ I) {scanf ("% d", & A, & B ); G [a] [B] = 1; // the row-column matching diagram. Finally, obtain a minimum vertex overwrite.} printf ("% d \ n", query ());} 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.