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

在这里插入图片描述

6 8 3
2 1
1 3
4 6
2 5
2 4
5 4
5 6
3 6
4
1 2 3 3 1 2
4 5 6 6 4 5
1 2 3 4 5 6
2 3 4 2 3 4

输出样例:

Yes
Yes
No
No

啊 这题 一句话就是看连通的两端点颜色是否相同
是否联通 也就是是否可达 无向图存储然后设置初始值就完事了
颜色就另开一个数组呗对应着端点做为下标来存

唯一注意的点就是每次输入测试组测试完的时候set要清除一下要不对下一组的测试会产生影响

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <vector>
#include <algorithm>
#include <queue>
#include <set>
#include <memory>
using namespace std;
const int INF = 1e+9;
const int maxn = 550;
int n;
int v,e,k;
int G[maxn][maxn],color[maxn];

set<int> c;
bool check(int i)
{
	for(int j = 1; j <= v; j++)
		{
			if(G[i][j] == 1 && color[i] == color[j]) 
			{
				return false;
			}
		}
	return true;
}
int main()
{
	cin >> v >> e >> k;
	while(e--)
	{
		int a,b;
		cin >> a >> b;
		G[a][b] = 1;
		G[b][a] = 1;
	}
	int m;
	cin >> m;
	while(m--)
	{
	    //set<int> c;这样写就不用每次做c.clear()了	
		bool flag = true;
		for(int i = 1; i <= v; i++)
		{
			cin >> color[i];
			c.insert(color[i]);
		}
		if(c.size() != k) 
			{
				flag = false;
				cout << "No" << endl;
			}
		else
			{
			  for(int x = 1; x <= v; x++)
					{
						if(check(x) == false) 
						{
							cout << "No"<<endl;
							flag = false;
							break;
						}
					}
			}
			if(flag) cout << "Yes" << endl;
		c.clear();
	}
	return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

moumoumouwang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值