Poj 3020 Algorithm for maximum matching of a general image with flowers and trees
Poj 3020 Algorithm for maximum matching of a general image with flowers and trees
Question:
Given an h * w graph, each vertex is 'O' or '*'. The minimum number of 1*2 rectangles is used to overwrite all '*' in the graph.
Restrictions:
1 <= h <= 40; 1 <= w <= 10
Ideas:
Minimum edge coverage = | V |-maximum matching
Max matching of general images and algorithms with flowers and trees
/* Poj 3020 Algorithm for maximum matching of a general graph with flowers and trees. A h * w graph is provided. Each vertex is 'O' or '*', the minimum number of 1*2 rectangles is required to overwrite all '*' in the graph. Restriction: 1 <= h <= 40; 1 <= w <= 10 train of thought: minimum edge coverage = | V |-maximum matching: The maximum matching of a general graph, flower tree algorithm */# include
# Include
# Include
# Include
Using namespace std; const int MAX_V = 405; // query set maintenance int fa [MAX_V]; int getFa (int x) {return fa [x] = x? X: fa [x] = getFa (fa [x]);} void merge (int a, int B) {a = getFa (a); B = getFa (B ); if (! = B) fa [a] = B;} int V, match [MAX_V]; vector
E [MAX_V]; int Q [MAX_V], rear; int next [MAX_V], mark [MAX_V], vis [MAX_V]; // The SIMPLE algorithm is used to search for two points x on the tree in a certain stage. The nearest common ancestor rint LCA (int x, int y) of y {static int t = 0; t ++; while (true) {if (x! =-1) {x = getFa (x); // The vertex must correspond to the corresponding flower. if (vis [x] = t) return x; vis [x] = t; if (match [x]! =-1) x = next [match [x]; else x =-1;} swap (x, y) ;}} void group (int a, int p) {while (! = P) {int B = match [a], c = next [B]; // The next array is used to mark the path in the flowers. It is used in combination with the match array, in fact, a/two-way linked list is formed. For example, (x, y) is matched. next [x] and next [y] can represent two directions. If (getFa (c )! = P) next [c] = B; // all vertices in the odd ring have the opportunity to find a match outside the ring, so they must be marked as s points and added to the queue, // because the number of matches in the ring is saturated, These vertices can match at most one point, in the aug, the break terminates the search in the current stage every time a vertex is matched, and the mark in the next stage is heavy // new. This is done to ensure this. If (mark [B] = 2) mark [Q [rear ++] = B] = 1; if (mark [c] = 2) mark [Q [rear ++] = c] = 1; merge (a, B); merge (B, c); a = c ;}} // augmented void aug (int s) {for (int I = 0; I <V; I ++) // re-mark next [I] =-1, fa [I] = I, mark [I] = 0, vis [I] =-1 for each stage; mark [s] = 1; Q [0] = s; rear = 1; for (int front = 0; match [s] =-1 & front <rear; front ++) {int x = Q [front]; // all vertices in queue Q are S-type for (int I = 0; I <(int) e [x]. size (); I ++) {I Nt y = e [x] [I]; if (match [x] = y) continue; // x and y match, ignore if (getFa (x) = getFa (y) continue; // x and y are in the same flower. Ignore if (mark [y] = 2) continue; // y is a T-type vertex, ignore if (mark [y] = 1) {// y is an S-type vertex, int r = LCA (x, y ); // r is the first public node from I and j to s if (getFa (x )! = R) next [x] = y; // r and x are not in the same flower, and next marks the path in the flower if (getFa (y )! = R) next [y] = x; // r and y are not in the same flower, next mark the path in the flowers // scale the entire r -- x-y --- r's odd ring into a point. r is used as the mark node of this ring, which is equivalent to the super node group (x, r); // The contraction path r --- x is the point group (y, r ); // shrink path r --- y is point} else if (match [y] =-1) {// y free, can be extended, r12 rule processing next [y] = x; for (int u = y; u! =-1;) {// cross-chain reverse int v = next [u]; int mv = match [v]; match [v] = u, match [u] = v; u = mv;} break; // search successful, exit the loop and enter the next stage} else {// the current search crossover chain + y + match [y] to form a new crossover chain, add match [y] To the queue as the node to be searched next [y] = x; mark [Q [rear ++] = match [y] = 1; // match [y] is also an S-type mark [y] = 2; // y is marked as T-type }}} bool g [MAX_V] [MAX_V]; const int N = 45; char str [N] [N]; int mp [N] [N]; int dx [] = {-1, 1, 0 }; int dy [] = {0, 0,-1, 1}; int h, w; bool OK (int x, int y) {if (x> = 0 & x
= 0 & y