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. [转载]Eclipse的常用快捷键

    常用的快捷键 ctrl+1:快速修复错误 ctrl+shift+L :查看快捷键 alt+?或alt+/:自动补全代码或者提示代码 ctrl+o:快速outline视图 ctrl+shift+r:打开 ...

  2. NoSQL&&Redis介绍

    再说Redis之前,想先说一下NoSQL.在最早的单机时代,随着数据的增加一台机器可能放不下了.同时索引占用的内存空间也会越来越大.对请求的读写操作影响很大.于是就在数据库之前增加了一层保护层 — 缓 ...

  3. php温习-变量,常量

    1.变量 内存中用于临时存储数据的一个空间,空间有一个名字子,变量都是以$开头 预定义变量:  $_GET  $_POST  $_REQUEST   $_SEVER  $_SEESION  $_COO ...

  4. 【转】由浅入深探究mysql索引结构原理、性能分析与优化

    摘要: 第一部分:基础知识 第二部分:MYISAM和INNODB索引结构 1.简单介绍B-tree B+ tree树 2.MyisAM索引结构 3.Annode索引结构 4.MyisAM索引与Inno ...

  5. 搭建etcd集群

    一 介绍 etcd 高可用一致性键值存储系统,使用Raft一直算法处理日志复制以保证数据一致性.主要在搭建kubernates时关注到etcd来研究部署etcd.使用golang语言编写,和zooke ...

  6. PANDAS 数据合并与重塑(concat篇)

    转自:http://blog.csdn.net/stevenkwong/article/details/52528616

  7. [Java基础] 深入jar包:从jar包中读取资源文件

    转载: http://hxraid.iteye.com/blog/483115?page=3#comments 我们常常在代码中读取一些资源文件(比如图片,音乐,文本等等).在单独运行的时候这些简单的 ...

  8. jsp获取绝对路径

    在JavaWeb开发中,常使用绝对路径的方式引入javaScript和CSS文件,这样可以避免因为目录变动导致引入文件找不到的情况,常用的做法是: 一.使用${pageContext.request. ...

  9. S16 day7 socket

    网络基础之网络协议篇:http://www.cnblogs.com/linhaifeng/articles/5937962.html 参考博客:http://www.cnblogs.com/linha ...

  10. 让你的ansible飞起来

    一.SSH Multiplexing 1 配置 vim /etc/ssh/ssh_config Host * GSSAPIAuthentication yes # If this option is ...