• 图的m着色问题


    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #define NUM 110
    
    using namespace std;
    
    int n;  ///图的顶点数
    int m;  ///可用的颜色数量
    int c[NUM][NUM];    ///图的邻接矩阵
    int x[NUM];         ///当前的解向量
    int sum;    ///总方案数目
    int match;
    
    bool Same(int t)
    {
        int i;
        for(i=1; i<=n; i++)
            if(c[t][i]==1&&x[i]==x[t])
                return false;
        return true;
    }
    
    ///形参t是回溯的深度,从1开始
    void BackTrack(int t)
    {
        int i;
        if(t>n)
        {
            sum++;
            for(i=1; i<=n; i++)
                printf("%d ",x[i]);
            printf("
    ");
        }
        else
        {
            ///搜索当前扩展节点的m个孩子
            for(i=1; i<=m; i++)
            {
                x[t] = i;
                if(Same(t)) BackTrack(t+1);
                x[t] = 0;
            }
        }
    }
    
    int main()
    {
        m=4;
        int Cases;
        scanf("%d",&Cases);
        while(Cases--)
        {
            sum=0;
            for(int i=0;i<NUM;i++)
                for(int j=0;j<NUM;j++)
                    c[i][j] = 0;
    
            scanf("%d%d",&n,&match);
            for(int i=1; i<=match; i++)
            {
                int father,brother;
                scanf("%d%d",&father,&brother);
                c[father][brother]=c[brother][father]=1;
            }
            BackTrack(1);
            printf("%d
    ",sum);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    flask之闪现
    对于Flask中蓝图的理解
    flask中的CBV和FBV
    Flask之基本使用与配置
    Flask
    Flask-信号(blinker)
    flask-migrate
    Flask WTForms的使用和源码分析 —— (7)
    mac下卸载jdk
    RabbitMQ五种消息队列学习(三)–Work模式
  • 原文地址:https://www.cnblogs.com/TreeDream/p/5503383.html
Copyright © 2020-2023  润新知