Guest User

Untitled

a guest
May 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.32 KB | None | 0 0
  1. struct MaxMatching
  2. {
  3.     int n,match[MAXN];
  4.     bool g[MAXN][MAXN];
  5.     int idx[MAXN][MAXN];
  6.     int q[MAXN],prev[MAXN],father[MAXN];
  7.     bool visited[MAXN],is_shrinked[MAXN];
  8.     bool is_in_path[MAXN];
  9.  
  10.     void clear()
  11.     {
  12.         for (int i=0;i<n;i++) match[i]=-1;
  13.     }
  14.     void init(int n)
  15.     {
  16.         this->n=n;
  17.         memset(g,false,sizeof(g));
  18.         clear();
  19.     }
  20.     void addedge(int u,int v,int d)
  21.     {
  22.         g[u][v]=g[v][u]=true;
  23.         idx[u][v]=idx[v][u]=d;
  24.     }
  25.     int ford()
  26.     {
  27.         int r=0;
  28.         for (int i=0;i<n;i++) for (int j=0;j<n;j++) if (match[i]<0 && match[j]<0 && g[i][j]) match[i]=j,match[j]=i,r++;
  29.         for (int i=0;i<n;i++) if (match[i]<0 && bfs(i)) r++;
  30.         return r;
  31.     }
  32.     bool bfs(int src)
  33.     {
  34.         for (int i=0;i<n;i++) prev[i]=-1;
  35.         for (int i=0;i<n;i++) visited[i]=false;
  36.         for (int i=0;i<n;i++) father[i]=i;
  37.         int op=0;
  38.         q[op++]=src;
  39.         visited[src]=true;
  40.         for (int cl=0;cl<op;cl++) for (int key=q[cl],other=0;other<n;other++)
  41.             if (g[key][other] && father[key]!=father[other] && other!=match[key])
  42.                 if (other==src || (match[other]>=0 && prev[match[other]]>=0))
  43.                 {
  44.                     int next=contract(key,other);
  45.                     for (int i=0;i<n;i++) if (is_shrinked[father[i]])
  46.                     {
  47.                         father[i]=next;
  48.                         if (!visited[i]) visited[i]=true,q[op++]=i;
  49.                     }
  50.                 }
  51.                 else if (prev[other]<0)
  52.                 {
  53.                     prev[other]=key;
  54.                     if (match[other]<0)
  55.                     {
  56.                         expand(other);
  57.                         return true;
  58.                     }
  59.                     else
  60.                     {
  61.                         q[op++]=match[other];
  62.                         visited[match[other]]=true;
  63.                     }
  64.                 }
  65.         return false;
  66.     }
  67.     void expand(int v)
  68.     {
  69.         while (v>=0)
  70.         {
  71.             int p=prev[v];
  72.             int k=match[p];
  73.             match[v]=p;
  74.             match[p]=v;
  75.             v=k;
  76.         }
  77.     }
  78.     void change_blossom(int b,int u)
  79.     {
  80.         while (father[u]!=b)
  81.         {
  82.             int v=match[u];
  83.             is_shrinked[father[v]]=is_shrinked[father[u]]=true;
  84.             u=prev[v];
  85.             if (father[u]!=b) prev[u]=v;
  86.         }
  87.     }
  88.     int contract(int u,int v)
  89.     {
  90.         memset(is_shrinked,false,sizeof(is_shrinked));
  91.         int key=get_father(father[u],father[v]);
  92.         change_blossom(key,u);
  93.         change_blossom(key,v);
  94.         if (father[u]!=key) prev[u]=v;
  95.         if (father[v]!=key) prev[v]=u;
  96.         return key;
  97.     }
  98.     int get_father(int u,int v)
  99.     {
  100.         for (int i=0;i<n;i++) is_in_path[i]=false;
  101.         while (1)
  102.         {
  103.             is_in_path[u]=true;
  104.             if (match[u]<0) break;
  105.             u=father[prev[match[u]]];
  106.         }
  107.         for (;!is_in_path[v];v=father[prev[match[v]]]);
  108.         return v;
  109.     }
  110. };
Add Comment
Please, Sign In to add comment