1575.图的m着色问题
时限:1000ms 内存限制:10000K 总时限:3000ms
描述
给定无向连通图G和m种不同的颜色。用这些颜色为图G的各顶点着色,每个顶点着一种颜色。如果有一种着色法使G中每条边的2个顶点着不同颜色,则称这个图是m可着色的。图的m着色问题是对于给定图G和m种颜色,找出所有不同的着色法。
输入
第1行有3个正整数n,r 和m(n < 20,r < 200,m < 10),表示给定的图G有n个顶点和r条边,m种颜色。顶点编号为0,1,2,…,n-1。接下来的r行中,每行有2个正整数u,v,表示图G 的一条边(u,v)。
输出
输出不同的着色方案的总数。
输入样例
3 2 2
0 1
1 2
输出样例
2
#include<stdio.h>
#include<stdlib.h>
#define max 21
int a[max][max];
int n,m,r;
int count; //number of the answer
int x[max]; //color of the vertex
bool notsame(int i)
{
for(int j=1;j<=n;j++)
if(a[i][j]==1 && x[i]==x[j])
{
return false;
}
return true;
}
void dfs(int i)
{
if(i>n)
{
count++;
}
else
{
for(int j=1;j<=m;j++)
{
x[i]=j;
if(notsame(i))
{
dfs(i+1);
}
x[i]=0;
}
}
}
int main()
{
int x,y;
scanf("%d%d%d",&n,&r,&m);
for(int i=1;i<=r;i++)
{
scanf("%d%d",&x,&y);
a[x+1][y+1]=1;
a[y+1][x+1]=1;
}
dfs(1);
if(count>0)
{
printf("%d\n",count);
}
else
{
printf("-1\n");
}
return 0;
}