﻿图的m着色问题-Ocrosoft

# 图的m着色问题

```#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <ctime>
#include <string>
#include <cstdio>
#include <vector>
#include <cctype>
#include <climits>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
#define strend string::npos
#define ms(a) memset(a,0,sizeof(a))
#define rep(a,v,b) for(int a=v;a<b;a++)
#define repe(a,v,b) for(int a=v;a<=b;a++)
#define pre(a,v,b) for(int a=v;a>b;a--)
#define pree(a,v,b) for(int a=v;a>=b;a--)
#define lowbit(x) x&-x
typedef long long LL;
const LL LINF = LLONG_MAX / 2;
const int INF = INT_MAX / 2;
const int MAXN = 27 + 10;
const int MOD = 1000000007;
int gcd(int a, int b)
{
if (!b)return a;
return gcd(b, a%b);
}
/*(◕‿‿◕)(◕‿‿◕) (◕‿‿◕) (◕‿‿◕) (◕‿‿◕) (◕‿‿◕)*/
/*(◕‿‿◕) 签订契约,成为马猴烧酒吧 (◕‿‿◕)*/
/*(◕‿‿◕)(◕‿‿◕) (◕‿‿◕) (◕‿‿◕) (◕‿‿◕) (◕‿‿◕)*/
using namespace std;
int mp[100][100];
int color[100];
int n, m, ans;//n个点，m种颜色
int maxx = -1;
int minn = INF;
bool check(int p, int c)
{
rep(i, 0, n)
if (mp[p][i] && c == color[i])/*相邻点涂了想涂的颜色*/
return 0;
return 1;
}
void dfs(int p)
{
if (p >= n)
{
printf("%d:", ++ans);
rep(i, 0, n)printf("%d ", color[i]);
printf("(%d)\n", maxx + 1);
minn = min(maxx + 1, minn);
}
else
{
rep(i, 0, m)
if (check(p, i))//p点涂第j种颜色
{
int pmax = maxx;
if (i > maxx)maxx = i;
color[p] = i;
dfs(p + 1);
maxx = pmax;
color[p] = -1;
}
}
}
int main()
{
cin >> n >> m;/*input*/
ms(mp); memset(color, -1, sizeof(color)); ans = 0;/*init*/
rep(i, 0, n)rep(j, 0, n)cin >> mp[i][j];
dfs(0);
if (!ans)printf("无解！\n");
else printf("最少使用颜色种数：%d\n", minn);
return 0;
}
/*
5 3
0 1 1 0 0
1 0 1 0 1
1 1 0 0 1
0 0 0 0 1
0 1 1 1 0

3 3
0 0 0
0 0 0
0 0 0

3 0
0 1 1
1 0 1
1 1 0
*/```