图论知识点记录

二分图相关:

博客1:http://dingdongsheng.cool.blog.163.com/blog/static/1186187552009431405995/

博客2:https://www.byvoid.com/blog/match-km


最小生成树的计数问题

博客1:http://hi.baidu.com/dispossessed/item/06be982f3e27109b9d63d150

博客2:http://www.cnblogs.com/reflec94/archive/2011/04/25/2028591.html

博客3:http://www.cnblogs.com/Fatedayt/archive/2012/05/10/2494877.html

博客4:http://blog.csdn.net/jarily/article/details/8902402

博客5(生成树计数):http://www.cnblogs.com/saltless/archive/2010/11/06/1870778.html


差分约束系统

博客1:http://ycool.com/post/m2uybbf


前k短路径问题

博客1:http://ycool.com/post/krb8pah


带花树算法:

博客1:http://fanhq666.blog.163.com/blog/static/8194342620120304463580/

博客2:http://hi.baidu.com/cloudygoose/item/1ccf42a1c678d0d85af19178

模板摘自:http://twinsclover.is-programmer.com/posts/21598.html

代码(hdu 4687):

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;

#define MAXN 45     //点集的大小
#define SET(a,b) memset(a,b,sizeof(a))
deque<int> Q;
//g[i][j]存放关系图:i,j是否有边,match[i]存放i所匹配的点
bool g[MAXN][MAXN],inque[MAXN],inblossom[MAXN];
int match[MAXN],pre[MAXN],base[MAXN];

//找公共祖先
int findancestor(int u,int v)
{
    bool inpath[MAXN]={false};
    while(1)
    {
        u=base[u];
        inpath[u]=true;
        if(match[u]==-1)break;
        u=pre[match[u]];
    }
    while(1)
    {
        v=base[v];
        if(inpath[v])return v;
        v=pre[match[v]];
    }
}

//压缩花
void reset(int u,int anc)
{
    while(u!=anc)
    {
        int v=match[u];
        inblossom[base[u]]=1;
        inblossom[base[v]]=1;
        v=pre[v];
        if(base[v]!=anc)pre[v]=match[u];
        u=v;
    }
}

void contract(int u,int v,int n)
{
    int anc=findancestor(u,v);
    SET(inblossom,0);
    reset(u,anc);reset(v,anc);
    if(base[u]!=anc)pre[u]=v;
    if(base[v]!=anc)pre[v]=u;
    for(int i=1;i<=n;i++)
        if(inblossom[base[i]])
        {
            base[i]=anc;
            if(!inque[i])
            {
                Q.push_back(i);
                inque[i]=1;
            }
        }
}

bool dfs(int S,int n)
{
    for(int i=0;i<=n;i++)pre[i]=-1,inque[i]=0,base[i]=i;
    Q.clear();Q.push_back(S);inque[S]=1;
    while(!Q.empty())
    {
        int u=Q.front();Q.pop_front();
        for(int v=1;v<=n;v++)
        {
            if(g[u][v]&&base[v]!=base[u]&&match[u]!=v)
            {
                if(v==S||(match[v]!=-1&&pre[match[v]]!=-1))contract(u,v,n);
                else if(pre[v]==-1)
                {
                    pre[v]=u;
                    if(match[v]!=-1)Q.push_back(match[v]),inque[match[v]]=1;
                    else
                    {
                        u=v;
                        while(u!=-1)
                        {
                            v=pre[u];
                            int w=match[v];
                            match[u]=v;
                            match[v]=u;
                            u=w;
                        }
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
int solve(int n)
{
    SET(match,-1);
    int ans=0;
    for(int i=1;i<=n;i++){
        if(match[i]==-1&&dfs(i,n))
            ans++;
    }
    return ans;
}
void init(){ memset(g,false,sizeof(g));}

int E[150][2];
bool mp[MAXN][MAXN];
int main(){
    int n,m,i,j,a,b,t;
    while(scanf("%d%d",&n,&m)!=EOF){
        memset(mp,false,sizeof(mp));
        for(i=1;i<=m;i++){
            scanf("%d%d",&a,&b);
            E[i][0]=a;E[i][1]=b;
            mp[a][b]=mp[b][a]=true;
        }
        memcpy(g,mp,sizeof(mp));
        t=solve(n);
        vector<int> ans;
        for(i=1;i<=m;i++){
            memcpy(g,mp,sizeof(mp));
            for(j=1;j<=n;j++) g[j][E[i][0]]=g[j][E[i][1]]=false;
            for(j=1;j<=n;j++) g[E[i][0]][j]=g[E[i][1]][j]=false;
            a=solve(n);
            if(a<t-1) ans.push_back(i);
        }
        printf("%d\n",ans.size());
        if(ans.size()){
            for(i=0;i<ans.size()-1;i++) printf("%d ",ans[i]);
            printf("%d",ans[i]);
        }printf("\n");
    }
    return 0;
}

你可能感兴趣的:(图论知识点记录)