图(三)着色问题

博客围绕图的着色问题展开,但具体内容缺失。图着色问题是信息技术领域的重要问题,在图论等方面有应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

//Ex8_1 图着色问题
#include<iostream>
#include<vector>
using namespace std;
const int MAX = 100;
struct Node{
	int color;
	vector<int> link;
	bool visited = false;
}node[MAX];
int nf_color = 3, nf_node = 5;
void print(){
	cout << "着色方案:";
	for (int i = 1; i <= nf_node; i++) 
		cout << (i ? " " : "") << node[i].color;
	cout << endl;
}
bool judge(const int& i, const int& color) {
	for (auto& it : node[i].link)
		if (node[it].color == color)
			return false;
	return true;
}
void dfs(int i) {	
	for (int j = 1; j <= nf_color; j++) {
		if (judge(i,j)) {
			node[i].color = j;
			if (i == nf_node)
				print();
			else
				dfs(i + 1);
			node[i].color = 0;
		}
	}
}
int main() {
	node[1].link.assign({2,4,5});
	node[2].link.assign({1,3,5});
	node[3].link.assign({2,4});
	node[4].link.assign({1,3,5});
	node[5].link.assign({1,2,4});
	dfs(1);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值