【POJ 1419】Graph Coloring

來源:互聯網
上載者:User

【POJ 1419】Graph Coloring

求圖的最大獨立集 最大獨立集=補圖最大團
很適合理解最大團/最大獨立集的模板題
建立補圖套模板既可 需要輸出集合點 原本想用stack 但發現copy比較麻煩 vector用一個iterator指標 迴圈很便利

代碼如下:

#include <iostream>#include <cstdlib>#include <cstdio>#include <cstring>#include <vector>using namespace std;bool mp[111][111];int link[111][111],dp[111],mx;vector <int> s,ss;void dfs(int n,int num,int step){    int i,j,k,cnt;    if(num == 0)    {        if(mx < step)        {            s.clear();            vector<int>::iterator it = ss.begin();            for(;it != ss.end(); ++it)                s.push_back(*it);            mx = step;        }        return;    }    for(i = 0; i < num; ++i)    {        cnt = 0;        k = link[step][i];        if(step + n - k <= mx) return;        if(step + dp[k] <= mx) return;        for(j = i+1; j < num; ++j) if(mp[link[step][j]][k]) link[step+1][cnt++] = link[step][j];        ss.push_back(k);        dfs(n,cnt,step+1);        ss.pop_back();    }}int GetMax(int n){    int i,j,sz;    mx = 0;    for(i = n; i >= 1; --i)    {        sz = 0;        for(j = i+1; j <= n; ++j) if(mp[i][j]) link[1][sz++] = j;        ss.push_back(i);        dfs(n,sz,1);        ss.pop_back();        dp[i] = mx;    }    return mx;}int main(){    int t,n,m,u,v,i;    scanf("%d",&t);    while(t--)    {        memset(mp,true,sizeof(mp));        scanf("%d %d",&n,&m);        while(m--)        {            scanf("%d %d",&u,&v);            mp[u][v] = mp[v][u] = false;        }        for(i = 1; i <= n; ++i) mp[i][i] = false;        printf("%d\n",GetMax(n));        vector <int> ::iterator it = s.begin();        for(; it+1 != s.end(); ++it)            printf("%d ",*it);        printf("%d\n",*it);    }    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.