pat甲级 1154 Vertex Coloring (25 分)
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 分)的更多相关文章
- 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 ...
- PAT 甲级 1154 Vertex Coloring
https://pintia.cn/problem-sets/994805342720868352/problems/1071785301894295552 A proper vertex color ...
- 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 ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which ...
- PAT 甲级 1071 Speech Patterns (25 分)(map)
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For ex ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 事后调试.ZC资料
1.查了一下,Delphi 程序 可以生成 map文件,可以用来 根据崩溃的内存报错 定位出错的代码位置 2.但是,Delphi程序 无法再崩溃的时候 生成dump文件 (这个不一定,研究了再说.记得 ...
- 如何调试nRF5 SDK
本文将讲述Nordic nRF5 SDK的主要调试手段,以帮助大家快速定位问题,并解决问题.一般来说,你可以通过打log方式,IDE的debug模式,SDK自带的app_error_check函数,以 ...
- java socket 判断Socket连接失效
要判断socket连接链路是否可用时,不能通过socket.isClosed() 和 socket.isConnected() 方法判断,要通过心跳包 socket.sendUrgentData(0x ...
- 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 ...
- Sql Server 2008 清理日志文件
Use LingZhong Select NAME,size From sys.database_files ALTER DATABASE LingZhong SET RECOVERY SIMPLE ...
- bzoj 4627 值域线段树
4627: [BeiJing2016]回转寿司 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 523 Solved: 227[Submit][Sta ...
- ZOJ 2599 Graduated Lexicographical Ordering ★(数位DP)
题意 定义两个数的比较方法,各位数字之和大的数大,如果数字和相等则按字典序比较两个数的大小.输入n,k,求:1.数字k的排名:2.排名为k的数. 思路 算是一类经典的统计问题的拓展吧~ 先来看第一问. ...
- Linux 系统启动过程,Linux 系统目录结构
一.Linux 系统启动过程 linux启动时我们会看到许多启动信息. Linux系统的启动过程并不是大家想象中的那么复杂,其过程可以分为5个阶段: 内核的引导. 运行 init. 系统初始化. 建立 ...
- ansible入门四(Ansible playbook基础组件介绍)
本节内容: ansible playbook介绍 ansible playbook基础组件 playbook中使用变量 一.ansible playbook介绍 playbook是由一个或多个“pla ...
- 尝试优化骨骼动画计算的意外收获——使用嵌入式汇编对float转int进行优化
本文为大便一箩筐的原创内容,转载请注明出处,谢谢:http://www.cnblogs.com/dbylk/p/4984530.html 公司引擎目前是使用CPU计算骨骼动画(采用了D3DX提供的函数 ...