hdu 4687 帶花樹求一般圖最大匹配(模板)

來源:互聯網
上載者:User

題意 :  給一個一般圖(無向圖),求所有的最大匹配情況不包含的邊 ,輸出這些沒有匹配的點 ;


     資料比較小,直接枚舉邊。先求一次最大匹配hig,然後依次枚舉所有邊,假設此邊為一個匹配,那麼刪掉邊的兩個節點,然後再剩下的圖中求最大匹配t,

如果t == hig-1那麼刪去的這條邊無疑使最大匹配裡的邊了, 如果t<hig-1 那麼就是不包含的這邊了。

  注意 : 枚舉刪去的這條邊,所有與該邊相鄰的邊也要刪去,因為我們假設刪的邊是匹配邊,顯然相鄰的邊就不能要了 ;


關於一般圖上的最大匹配演算法,O(n^3)的Edmonds's matching algorithm,理解起來比較容易,但是寫起來比較麻煩,收集了一個模板,


#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>using namespace std;const int MAXN = 50;int N; //點的個數,點的編號從1到Nbool Graph[MAXN][MAXN];int Match[MAXN];bool InQueue[MAXN],InPath[MAXN],InBlossom[MAXN];int Head,Tail;int Queue[MAXN];int Start,Finish;int NewBase;int Father[MAXN],Base[MAXN];int Count;void Push(int u){    Queue[Tail] = u;    Tail++;    InQueue[u] = true;}int Pop(){    int res = Queue[Head];    Head++;    return res;}int FindCommonAncestor(int u,int v){    memset(InPath,false,sizeof(InPath));    while(true)    {        u = Base[u];        InPath[u] = true;        if(u == Start) break;        u = Father[Match[u]];    }    while(true)    {        v = Base[v];        if(InPath[v])break;        v = Father[Match[v]];    }    return v;}void ResetTrace(int u){    int v;    while(Base[u] != NewBase)    {        v = Match[u];        InBlossom[Base[u]] = InBlossom[Base[v]] = true;        u = Father[v];        if(Base[u] != NewBase) Father[u] = v;    }}void BloosomContract(int u,int v){    NewBase = FindCommonAncestor(u,v);    memset(InBlossom,false,sizeof(InBlossom));    ResetTrace(u);    ResetTrace(v);    if(Base[u] != NewBase) Father[u] = v;    if(Base[v] != NewBase) Father[v] = u;    for(int tu = 1; tu <= N; tu++)        if(InBlossom[Base[tu]])        {            Base[tu] = NewBase;            if(!InQueue[tu]) Push(tu);        }}void FindAugmentingPath(){    memset(InQueue,false,sizeof(InQueue));    memset(Father,0,sizeof(Father));    for(int i = 1;i <= N;i++)        Base[i] = i;    Head = Tail = 1;    Push(Start);    Finish = 0;    while(Head < Tail)    {        int u = Pop();        for(int v = 1; v <= N; v++)            if(Graph[u][v] && (Base[u] != Base[v]) && (Match[u] != v))            {                if((v == Start) || ((Match[v] > 0) && Father[Match[v]] > 0))                    BloosomContract(u,v);                else if(Father[v] == 0)                {                    Father[v] = u;                    if(Match[v] > 0)                        Push(Match[v]);                    else                    {                        Finish = v;                        return;                    }                }            }    }}void AugmentPath(){    int u,v,w;    u = Finish;    while(u > 0)    {        v = Father[u];        w = Match[v];        Match[v] = u;        Match[u] = v;        u = w;    }}void Edmonds(){    memset(Match,0,sizeof(Match));    for(int u = 1; u <= N; u++)        if(Match[u] == 0)        {            Start = u;            FindAugmentingPath();            if(Finish > 0)AugmentPath();        }}int getMatch(){    Edmonds();    Count = 0;    for(int u = 1; u <= N;u++)        if(Match[u] > 0)            Count++;    return Count/2;  //無向邊,所以匹配點數除2 ; }bool g[MAXN][MAXN];pair<int,int>p[150];int main(){    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    int m;    while(scanf("%d%d",&N,&m)==2)    {        memset(g,false,sizeof(g));        memset(Graph,false,sizeof(Graph));        int u,v;        for(int i = 1;i <= m;i++)        {            scanf("%d%d",&u,&v);            p[i] = make_pair(u,v);            g[u][v] = true;            g[v][u] = true;            Graph[u][v] = true;            Graph[v][u] = true;        }        int cnt0 = getMatch(); //最大匹配         //cout<<cnt0<<endl;        vector<int>ans;        for(int i = 1;i <= m;i++)  //枚舉刪點的邊         {            u = p[i].first;            v = p[i].second;            memcpy(Graph,g,sizeof(g));            for(int j = 1;j <= N;j++)  //相鄰的邊不要;                 Graph[j][u] = Graph[u][j] = Graph[j][v] = Graph[v][j] = false;            int cnt = getMatch();            //cout<<cnt<<endl;            if(cnt < cnt0-1)                  ans.push_back(i);        }        int sz = ans.size();        printf("%d\n",sz);        for(int i = 0;i < sz;i++)        {            printf("%d",ans[i]);            if(i < sz-1)printf(" ");        }        printf("\n");    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.