#if 1
#include<iostream>
using namespace std;
class Color {
friend int mColoring(int, int, int**);
private:
bool Ok(int k);//检查颜色可用性
void Backtrack(int t);
int n,//图的顶点数
m,//可用颜色数
** a,//邻接矩阵
* x;//当前解
long sum;//可行解个数
};
bool Color::Ok(int k) {
for (int j = 1; j <= n; j++) {
if ((a[k][j] == 1) && (x[j] == x[k]))
return false;
return true;
}
}
void Color::Backtrack(int t) {
if (t > n) {
sum++;
for (auto i = 1; i <= n; i++)
cout << x[i] << " ";
cout << '\n';
}
else
for (auto i = 1; i <= m; i++) {
x[t] = i;
if (Ok(t))Backtrack(t + 1);
x[t] = 0;
}
}
int mColoring(int n, int m, int** a) {
Color X;
X.n = n;
X.m = m;
X.a = a;
X.sum = 0;
int* p = new int[n + 1];
for (auto i = 0; i <= n; i++)
p[i] = 0;
X.x = p;
X.Backtrack(1);
delete[]p;
return X.sum;
}
#endif