POJ 3715 Blue and Red 二分圖,poj3715

來源:互聯網
上載者:User

POJ 3715 Blue and Red 二分圖,poj3715

說是有一個軍事演習

n個士兵,其中有m個關係表示某兩個人是好友

現在士兵已經分好了兩組了,用來進行對抗,但是這兩組之間可能有好友,會影響軍事演習的情況

所以要去掉盡量少的人,使得這個兩組之間沒有好友。


那麼題目給了一個分組方案了,  但是不同組之間可能有好友,

我們就要在這些個不同組的好友之間  連邊然後求二分圖最大匹配,

求出來的結果就是要去掉的人數

但是題目又要求字典序要最小。


那我們就從序號小的開始枚舉,  類比刪除掉該人, 然後求二分圖最大匹配看有沒有變化, 如果有變化說明這個人必須去掉


#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <cmath>#include <algorithm>#include <map>#include <ctime>#define MAXN 222#define MAXM 6122222#define INF 1000000001using namespace std;int n, m;vector<int>g[MAXN], ans;int mark[MAXN], used[MAXN], cx[MAXN], cy[MAXN], a[MAXN];int path(int u){    int sz = g[u].size();    for(int i = 0; i < sz; i++)    {        int v = g[u][i];        if(!mark[v] && !used[v])        {            mark[v] = 1;            if(cy[v] == -1 || path(cy[v]))            {                cx[u] = v;                cy[v] = u;                return 1;            }        }    }    return 0;}int gao(){    int ans = 0;    memset(cy, -1, sizeof(cy));    for(int i = 1; i <= n; i++)    {        memset(mark, 0, sizeof(mark));        if(a[i] || used[i]) continue;        ans += path(i);    }    return ans;}int main() {    int T;    scanf("%d", &T);    while(T--) {        scanf("%d%d", &n, &m);        for(int i = 1; i <= n; i++) g[i].clear();        for(int i = 1; i <= n; i++) scanf("%d", &a[i]);        int u, v;        for(int i = 1; i <= m; i++) {            scanf("%d%d", &u, &v);            u++, v++;            if(a[u] != a[v]) {                if(a[u] == 0) {                    g[u].push_back(v);                } else {                    g[v].push_back(u);                }            }        }        memset(used, 0, sizeof(used));        ans.clear();        int d = gao();        for(int i = 1; i <= n; i++) {            used[i] = 1;            int z = gao();            if(z < d) {                ans.push_back(i);                d = z;            } else used[i] = 0;        }        printf("%d", ans.size());        for(int i = 0; i < ans.size(); i++) printf(" %d", ans[i] - 1);        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.