【圖染色】ZOJ 2066

來源:互聯網
上載者:User

很簡單,先根據每個人選擇情況建一個無向圖,然後標記染色,有相鄰邊的不能染成同種顏色!

原來標準時先對度排序,再染色。。。沒有排序都可以過,這種演算法叫Welch Powell演算法,自己加上度的排序吧~

用Welch Powell 演算法進行圖著色的步驟如下:

(1)將圖G的結點按照度數的遞減次序進行排列.(這種排列可能並不是唯一的,因為有些點有相同的度數).
(2)用第一種顏色對第一點進行著色,並且按排列次序,對與前面著色點不鄰接的每一點著上同樣的顏色.
(3)用第二種顏色對尚未著色的點重複(2),用第三種顏色繼續這種做法,直到所有的點全部著上色為止.

註:Welch Powell 演算法只能算出用最小几色,不能求出最大不矛盾點有哪幾個!

#include <vector>#include <list>#include <map>#include <set>#include <queue>#include <string.h>#include <deque>#include <stack>#include <algorithm>#include <iostream>#include <iomanip>#include <cstdio>#include <cmath>#include <cstdlib>#include <limits.h>#include <time.h>using namespace std;int lowbit(int t){return t&(-t);}int countbit(int t){return (t==0)?0:(1+countbit(t&(t-1)));}int gcd(int a,int b){return (b==0)?a:gcd(b,a%b);}#define LL long long#define PI acos(-1.0)#define N  201#define MAX INT_MAX#define MIN INT_MIN#define eps 1e-8#define FRE freopen("a.txt","r",stdin)struct node{    int color;    int du;}p[N];int n,m;int g[N][N];int ans;bool cmp(node a,node b){    return a.du>b.du;}void gao(){    int i,j,k,co;    for(i=1;i<=n;i++){        if(p[i].color)continue;        for(co=1;co<=n;co++){            for(j=1;j<=n;j++){                if(g[i][j] && co==p[j].color)break;            }            if(j>n)break;        }        p[i].color=co;        if(ans<co)ans=co;    }}int main(){    int t;    while(scanf("%d%d",&n,&m)!=EOF){        int i,j,k;        memset(g,0,sizeof(g));        for(i=0;i<m;i++){            int a,b;            scanf("%d%d",&a,&b);            g[a][b]=g[b][a]=1;        }        for(i=1;i<=n;i++){            p[i].du=0;            for(j=1;j<=n;j++){                if(g[i][j])                p[i].du++;            }            p[i].color=0;        }        sort(p+1,p+1+n,cmp);        ans=0;        gao();        printf("%d\n",ans);    }    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.