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
遍历加判断。set记录总共包含几种颜色,如果两个相邻点颜色相同就把flag置为1。
No
6-coloring
No
代码:
#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 ...
随机推荐
- 聚类算法——MCL
最近在看聚类方面的论文,接触到了MCL聚类,在网上找了许久,没什么中文的资料,可能写的最具体的便是GatsbyNewton写的 马尔可夫聚类算法(MCL) 这篇博客了.但是,其中仍有一些不详细的地方. ...
- FlatBuffer入门笔记
FlatBuffer入门笔记 1 flatbuffer资料 flatbuffer下载地址:https://github.com/google/flatbuffers flatbuffer官方使用文档: ...
- Resource——资源的总结
在xaml中,对于Style.DataTemplate.ControlTemplate.StoryBord等资源,可以放在UserControl.Resource.Windows.Resource.C ...
- [转载]宿主机为linux、windows分别实现VMware三种方式上网,Host-only win10+rhel7.2实现
研究一下虚拟机三种上网方式 转自:http://linuxme.blog.51cto.com/1850814/389691 一.VMware三种方式工作原理1 Host-only连接方式 让虚机具有与 ...
- brew || yarn 软件包管理工具
1.brew || yarn 软件包管理工具
- 【51nod-1278】相离的圆(二分)
思路 做法就是先把圆的直径化成线段,然后将线段的起点从小到大排序,以第i条线段为例,找i+1~n条中这样一条线段,满足是第一条且起点比第i条的终点要大(即满足相离),那么包括这条线段之后的线段也满足和 ...
- 关于javascript严格模式下七种禁止使用的写法
分享至javascript语言精髓与编程实践 开启严格模式(”use strict"): 在全局代码的开始处加入 在eval代码的开始处加入 在函数声明代码处加入 在new Function ...
- Python3的基础语法(四)
1,编码 默认情况下,Python 3 源码文件以 UTF-8 编码,所有字符串都是 unicode 字符串. 当然你也可以为源码文件指定不同的编码: # -*- coding: cp-1252 -* ...
- The main points of capacitive screen technology
- Git 配置ssh key的步骤
First start by setting up your own public/private key pair set. This can use either DSA or RSA, so b ...