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. tyvj 1027 木瓜地 简单模拟

    P1027 木瓜地 时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 USACO OCT09 4TH 描述 Bessie不小心游荡出Farmer John的田地 ...

  2. brew 与 nvm

    brew  与 nvm 是两个管理软件工具 今天更新了brew结果brew下安装的软件都找不着了.得重新安装,据说brew已经不再更新了.应该是通过github的吧. 结果得重装node与npm,这两 ...

  3. Konva的使用

    KonvaJS 快速入门 Konva 是一个 基于 Canvas 开发的 2d js 框架库, 它可以轻松的实现桌面应用和移动应用中的图形交互交互效果. Konva 可以高效的实现动画, 变换, 节点 ...

  4. panda 函数-处理空值

    今天这里谈的函数,以后进行数据分析的时候会经常用到. import numpy as npimport pandas as pdfrom pandas import DataFrame , Serie ...

  5. poj2823单调队列

    这个裸题,滑动窗口求最大最小值,单调队列来两边,一次单调递增q[s]就是最小值,一次单调递减q[s]就是最大值 cin会超时,解除同步也没用... #include<map> #inclu ...

  6. [myeclipse]@override报错问题

    @Override是JDK5 就已经有了,但有个小小的Bug,就是不支持对接口的实现,认为这不是Override 而JDK6 修正了这个Bug,无论是对父类的方法覆盖还是对接口的实现都可以加上@Ove ...

  7. 1-22-shell脚本基本应用-实验手册

    脚本应用思路 1. 确定命令操作(设计并执行任务) 2. 编写Shell脚本(组织任务过程) 3. 设置计划任务(控制时间,调用任务脚本) ------------------------------ ...

  8. Java中如何获取多维数组的长度

    在程序处理中遍历数组是一种很常见的操作,但遍历数组时,我们需要知道数组各维度的长度(一维数组除外,for each 可以遍历),下面举例说明如何求多维数组的长度. 一维 : int [] array ...

  9. Day14 js高级部分

    JS中文学习文档 http://jquery.cuishifeng.cn/ 一.函数变量的作用域: 变量的作用域是在声明时决定的而不是调用执行时决定 作用域链: 二.词法分析: 函数执行前,会进行预编 ...

  10. zoj3765

    题解: splay维护 注意是gcd 代码: #include<bits/stdc++.h> using namespace std; #define Key_value ch[ch[ro ...