MCPcopy Create free account
hub / github.com/SuprDewd/CompetitiveProgramming / find_augmenting_path

Function find_augmenting_path

code/graph/blossom.cpp:4–72  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2bool marked[MAXV], emarked[MAXV][MAXV];
3int S[MAXV];
4vi find_augmenting_path(const vector<vi> &adj,const vi &m){
5 int n = size(adj), s = 0;
6 vi par(n,-1), height(n), root(n,-1), q, a, b;
7 memset(marked,0,sizeof(marked));
8 memset(emarked,0,sizeof(emarked));
9 rep(i,0,n) if (m[i] >= 0) emarked[i][m[i]] = true;
10 else root[i] = i, S[s++] = i;
11 while (s) {
12 int v = S[--s];
13 iter(wt,adj[v]) {
14 int w = *wt;
15 if (emarked[v][w]) continue;
16 if (root[w] == -1) {
17 int x = S[s++] = m[w];
18 par[w]=v, root[w]=root[v], height[w]=height[v]+1;
19 par[x]=w, root[x]=root[w], height[x]=height[w]+1;
20 } else if (height[w] % 2 == 0) {
21 if (root[v] != root[w]) {
22 while (v != -1) q.push_back(v), v = par[v];
23 reverse(q.begin(), q.end());
24 while (w != -1) q.push_back(w), w = par[w];
25 return q;
26 } else {
27 int c = v;
28 while (c != -1) a.push_back(c), c = par[c];
29 c = w;
30 while (c != -1) b.push_back(c), c = par[c];
31 while (!a.empty()&&!b.empty()&&a.back()==b.back())
32 c = a.back(), a.pop_back(), b.pop_back();
33 memset(marked,0,sizeof(marked));
34 fill(par.begin(), par.end(), 0);
35 iter(it,a) par[*it] = 1; iter(it,b) par[*it] = 1;
36 par[c] = s = 1;
37 rep(i,0,n) root[par[i] = par[i] ? 0 : s++] = i;
38 vector<vi> adj2(s);
39 rep(i,0,n) iter(it,adj[i]) {
40 if (par[*it] == 0) continue;
41 if (par[i] == 0) {
42 if (!marked[par[*it]]) {
43 adj2[par[i]].push_back(par[*it]);
44 adj2[par[*it]].push_back(par[i]);
45 marked[par[*it]] = true; }
46 } else adj2[par[i]].push_back(par[*it]); }
47 vi m2(s, -1);
48 if (m[c] != -1) m2[m2[par[m[c]]] = 0] = par[m[c]];
49 rep(i,0,n) if(par[i]!=0&&m[i]!=-1&&par[m[i]]!=0)
50 m2[par[i]] = par[m[i]];
51 vi p = find_augmenting_path(adj2, m2);
52 int t = 0;
53 while (t < size(p) && p[t]) t++;
54 if (t == size(p)) {
55 rep(i,0,size(p)) p[i] = root[p[i]];
56 return p; }
57 if (!p[0] || (m[c] != -1 && p[t+1] != par[m[c]]))
58 reverse(p.begin(), p.end()), t=(int)size(p)-t-1;
59 rep(i,0,t) q.push_back(root[p[i]]);
60 iter(it,adj[root[p[t-1]]]) {
61 if (par[*it] != (s = 0)) continue;

Callers 2

iterFunction · 0.85
max_matchingFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected