图的m着色

图的m着色

#include <bits/stdc++.h>
using namespace std;
int n, k, m, ans;

struct node{
    int m, color;
    vector<int> arr;
};

node b[105];

bool p(int a, int w)
{
    vector<int> :: iterator it;
    for(it = b[a].arr.begin(); it != b[a].arr.end(); it++)
        if(b[*it].color == w) return 0;
    return 1;
}

int dfs(int j)
{
    if(j > n){
        ans++;
        return 0;
    }
    for(int i = 1; i <= m; i++){
        if(!p(j, i)) continue;
        b[j].color = i;
        dfs(j +1);
        b[j].color = 0;
    }
}

int main()
{
    int u, v;
    cin>>n>>k>>m;
    for(int i = 1; i <= k; i++){
        cin>>u>>v;
        b[u].m++, b[v].m++;
        b[u].arr.push_back(v), b[v].arr.push_back(u);
    }
    dfs(1);
    cout<<ans<<endl;
    return 0;
}
posted @ 2018-07-13 14:27  wbss  阅读(145)  评论(0编辑  收藏  举报