#include<iostream>
#include<vector>
using namespace std;
int n;
vector<int> g[10010];
int V;
int color[10010];
bool dfs(int v,int c){
color[v]=c;
for(int i=0;i<g[v].size();i++){
if(color[g[v][i]]==c) return false;
else if(color[g[v][i]==0]&&!dfs(g[v][i],-c)){
return false;
}
}
return true;
}
void solve(){
for(int i=0;i<V;i++){
if(color[i]==0) {
if(!dfs(1, 1)){
cout<<"NO"<<endl;
return;
}
}
}
cout<<"YES"<<endl;
}
算法入门 14.染色问题(dfs+图)
最新推荐文章于 2022-10-15 19:20:41 发布
本文介绍了一种使用深度优先搜索(DFS)和图着色技术来判断图中是否存在颜色冲突的算法。通过递归遍历并检查节点间的染色规则,判断是否可以为图中的所有节点分配不同颜色,以确保它们不会相邻。若无法完成着色,则输出'NO';成功则输出'YES'。
摘要由CSDN通过智能技术生成