zoukankan      html  css  js  c++  java
  • 一般图的最大匹配-带花树算法

    UOJ79模板
    here

    #include<cstdio>
    #include<cctype>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int read() {
        int x=0,f=1;
        char c=getchar();
        for (;!isdigit(c);c=getchar()) if (c=='-') f=-1;
        for (;isdigit(c);c=getchar()) x=x*10+c-'0';
        return x*f;
    }
    const int maxn=505;
    const int maxm=maxn*maxn*2;
    int n,m,que[maxm],ql,qr,pre[maxn],tim=0;
    struct edge {
        int v,nxt;
    } e[maxm];
    int h[maxn],tot=0;
    int match[maxn],f[maxn],tp[maxn],tic[maxn];
    int find(int x) {
        return f[x]==x?f[x]:f[x]=find(f[x]);
    }
    void add(int u,int v) {
        e[++tot]=(edge){v,h[u]};
        h[u]=tot;
    }
    int lca(int x,int y) {
        for (++tim;;swap(x,y)) if (x) {
            x=find(x);
            if (tic[x]==tim) return x; else tic[x]=tim,x=pre[match[x]];
        }
    }
    void shrink(int x,int y,int p) {
        while (find(x)!=p) {
            pre[x]=y,y=match[x];
            if (tp[y]==2) tp[y]=1,que[++qr]=y;
            if (find(x)==x) f[x]=p;
            if (find(y)==y) f[y]=p;
            x=pre[y];
        }
    }
    bool aug(int s) {
        for (int i=1;i<=n;++i) f[i]=i;
        memset(tp,0,sizeof tp),memset(pre,0,sizeof pre);
        tp[que[ql=qr=1]=s]=1; // 1: type A ; 2: type B
        int t=0;
        while (ql<=qr) {
            int x=que[ql++];
            for (int i=h[x],v=e[i].v;i;i=e[i].nxt,v=e[i].v) {
                if (find(v)==find(x) || tp[v]==2) continue; 
                if (!tp[v]) {
                    tp[v]=2,pre[v]=x;
                    if (!match[v]) {
                        for (int now=v,last,tmp;now;now=last) {
                            last=match[tmp=pre[now]];
                            match[now]=tmp,match[tmp]=now;
                        }
                        return true;
                    } 
                    tp[match[v]]=1,que[++qr]=match[v];
                } else if (tp[v]==1) {
                    int l=lca(x,v);
                    shrink(x,v,l);
                    shrink(v,x,l);
                }
            }
        }   
        return false;
    }
    int main() {
        n=read(),m=read();
        for (int i=1;i<=m;++i) {
            int x=read(),y=read();
            add(x,y),add(y,x);
        }
        int ans=0;
        for (int i=1;i<=n;++i) ans+=(!match[i] && aug(i));
        printf("%d
    ",ans);
        for (int i=1;i<=n;++i) printf("%d ",match[i]);
        puts("");
        return 0;
    }
    

    TP

  • 相关阅读:
    gimp 语言设置
    gnome 3.4 评测
    linux 添加PATH路径
    永中office2012 linux版 使用初步感受
    Bio per常规用法(1、读取序列)
    blast程序 介绍 简介
    genewise运行过程中遇到的错误及其解决方法
    gnome3 隐藏标题栏
    Bio per常规用法(2、翻译蛋白)
    genewise 编译过程中遇到的 getline冲突
  • 原文地址:https://www.cnblogs.com/Cwolf9/p/9513281.html
Copyright © 2011-2022 走看看