POJ-3041 asteroids Bipartite Graph Matching

Source: Internet
Author: User

This topic defines the coordinates of vertices in a given grid. Now we want to destroy these vertices. There is a weapon that can destroy one row or all targets in one column at a time, ask the minimum number of times that the weapon can be used to make all the targets disappear.

How can this problem be converted into a bipartite graph? The process is like this. First, there are two ways for each target to disappear. One is to use weapons on its row, one is to use weapons in the column where they are located. For all the targets, the targets can be cleared only when weapons are used in one of the target columns. So we can match the row and column of the target, and then find the minimum vertex overwrite. Because the minimum vertex overwrite satisfies an edge and at least one vertex is in this vertex set.

The Code is as follows:

# Include <cstdlib> # include <cstdio> # include <cstring> # include <algorithm> # define maxn 1000 using namespace STD; int n, m, Match [maxn], visit [maxn]; int G [maxn] [maxn]; bool path (int u) {// visit [u] = 1; // the above formula is incorrect, visit uses a part corresponding to U, so it cannot mark for (INT I = 1; I <= N; ++ I) {If (! G [u] [I] | visit [I]) {continue;} visit [I] = 1; // consider the access to part y if (! Match [I] | path (Match [I]) {match [I] = u; return true ;}} return false ;}int main () {int X, y, ANS = 0; scanf ("% d", & N, & M); For (INT I = 1; I <= m; ++ I) {scanf ("% d", & X, & Y); G [x] [Y] = 1 ;}for (INT I = 1; I <= N; ++ I) {memset (visit, 0, sizeof (visit); If (path (I) {++ ans ;}} printf ("% d \ n ", ans); 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.