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;

}

猜你喜欢

转载自blog.csdn.net/moumoumouwang/article/details/109656062