首页 > 编程知识 正文

染色问题,数学染色问题

时间:2023-05-03 17:08:08 阅读:249155 作者:3933

1762: 染色
时间限制: 1 Sec 内存限制: 128 MB

题目描述
给定无向连通图G和m种不同的颜色,用这些颜色给图的各个顶点着一种颜色,若某种方案

使得图中每条边的2个顶点的颜色都不相同,则是一个满足的方案,找出所有的方案。

输入
第一行有3个正整数n(n<=100),k和m(m<3000),分别表示n个顶点,k条边,m种颜色

接下来k行,每行2个正整数,保送一条边的两个顶点

输出
所有不同的着色方案数

样例输入
5 6 3
1 2
1 3
2 3
2 4
2 5
4 5
样例输出
12
提示
来源
ac_code:
/*
每个点有m种颜色可选,对于一点,在选定一种颜色前 要判断与它成边的点染的颜色是否和它相同
*/

#include <iostream>#include <string.h>#include <vector>using namespace std;int n,k,m,ans;vector<int>edg[105];int color[3005];bool judge(int s){ for(int i = 0; i < edg[s].size(); i++) { if(color[edg[s][i]]==color[s]) return false; } return true;}void DFS(int s,int cnt){ if(cnt == n) { ans++; return; } for(int i = 1; i <= m; i++) { color[s] = i; if(judge(s)) DFS(s+1,cnt+1); color[s] = 0; }}int main(){ cin>>n>>k>>m; int x,y; for(int i = 0; i < k; i++) { cin>>x>>y; edg[x].push_back(y); edg[y].push_back(x); } DFS(1,0); cout<<ans<<endl; return 0;}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。