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. [spring]xml配置文件---节点解释

    转载:https://blog.csdn.net/u012099568/article/details/51423837

  2. 代码演示神器——jsfiddle

    目录: 1. 介绍 2. jsfiddle的具体使用 3. 总结 1. 介绍 很多时候,我们需要在我们写的文章或博客中,即时显示出我们写的demo,能方便的解释出我们的思路.很久之前我也写过一篇文章, ...

  3. gitflow工作流程基本命令使用

    1 基础命令: 初始化: git flow init 开始新Feature: git flow feature start MYFEATURE Publish一个Feature(也就是push到远程) ...

  4. 1.SpringMVC设计理念与DispatcherServlet

    SpringMVC作为Struts2之后异军突起的一个表现层框架,正越来越流行,相信javaee的开发者们就算没使用过SpringMVC,也应该对其略有耳闻.我试图通过对SpringMVC的设计思想和 ...

  5. 13.LockSupport工具

    1. LockSupport简介 在之前介绍AQS的底层实现,已经在介绍java中的Lock时,比如ReentrantLock,ReentReadWriteLocks,已经在介绍线程间等待/通知机制使 ...

  6. Django之model字段操作

    # -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db import models import ...

  7. 022——VUE中remove()方法的使用:

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. CF 1A

    题解: n/a*m/a 都要向上取整 代码: #include<cstdio> #include<cmath> #include<cstring> #include ...

  9. flume 启动,停止,重启脚本

    #!/bin/bash #echo "begin start flume..." #flume的安装根目录(根据自己情况,修改为自己的安装目录) path=/sysware/apa ...

  10. LeetCode OJ:Reorder List(重序链表)

    Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...