poj3041-Asteroids , 二分圖的最小頂點覆蓋數 = 最大匹配數,最小頂點覆蓋

來源:互聯網
上載者:User

poj3041-Asteroids , 二分圖的最小頂點覆蓋數 = 最大匹配數,最小頂點覆蓋

點擊開啟連結

二分圖的最小頂點覆蓋數 = 二分圖的最大匹配數


題意: 在N*N的網路中有K顆小行星。小行星i的位置是(Ri, Ci)。現在有一個強力的武器能夠用一發光束將一整行或一整列的小行星消滅。想要利用這個武器消滅所有的小行星最少需要幾發光束?

分析: 以小行星的左右座標建立二分圖,就可以看出是求二分圖的最小頂點覆蓋數。


#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;const int maxn = 500 + 5; //單側頂點的最大數目struct BPM{    int n, m; //左右頂點個數    vector<int> G[maxn];  //鄰接表    int left[maxn];//left[i]為右邊第i個點的匹配點編號,-1表示不存在    bool T[maxn];//T[i]為右邊第i個點是否已標記    int right[maxn]; //求最小覆蓋用    bool S[maxn];    //求最小覆蓋用    void init(int n, int m){        this->n = n;        this->m = m;        for(int i=0; i<n; ++i) G[i].clear();    }    void AddEdge(int u, int v){        G[u].push_back(v);    }    bool match(int u){        S[u] = true;        for(int i=0; i<G[u].size(); ++i){            int v = G[u][i];            if(!T[v]){                T[v] = true;                if(left[v]==-1 || match(left[v])){                    left[v] = u;                    right[u] = v;                    return true;                }            }        }        return false;    }    //求最大匹配    int solve()    {        memset(left, -1, sizeof left );        memset(right, -1, sizeof right );        int ans = 0;        for(int u=0; u<n; ++u){            //從左邊結點u開始增廣            memset(S, 0, sizeof S );            memset(T, 0, sizeof T );            if(match(u)) ans++;        }        return ans;    }    //求最小覆蓋。 X 和 Y為最小覆蓋中的點集    int mincover(vector<int>& X, vector<int>& Y){        int ans = solve();        memset(S, 0, sizeof S );        for(int u =0; u<n; ++u)            if(right[u]==-1) match(u);            //從所有X未蓋點出發增廣        for(int u=0; u<n; ++u)            if(!S[u]) X.push_back(u); //X中的未標記點        for(int v=0; v<m; ++v)            if(T[v]) Y.push_back(v); //Y中的已標記點        return ans;    }};BPM  solver;int main(){    int i, j, n, k;    scanf("%d%d", &n, &k);    solver.init(n, n);    for(i=0; i<k; ++i)    {        int x, y;        scanf("%d%d", &x, &y);        x--; y--;        solver.AddEdge(x, y);  //有向圖    }    int ans = solver.solve();    printf("%d\n", ans);    return 0;}


 


最小頂點覆蓋==最大二分匹配證明?

比如最大匹配是M。為了求最少的點讓每條邊都至少和期中一個點關聯。
M個點是足夠的。就是說他們覆蓋最大匹配的那M條邊後,假設有某邊e沒被覆蓋,那麼把e加入後會得到一個更大的匹配,出現矛盾。
M個點是必需的。匹配的M條邊,由於他們兩兩無公用點,就是說至少有M個點才能把他們覆蓋。
參考資料:lrj黑書
 
圖論中無向圖解最小覆蓋,是通過最大匹配x,然後用頂點數n-x得到答案

這個DFS是找最大匹配的hungray演算法 ,你百度一下就知道了
 

聯繫我們

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