洛谷P2819 图的m着色问题
2017-07-11 19:47
344 查看
题目链接:
https://www.luogu.org/problem/show?pid=2819
思路:
要求每条边的两个定点着不同颜色,可以按照点来dfs,如果与之相连的点已染上一种颜色,那当前点就不染了
https://www.luogu.org/problem/show?pid=2819
思路:
要求每条边的两个定点着不同颜色,可以按照点来dfs,如果与之相连的点已染上一种颜色,那当前点就不染了
#include<iostream> #include<cstdio> using namespace std; int n,k,m,g[105][105],b[205],ans,x,y; bool check(int x,int y){ for (int i=1;i<=n;i++) if (g[y][i]&&b[i]==x) return 0;//与之相连的点染了颜色 return 1; } void dfs(int x){ if (x>n){//所有点都已染色,方案数+1 ans++;return ; } for (int i=1;i<=m;i++)//m种颜色 if (b[x]==0&&check(i,x)==1){//当前没染色并且与之相连的点没有连第i个颜色 b[x]=i;//当前然i种颜色 dfs(x+1); b[x]=0; } } int main(){ cin>>n>>k>>m; for (int i=1;i<=k;i++){ scanf("%d%d",&x,&y); g[x][y]=1;g[y][x]=1; } dfs(1); cout<<ans; return 0; }
相关文章推荐
- P2819 图的m着色问题 洛谷
- 回溯法之k着色问题
- 图的着色问题
- 贪心算法求解 图的m着色问题
- Graph Coloring 图着色问题
- 洛谷P1984 [SDOI2008]烧水问题
- 算法导论16.1-3 区间图着色(interval-graph coloring)问题(贪心算法)
- 洛谷P1602 Sramoc问题
- 区间图着色问题(贪心算法)C++实现
- 回溯法---->图的着色问题
- 着色问题 一个圆分成N个扇形,M种颜色,相邻区块重色 总工的着色方法数
- Android05--Android之Drawable的着色兼容问题
- 高效率安排面试(图着色问题扩展)
- 区间图着色问题(贪心算法)C++实现
- 考题一:研究对全排列着色的性质 问题 A: 首先需要生成 n 的全排列然后对 n 的全排列进行着色, 使得相邻的两个数只需用最少颜色就可以把相邻的两个数用那区分开. (这里相邻包含两层含义:同时在自然
- 图的m着色问题
- 三着色问题 递归和迭代方法 打印着色方案
- 解决Sublime Text 3中文显示乱码和语法着色问题 等问题
- L2-023 图着色问题
- HDU 2819 Swap(最大匹配问题)