团体程序设计天梯赛 L2-023 图着色问题 (25分)

题目链接:

L2-023 图着色问题 (25分)

思路:

对于每种方案,我们遍历一遍所有点即可;

代码:

#include<bits/stdc++.h>

using namespace std;

const int maxv = 505;
int v, e, k, n;
vector<int> G[maxv];
int c[maxv], vst[maxv];
bool dfs(int u) {
	vst[u] = true;
	for(int & x : G[u]) {
		if(c[u] == c[x]) return false;
		if(!vst[x]) return dfs(x);
	}
	return true;
}

int main() {
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> v >> e >> k;
	for(int i = 0; i < e; i++) {
		int x, y;
		cin >> x >> y;
		G[x].push_back(y);
		G[y].push_back(x);	
	}
	cin >> n;
	while(n--) {
		set<int> st;
		for(int i = 1; i <= v; i++) {
			cin >> c[i];
			st.insert(c[i]);
			vst[i] = false;
		}
		bool res = st.size() == k;
		for(int i = 1; i <= v; i++) if(!vst[i]) res &= dfs(i);
		puts(res ? "Yes" : "No");
	}
	return 0;
}
发布了299 篇原创文章 · 获赞 8 · 访问量 7447

猜你喜欢

转载自blog.csdn.net/qq_45228537/article/details/104064840