二分圖匹配的H-K演算法

來源:互聯網
上載者:User

        演算法的具體描述請看http://chenhaifeng.blog.edu.cn/2007/91117.html,我只是來貼代碼的。

        http://blog.csdn.net/emoizhang/article/details/8004502這裡講的比我詳細(雖然都是廢話),但他的代碼比我的慢。

【題目描述】

給一個有向非循環圖,求最小鏈覆蓋

【輸入格式】

第一行兩個數n、m,分別表示地圖的點數和邊數,接下來m行,每行兩個數u、v,表示有一條從點u到點v的有向邊。

【資料範圍】

n<=200000,m<=500000

【題解】

將每個點拆成兩個點,求最大匹配,答案即為點數減去最大匹配數。由於資料很大,所以需要使用HK演算法。
Code:

program maze;type        int=longint;var        i,j,k,m,n,x,y,tot,ans,l,r:int;        ne,t:array[1..500100]of int;        h,link,d,q:array[1..401000]of int;        visited,v:array[1..201000]of boolean;procedure prepare;begin        for i:=1 to n do begin                j:=h[i];                while j<>0 do begin                        k:=t[j];                        if link[k]=0 then begin                                link[k]:=i;inc(ans);                                v[i]:=true;break;                        end;                        j:=ne[j];                end;        end;end;function bfs:boolean;begin        fillchar(d,sizeof(d),$FF);        l:=0;r:=0;bfs:=false;        for i:=1 to n do if not v[i]then begin                d[i]:=0;inc(r);q[r]:=i;        end;        if l=r then exit;        repeat                inc(l);i:=q[l];j:=h[i];                while j<>0 do begin                        k:=t[j];m:=link[k];                        if m=0 then bfs:=true                        else if d[m]=-1 then begin                                d[m]:=d[i]+1;inc(r);q[r]:=m;                        end;                        j:=ne[j];                end;        until l=r;end;function dfs(x:int):boolean;var j,k:int;begin        j:=h[x];        while j<>0 do begin                k:=t[j];                if(link[k]=0)or((d[link[k]]=d[x]+1)and(dfs(link[k])))then begin                        link[k]:=x;exit(true);                end;                j:=ne[j];        end;        exit(false);end;begin        assign(input,'maze.in');reset(input);        assign(output,'maze.out');rewrite(output);        read(n,m);        for i:=1 to m do begin                read(x,y);                inc(tot);t[tot]:=y+n;                ne[tot]:=h[x];h[x]:=tot;        end;        ans:=0;        prepare;        while bfs do                for i:=1 to n do if(not v[i])and(dfs(i))then begin                        v[i]:=true;inc(ans);                end;        write(n-ans);        close(input);close(output);end.

在具體的實現時,我先貪心了一個初始匹配(prepare),稍微加快了一點速度。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.