当前位置:网站首页>L2-023 图着色问题 (25 分)(图的遍历)
L2-023 图着色问题 (25 分)(图的遍历)
2022-04-23 08:43:00 【.Ashy.】
描述:
图着色问题是一个著名的NP完全问题。给定无向图G=(V,E),问可否用K种颜色为V中的每一个顶点分配一种颜色,使得不会有两个相邻顶点具有同一种颜色?
但本题并不是要你解决这个着色问题,而是对给定的一种颜色分配,请你判断这是否是图着色问题的一个解。
输出:
输入在第一行给出3个整数V(0<V≤500)、E(≥0)和K(0<K≤V),分别是无向图的顶点数、边数、以及颜色数。顶点和颜色都从1到V编号。随后E行,每行给出一条边的两个端点的编号。在图的信息给出之后,给出了一个正整数N(≤20),是待检查的颜色分配方案的个数。随后N行,每行顺次给出V个顶点的颜色(第i个数字表示第i个顶点的颜色),数字间以空格分隔。题目保证给定的无向图是合法的(即不存在自回路和重边)。
输出格式:
对每种颜色分配方案,如果是图着色问题的一个解则输出Yes,否则输出No,每句占一行。
输入样例:
输入样例:
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
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const ll maxx = 1e18;
const int N = 1e6+100;
const int p = 1e4+10;
const double eps = 1e-8;
vector<int>ve[1001];
int n,m,k;
int a,b;
int co[N];
int q;
map<int,int>mp;
int judge()
{
for(int i=1;i<=n;i++)
{
int len=ve[i].size();
for(int j=0;j<len;j++)
{
if(co[i]==co[ve[i][j]])
return 0;
}//判断临边是否有颜色相同存在
}
return 1;
}
int main()
{
cin>>n>>m>>k;
for(int i=1;i<=m;i++)
{
cin>>a>>b;
ve[a].push_back(b);
ve[b].push_back(a);
}//建立无向图
cin>>q;
for(int i=1;i<=q;i++)
{
mp.clear();
for(int j=1;j<=n;j++)
{
cin>>co[j];
mp[co[j]]++;
}
if(mp.size()!=k)//判断颜色数是否合法
{
cout<<"No"<<endl;
}
else
{
if(judge()==1)
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No"<<endl;
}
}
}
return 0;
}
版权声明
本文为[.Ashy.]所创,转载请带上原文链接,感谢
https://blog.csdn.net/woshilichunyang/article/details/124357394
边栏推荐
猜你喜欢
让地球少些“碳”息 度能在路上
面了一圈,整理了这套面试题。。
Use of Arthas in JVM tools
在sqli-liabs学习SQL注入之旅(第十一关~第二十关)
Overview of bus structure
洋桃電子STM32物聯網入門30步筆記一、HAL庫和標准庫的區別
DJ音乐管理软件Pioneer DJ rekordbox
SYS_ CONNECT_ BY_ Path (column, 'char') combined with start with connect by prior
idea配置连接远程数据库MySQL,或者是Navicat连接远程数据库失败问题(已解决)
根据字节码获取类的绝对路径
随机推荐
'恶霸' Oracle 又放大招,各大企业连夜删除 JDK。。。
idea底栏打开services
经典题目刷一刷
Consensus Token:web3.0生态流量的超级入口
Complete binary search tree (30 points)
LeetCode396.旋转数组
LINQ学习系列-----1.4 匿名对象
Add random attributes to the Li class array objects and sort them
Restore binary tree (25 points)
关于cin,scanf和getline,getchar,cin.getline的混合使用
引用传递1
JSP page coding
DOM learning notes - traverse all element nodes of the page
Transformer XL: attention language modelsbbeyond a fixed length context paper summary
匿名類型(C# 指南 基礎知識)
增强现实技术是什么?能用在哪些地方?
Stm32f103zet6 [development of standard library functions] - Introduction to library functions
Use of applicationreadyevent
Excle plus watermark
What is RPC