题目

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 k colors 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 10^4), 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. Afer 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

题目分析

已知图每条边的顶点信息,查询已知条件为图每个顶点的颜色。要求在查询已知条件下图每条边两个顶点颜色不同,满足条件条件输出K-coloring(K为颜色数),否则输出No

解题思路

  1. 定义结构体edge,记录每条边两个顶点的数字
  2. 定义edge es[M],记录所有边的顶点信息
  3. 定义int acs[N],记录每个查询条件中每个顶点的颜色;set cs,记录每个查询条件颜色数
  4. 遍历所有边,验证查询条件每个顶点的颜色是否满足每条边两个顶点颜色不同

知识点

  1. 局部定义bool flag;

    局部循环中定义bool flag,某次循环将flag=true后,下一次循环执行到bool flag,并不会将flag重新初始化为false,而是使用上一次循环执行的结果
  2. set
  1. set<int> s1; //初始化
  2. s1.insert(10); //插入
  3. //set<int>::iterator pos = s1.find(30); if (pos != s1.end()){//可找到} //查找
  4. //int num = s1.count(30); //统计

Code

Code 01

  1. #include <iostream>
  2. #include <set>
  3. using namespace std;
  4. struct edge {
  5. int left,right;
  6. };
  7. int main(int argc,char * argv[]) {
  8. int N,M,K,T;
  9. scanf("%d %d",&N,&M);
  10. edge es[M];
  11. for(int i=0; i<M; i++) {
  12. scanf("%d %d",&es[i].left,&es[i].right);
  13. }
  14. scanf("%d",&K);
  15. for(int i=0; i<K; i++) {
  16. int acs[N] = {0};
  17. set<int> cs;
  18. for(int j=0;j<N;j++){
  19. scanf("%d",&acs[j]);
  20. cs.insert(acs[j]);
  21. }
  22. int j;
  23. for(j=0;j<M;j++){
  24. if(acs[es[j].left]==acs[es[j].right])break;
  25. }
  26. if(j==M)printf("%d-coloring\n",cs.size());
  27. else printf("No\n");
  28. }
  29. return 0;
  30. }

Code 02

  1. #include <iostream>
  2. #include <set>
  3. using namespace std;
  4. struct edge {
  5. int left,right;
  6. };
  7. int main(int argc,char * argv[]) {
  8. int N,M,K,T;
  9. scanf("%d %d",&N,&M);
  10. edge es[M];
  11. for(int i=0; i<M; i++) {
  12. scanf("%d %d",&es[i].left,&es[i].right);
  13. }
  14. scanf("%d",&K);
  15. for(int i=0; i<K; i++) {
  16. int acs[N] = {0};
  17. set<int> cs;
  18. for(int j=0;j<N;j++){
  19. scanf("%d",&acs[j]);
  20. cs.insert(acs[j]);
  21. }
  22. bool flag=false;// 如果不写=false;初始化是false,但是之后的循环,并不会重置为false,而依旧使用的是上一次循环处理结束的值
  23. for(int j=0;j<M;j++){
  24. if(acs[es[j].left]==acs[es[j].right]){
  25. flag = true;
  26. break;
  27. }
  28. }
  29. if(!flag)printf("%d-coloring\n",cs.size());
  30. else printf("No\n");
  31. }
  32. return 0;
  33. }

PAT Advanced 1154 Vertex Coloring (25) [set,hash]的更多相关文章

  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 (25 分)

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

  3. PAT 甲级 1154 Vertex Coloring

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

  4. PAT Advanced 1134 Vertex Cover (25) [hash散列]

    题目 A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at ...

  5. PTA 1154 Vertex Coloring

    题目链接:1154 Vertex Coloring A proper vertex coloring is a labeling of the graph's vertices with colors ...

  6. PAT Advanced 1048 Find Coins (25) [Hash散列]

    题目 Eva loves to collect coins from all over the universe, including some other planets like Mars. On ...

  7. PAT Advanced 1020 Tree Traversals (25 分)

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

  8. PAT (Advanced Level) 1078. Hashing (25)

    二次探测法.表示第一次听说这东西... #include<cstdio> #include<cstring> #include<cmath> #include< ...

  9. PAT (Advanced Level) 1070. Mooncake (25)

    简单贪心.先买性价比高的. #include<cstdio> #include<cstring> #include<cmath> #include<vecto ...

随机推荐

  1. 五十二、SAP中的可编辑表格LVC

    一.之前我们写的表格如下,都是通过WRITE输出,不支持同步编辑等操作,给人感觉非常之LOW 二.在SAP中还存在另外一种可编辑的表格,叫LVC表格,效果如下, 三.此可标记表格是座位SAP内置模块来 ...

  2. 如何让手游内存占用更小?从内存消耗iOS实时统计开始

    为什么iOS内存使用过多会崩溃,性能会下降?腾讯游戏学院专家Devlin在本文给了解释,如何让手游内存占用更小?从内存消耗iOS实时统计开始. 一.问题 在之前的手游项目中,内存使用过多,都开始崩溃了 ...

  3. LCT(1)

    LCT(Link-Cut Tree,动态树)是一个支持动态修改树的结构的数据结构,其基本操作有 \(\texttt{access}\) , \(\texttt{findroot}\) , \(\tex ...

  4. 留学Essay写作常见谬误盘点

    留学生在完成英语论文作业的时候总会出现各种各样的谬误,导致最后拿不到高分,甚至挂科,最终只得选择写作完成.本文小编为大家总结出我们留学生在essay写作中几个常见谬误,希望大家有则改之,无则加勉. E ...

  5. 备份CSDN

    说明:https://blog.csdn.net/Feynman1999/article/details/87908082 源码:https://github.com/Feynman1999/CSDN ...

  6. Windows2008R2安装iis和iis下搭建web服务器(9.18 第七天)

    IIS internet information services 互联网信息服务微软开发的运行在windows中的互联网服务,提供了web.ftp.smtp服务 Windows server 200 ...

  7. CentOS下的安装命令 安装Nginx 更新yum源 kali系统当中的软件管理命令(第五天)

    Linux下软件的安装:方式:yum/rpm/源码安装YUM安装(帮助管理员解决依赖关系):yum search mysqld 在源中搜索软件包yum install mysql-connector- ...

  8. POJ 1068:Parencodings

    Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22849   Accepted: 13394 De ...

  9. shell 疑难

    #!/bin/bashBIN=`which $0`BIN=`dirname ${BIN}`BIN=`cd "$BIN"; pwd`  #列出脚本所在目录全局路径

  10. CSS 宽,高,背景设置

    width 宽,height 高,background 背景:背景色 background-color:颜色值--英文单词/十六进制/rgb:背景图 background-image:url(‘图片路 ...