POJ 30,201-like graph maximum matching with flower tree flowering algorithm
Test instructions
Give a h*w figure, each point is ' o ' or ' * ', at least how many 1*2 rectangles are needed to cover all the ' * ' in the graph.
Limit:
1 <= H <= 40; 1 <= w <= 10
Ideas:
Minimum Edge Overlay =| v|-Maximum Match
Maximum matching of general graphs, flowering algorithm with flower tree
/*poj 30,201-like graph maximum matching with flower tree flowering algorithm test instructions: Give a h*w figure, each point is ' o ' or ' * ', at least how many 1*2 rectangles to use to cover all the ' * ' in the graph. Limit: 1 <= h <= 40; 1 <= w <= 10 ideas: Minimum side coverage =| v|-Maximum matching general graph maximum match, with flower tree flowering algorithm */#include <cstdio> #include <cstring> #include <iostream> #include <queue >using namespace Std;const int max_v = 405;//and check set maintenance int fa[max_v];int GETFA (int x) {return fa[x] = = x? x:fa[x] = Get Fa (Fa[x]);} void merge (int a, int b) {a = GETFA (a); b = GETFA (b); if (A! = b) Fa[a] = b;} int V, match[max_v];vector<int> e[max_v];int Q[max_v], Rear;int Next[max_v], Mark[max_v], vis[MAX_V];// The naïve algorithm searches for a stage in the search tree two x, Y's nearest common ancestor rint LCA (int x, int y) {static int t = 0; T++;while (true) {if (x! =-1) {x = GETFA (x);//points to correspond to The corresponding flower goes up 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 (A! = p) {int b = match[a], c = next[b];//Next array is used to mark the path in the flower, the composite match array is used, actually formed a//doubly linked list, such as ( x, y) are matched, next[x] and Next[y] can refer to two directions. if (GETFA (c)! = P) Next[c] = b;//The points in the odd ring have the opportunity to find a match outside the ring, so all are marked with the S-type point added to the queue,//Because the number of matches in the ring is saturated, so the points are only allowed to match a successful point at most, in the//each match to a point break terminates the current phase of the search, and the next The new one, this is to make sure that. 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 i++ (int s) {for (int i = 0; i < V;)//Each phase is re-labeled next[i] =-1, fa[i] = i, mark[i] = 0, vis[i] = -1;mark[s ] = 1; Q[0] = s; Rear = 1; for (int front = 0; Match[s] = = 1 && front < rear; front++) {int x = Q[front];//The points in the queue Q are all S-type for (int i = 0; i < (int) e[x].size (); i++) {int y = e[x][i];if (match[x] = y) continue;//x matches y, ignoring if (GETFA (x) = = GETFA (y)) continue;//x with y in a flower, ignoring if (mark[ Y] = = 2) continue; Y is the T-point, ignoring if (mark[y] = = 1) {//Y is the S-type point, the odd-ring-indent int r = LCA (x, y);//R is the first common node on the path from I and J to S (GETFA (x) = r) Next[x] = y;//R And x is not the same flower, next marks the inner path if (GETFA (y) = r) Next[y] = x; R and Y are not in the same flower, next marks the inner path of the flower//will indent the entire R-X-y---R's odd ring, and r as the marker node of the ring, equivalent to the Super node group (x, R) in the paper; The indented path r---X is the point group (Y, R); The indented path r---y is the point}else if (Match[y] = =-1) {//Y free, can be augmented, R12 rule processing next[y] = x;for (int u = y; u! =-1;) {//cross-chain take inverse int v = next[u];int mv = Match[v];match[v] = u, match[u] = V;u = mv;} Break The search succeeds, the exit loop will go to the next stage}else {//Current search cross-chain +y+match[y] form a new cross-chain, will match[y] join the queue as the node to be searched next[y] = x;mark[q[rear++] = match[y]] = 1; /Match[y] is also the S-type mark[y] = 2; Y labeled T-}}}}bool g[max_v][max_v];const int N=45;char str[n][n];int mp[n][n];int dx[]={-1,1,0,0};int dy[]={0,0,-1,1}; int h,w;bool ok (int x,int y) {if (x>=0 && x
POJ 30,201-like graph maximum matching with flower tree flowering algorithm