2016-level algorithm final simulation exercise-E.ALVINZH's Youth Memory III

Source: Internet
Author: User

1083 Alvinzh's Youth Memory III thought

Puzzle, two-point diagram.

Say this is an examination of the topic of the dichotomy, you can say "impossible", where this is like a two-part map! This is really a binary graph, which examines the minimum vertex overlay.

You do not know the minimum vertex coverage does not matter, do not Baidu, first slowly analysis, do not preconceived.

Simplify test instructions. A, b two columns of numbers, if b[i]%a[j]==0, then they need to remove one. Ask at least how many people need to be removed!

Error method: Double cycle record can divide the two sides number B[i] and a[j], remember M1[a[j]]=m2[b[i]]=true, after the M1, M2 the number of true, take a smaller value output.

Why is this wrong? My backhand gives a counter example: 2,4,6 and 2,10,12, calculates that the number of true in M1, M2 is 3, but the answer is 2, as long as you remove the 2 in a and 12 in B.

The reason for the error is that it is not considered possible to delete some data in both arrays. So how do you deal with the possibility of deleting data on both sides? The answer is no consideration. Of course, I mean not to consider this situation, but not to consider the point, instead of considering the side, if there is a b[i]%a[j]==0, then the next two numbers will be connected to one side, indicating the need to delete one of them, our goal is to delete some of them, so that there is no connection between the two sides.

What does it mean that there is no connection? Yes, there is no augmented path!

The augmented path is defined as follows: P is an augmented path relative to m if p is a path to the two unmatched vertices in Figure g, and an edge that belongs to M and an edge that does not belong to M alternate on p.

So how do we deal with the deletion of points? Can not try to delete a point, DFS delete the process of Dfs to find the augmented path it, think all terrible. This is done by using the binary graph matching to complete the simulated delete point. Let's start with a definition transformation, a match for a binary graph (containing a collection of several edges, and any of the two edges without a public endpoint): Just delete one of the points in each edge of the match, and you can erase the edges. If there is the remaining edge, then the original image must have an augmented path, if the maximum matching two points, represents no augmented path, assuming that the maximum number of matches is x, then there must be a method, delete the maximum matching x points, so that the binary graph of the maximum match "collapse", that is, the matching edge does not exist. Note that the method of the deletion point must not arbitrarily select the matching edge two points in the point, because there is no augmented path, that is, the points in the unmatched edge must have a maximum matching vertex, the deletion of the process can not only delete the matching edge, but also to ensure that the unmatched edge deleted together.

I don't know, do you understand me? In the final analysis, this is the minimum vertex overlay for a binary graph match. I actually explained that the minimum vertex coverage = Two maximum match number, need to explain: theoretically also proved.

Minimum vertex overlay: The minimum number of vertices allows each edge in the binary graph G to be associated with at least one of the points. The minimum vertex cover number of a binary graph = Two The maximum number of matches for the graph.

Obviously, we are looking for this minimum vertex number, because each edge is associated with one of the points, if the minimum vertex number of the corresponding vertex is all deleted, then all the edges are gone.

The next is to find the maximum matching of the two-dimensional graph, do not say, reference alvinzh genius form Iv.

Analysis

Minimum point overlay exercises

Two battalion commander, where's your damn Italian cannon?

Maximum matching reference for minimum vertex overlay = two-point graph

Two-part diagram detailed

Reference Code
////Created by Alvinzh on 2017/12/5.//Copyright (c) alvinzh. All rights reserved.//#include <cstdio>#include <cstring>#include <vector>#define MAXSIZE 1005#define INF 0x3f3f3f3fusing namespaceStdintN, M, ans;vector<int> G[maxsize];//record matching of both disciplesintMatch[maxsize];//Record matching pointsintVisit[maxsize];//record is accessedintA[maxsize], b[maxsize];BOOLDfsintX//Search for augmented paths{ for(inti =0; I < g[x].size (); ++i) {intto = G[x][i];if(!visit[to]) {Visit[to] =1;if(!match[to] | | DFS (MATCH[TO)) {Match[to] = x;return true; }        }    }return false;}intMaxmatch () {ans =0; memset (Match,0,sizeof(match)); for(inti =1; I <= m; ++i) {memset (visit,0,sizeof(visit));//Clear Access        if(Dfs (i)) ans++;//try to extend from node I}returnAns;}intMain () { while(~SCANF ("%d %d", &n, &m)) { for(inti =1; i < MaxSize; ++i) g[i].clear (); for(inti =1; I <= N; ++i) scanf ("%d", &a[i]); for(inti =1; I <= m; ++i) {scanf ("%d", &b[i]); for(intj =1; J <= N; ++J) {if(B[i]% a[j] = =0) G[i].push_back (j); }} printf ("%d\n", Maxmatch ()); }}

2016-level algorithm final simulation exercise-E.ALVINZH's Youth Memory III

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.