A proper vertex coloring is a labeling of the graph's vertices with colors such that no two vertices sharing the same edge have the same color. A coloring using at most kcolors is called a (proper) k-coloring.

Now you are supposed to tell if a given coloring is a proper k-coloring.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N and M (both no more than 1), being the total numbers of vertices and edges, respectively. Then M lines follow, each describes an edge by giving the indices (from 0 to N−1) of the two ends of the edge.

After the graph, a positive integer K (≤ 100) is given, which is the number of colorings you are supposed to check. Then K lines follow, each contains N colors which are represented by non-negative integers in the range of int. The i-th color is the color of the i-th vertex.

Output Specification:

For each coloring, print in a line k-coloring if it is a proper k-coloring for some positive k, or No if not.

Sample Input:

10 11
8 7
6 8
4 5
8 4
8 1
1 2
1 4
9 8
9 1
1 0
2 4
4
0 1 0 1 4 1 0 1 3 0
0 1 0 1 4 1 0 1 0 0
8 1 0 1 4 1 0 5 3 0
1 2 3 4 5 6 7 8 8 9

Sample Output:

4-coloring
No
6-coloring
No
遍历加判断。set记录总共包含几种颜色,如果两个相邻点颜色相同就把flag置为1。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
#include <algorithm>
#define MAX 10000
#define DMAX 10000
using namespace std;
typedef long long ll;
int n,m,k,flag;
bool vis[MAX + ];
vector<int> v[MAX + ];//记录邻接表 也可以用数组模拟 vector比较方便
set<int> s;
int color[MAX + ];
void dfs(int x) {
if(flag) return;
s.insert(color[x]);
vis[x] = true;
for(int i = ;i < v[x].size();i ++) {
if(color[v[x][i]] == color[x]) flag = ;
if(flag) return;
if(!vis[v[x][i]]) dfs(v[x][i]);
}
} int main() {
int a,b;
scanf("%d%d",&n,&m);
for(int i = ;i < m;i ++) {
scanf("%d%d",&a,&b);
v[a].push_back(b);
v[b].push_back(a);
}
scanf("%d",&k);
while(k --) {
for(int i = ;i < n;i ++) {
scanf("%d",&color[i]);
}
s.clear();
flag = ;
memset(vis,false,sizeof(vis));
for(int i = ;i < n;i ++) {///图不一定是连通图
if(!vis[i]) {
dfs(i);
}
}
if(flag || s.empty()) puts("No");
else printf("%d-coloring\n",s.size());
}
}

pat甲级 1154 Vertex Coloring (25 分)的更多相关文章

  1. PAT Advanced 1154 Vertex Coloring (25 分)

    A proper vertex coloring is a labeling of the graph's vertices with colors such that no two vertices ...

  2. PAT 甲级 1154 Vertex Coloring

    https://pintia.cn/problem-sets/994805342720868352/problems/1071785301894295552 A proper vertex color ...

  3. PAT Advanced 1154 Vertex Coloring (25) [set,hash]

    题目 A proper vertex coloring is a labeling of the graph's vertices with colors such that no two verti ...

  4. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  5. PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)

    1146 Topological Order (25 分)   This is a problem given in the Graduate Entrance Exam in 2018: Which ...

  6. PAT 甲级 1071 Speech Patterns (25 分)(map)

    1071 Speech Patterns (25 分)   People often have a preference among synonyms of the same word. For ex ...

  7. PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)

    1063 Set Similarity (25 分)   Given two sets of integers, the similarity of the sets is defined to be ...

  8. PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)

    1059 Prime Factors (25 分)   Given any positive integer N, you are supposed to find all of its prime ...

  9. PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)

    1051 Pop Sequence (25 分)   Given a stack which can keep M numbers at most. Push N numbers in the ord ...

随机推荐

  1. jmeter-time函数

    别人写的一个详解置顶 http://www.cnblogs.com/MasterMonkInTemple/p/3442770.html 新建beanshell,time函数格式${_time(YYYY ...

  2. zoom:1;

    zoom:1;属性是IE浏览器的专有属性,可以设置或检索对象的缩放比例.触发ie的hasLayout属性.清除浮动.清除magin的重叠等. 注意:它未通过W3C验证.

  3. jquery阻止冒泡和阻止默认事件

    event.stopPropagation(); event.preventDefault(); http://www.cnblogs.com/qixuejia/archive/2013/10/10/ ...

  4. 树 & 二叉树

    2018-01-04 19:13:46 一.树 在计算机科学中,树(英语:tree)是一种数据结构,用来模拟具有树状结构性质的数据集合.它是由n(n>0)个有限节点组成一个具有层次关系的集合.把 ...

  5. 英语每日阅读---8、VOA慢速英语(翻译+字幕+讲解):脸肓症患者记不住别人的脸

    英语每日阅读---8.VOA慢速英语(翻译+字幕+讲解):脸肓症患者记不住别人的脸 一.总结 一句话总结: a.neural abnormalities are more widespread:Duc ...

  6. jmeter-01 JMeter HTTP测试的各元件功能演示示例

    最小的测试将包括测试计划,线程组和一个或多个采样器. Jmeter HTTP测试的各元件演示示例 功能逻辑:wap官网(a.4399sy.com.hk),每个用户登录一次,发帖2次,发帖成功后注销退出 ...

  7. Kafka、RabbitMQ、RocketMQ、ActiveMQ 17 个方面综合对比

    本文将从,Kafka.RabbitMQ.ZeroMQ.RocketMQ.ActiveMQ 17 个方面综合对比作为消息队列使用时的差异.(欢迎加入Java程序员群:630441304,一起学习交流会) ...

  8. 从零开始搭建webpack+react开发环境

    环境主要依赖版本 webpack@4.8.1 webpack-cli@2.1.3 webpack-dev-server@3.1.4 react@16.3.2 babel-core@6.26.3 bab ...

  9. HDU6166-求集合间的最短路

    Senior Pan Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tot ...

  10. magento: Your web server is configured incorrectly. As a result, configuration files with sensitive information are accessible from the outside 解决方案

    在linux(以UBUNTU, CENTOS为例)下安装完成magento时,在进入后台时, 有些童鞋可能会发现有如下的提示: Your web server is configured incorr ...