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 ;}