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:

  1. 10 11
  2. 8 7
  3. 6 8
  4. 4 5
  5. 8 4
  6. 8 1
  7. 1 2
  8. 1 4
  9. 9 8
  10. 9 1
  11. 1 0
  12. 2 4
  13. 4
  14. 0 1 0 1 4 1 0 1 3 0
  15. 0 1 0 1 4 1 0 1 0 0
  16. 8 1 0 1 4 1 0 5 3 0
  17. 1 2 3 4 5 6 7 8 8 9

Sample Output:

  1. 4-coloring
  2. No
  3. 6-coloring
  4. No
  5.  
  6. 遍历加判断。set记录总共包含几种颜色,如果两个相邻点颜色相同就把flag置为1
    代码:
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <vector>
  5. #include <set>
  6. #include <algorithm>
  7. #define MAX 10000
  8. #define DMAX 10000
  9. using namespace std;
  10. typedef long long ll;
  11. int n,m,k,flag;
  12. bool vis[MAX + ];
  13. vector<int> v[MAX + ];//记录邻接表 也可以用数组模拟 vector比较方便
  14. set<int> s;
  15. int color[MAX + ];
  16. void dfs(int x) {
  17. if(flag) return;
  18. s.insert(color[x]);
  19. vis[x] = true;
  20. for(int i = ;i < v[x].size();i ++) {
  21. if(color[v[x][i]] == color[x]) flag = ;
  22. if(flag) return;
  23. if(!vis[v[x][i]]) dfs(v[x][i]);
  24. }
  25. }
  26.  
  27. int main() {
  28. int a,b;
  29. scanf("%d%d",&n,&m);
  30. for(int i = ;i < m;i ++) {
  31. scanf("%d%d",&a,&b);
  32. v[a].push_back(b);
  33. v[b].push_back(a);
  34. }
  35. scanf("%d",&k);
  36. while(k --) {
  37. for(int i = ;i < n;i ++) {
  38. scanf("%d",&color[i]);
  39. }
  40. s.clear();
  41. flag = ;
  42. memset(vis,false,sizeof(vis));
  43. for(int i = ;i < n;i ++) {///图不一定是连通图
  44. if(!vis[i]) {
  45. dfs(i);
  46. }
  47. }
  48. if(flag || s.empty()) puts("No");
  49. else printf("%d-coloring\n",s.size());
  50. }
  51. }

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. 事后调试.ZC资料

    1.查了一下,Delphi 程序 可以生成 map文件,可以用来 根据崩溃的内存报错 定位出错的代码位置 2.但是,Delphi程序 无法再崩溃的时候 生成dump文件 (这个不一定,研究了再说.记得 ...

  2. 如何调试nRF5 SDK

    本文将讲述Nordic nRF5 SDK的主要调试手段,以帮助大家快速定位问题,并解决问题.一般来说,你可以通过打log方式,IDE的debug模式,SDK自带的app_error_check函数,以 ...

  3. java socket 判断Socket连接失效

    要判断socket连接链路是否可用时,不能通过socket.isClosed() 和 socket.isConnected() 方法判断,要通过心跳包 socket.sendUrgentData(0x ...

  4. Mahout 0.10.1安装(Hadoop2.6.0)及Kmeans测试

    1.版本和安装路径 Ubuntu 14.04 Mahout_Home=/opt/mahout-0.10.1 Hadoop_Home=/usr/local/hadoop Mavent_Home=/opt ...

  5. Sql Server 2008 清理日志文件

    Use LingZhong Select NAME,size From sys.database_files ALTER DATABASE LingZhong SET RECOVERY SIMPLE ...

  6. bzoj 4627 值域线段树

    4627: [BeiJing2016]回转寿司 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 523  Solved: 227[Submit][Sta ...

  7. ZOJ 2599 Graduated Lexicographical Ordering ★(数位DP)

    题意 定义两个数的比较方法,各位数字之和大的数大,如果数字和相等则按字典序比较两个数的大小.输入n,k,求:1.数字k的排名:2.排名为k的数. 思路 算是一类经典的统计问题的拓展吧~ 先来看第一问. ...

  8. Linux 系统启动过程,Linux 系统目录结构

    一.Linux 系统启动过程 linux启动时我们会看到许多启动信息. Linux系统的启动过程并不是大家想象中的那么复杂,其过程可以分为5个阶段: 内核的引导. 运行 init. 系统初始化. 建立 ...

  9. ansible入门四(Ansible playbook基础组件介绍)

    本节内容: ansible playbook介绍 ansible playbook基础组件 playbook中使用变量 一.ansible playbook介绍 playbook是由一个或多个“pla ...

  10. 尝试优化骨骼动画计算的意外收获——使用嵌入式汇编对float转int进行优化

    本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4984530.html 公司引擎目前是使用CPU计算骨骼动画(采用了D3DX提供的函数 ...