Problem1510--【搜索基础】图的m着色问题1510: 【搜索基础】图的m着色问题
Time Limit: 2.000 Sec Memory Limit: 128 MB
Submit: 365 Solved: 144
[Submit] [Status] [Web Board] [Creator:]Description
图的m着色问题 color.pas
【问题描述】
给定无向连通图G和m种不同的颜色,用这些颜色给图的各个顶点着一种颜色,若某种方案使得图中每条边的2个顶点的颜色都不相同,则是一个满足的方案,找出所有的方案。
【输入格式】
第一行有3个正整数n,k和m,分别表示n个顶点,k条边,m种颜色
接下来k行,每行2个正整数,保送一条边的两个顶点
【输出格式】
所有不同的着色方案数
【输入样例】
5 8 4
1 2
1 3
1 4
2 3
2 4
2 5
3 4
4 5
【输出样例】
48
Sample Input
Sample Output
HINT
#include<bits/stdc++.h>
using namespace std;
int n,k,m,a[999][999],x1,x2,s=0,c[999999]={0};
void dfs(int x){
if(x>n){
s++;
return;
}
else
for(int i=1;i<=m;i++){
int f=0;
for(int j=1;j<=x;j++)
if(a[j][x]==1&&c[j]==i)
f=1;
if(f==0){
c[x]=i;
dfs(x+1);
c[x]=0;
}
else
f=0;
}
}
int main(){
cin>>n>>k>>m;
for(int i=1;i<=k;i++){
cin>>x1>>x2;
a[x1][x2]=1;
a[x2][x1]=1;
}
dfs(1);
cout<<s<<" ";
return 0;
}
Source/Category
[Submit] [Status]