《算法分析》作业12

1. 问题

图的m着色问题。给定无向连通图Gm种颜色,用这些颜色给图的顶点着色,每个顶点一种颜色。如果要求G的每条边的两个顶点着不同颜色。给出所有可能的着色方案;如果不存在,则回答“NO”。

2. 解析

 

3. 设计

 

核心代码:

bool check(int t) {//判断是否能往下涂色

    for (int i = 1; i <= n; ++i) {

        if (g[t][i] == 1 && col[i] == col[t])

            return false;

    }

    return true;

}

 

void dfs(int t) {

    if (t > n) {

        sum++;

        for (int i = 1; i <= n; ++i)

            printf("%d ", col[i]);

        printf("
");

    }

    else {

        for (int i = 1; i <= m; ++i) {

            col[t] = i;

            if (check(t))dfs(t + 1);

            col[t] = 0;

        }

    }

}

 

4. 分析

 

5. 源码

https://github.com/xiaojunjun601/sfHomework1/commit/a0503e317dcb736c72ef1f1f21fb0a8a11e33258

原文地址:https://www.cnblogs.com/zpj61/p/14832298.html