PAT_A1154#Vertex Coloring

Source:

PAT A 1154 Vertex Coloring (25 分)

Description:

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 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

Keys:

  • set(C++ STL)
  • 散列(Hash)

Attenrion:

  • 矩阵存储图,规模小于10^3
  • 对于多组测试用例的输入,要注意统计值和哈希函数的初始化(出题老师太坏了,测试用例即使不初始化也是对的-,-)

Code:

 1 /*
 2 Data: 2019-05-10 20:56:31
 3 Problem: PAT_A1154#Vertex Coloring
 4 AC: 17:39
 5 */
 6
 7 #include<cstdio>
 8 #include<set>
 9 using namespace std;
10 const int M=1e4+10;
11 struct node
12 {
13     int v1,v2;
14 }grap[M];
15 int c[M],n,m,k;
16 set<int> color;
17
18 int IsColoring()
19 {
20     for(int i=0; i<m; i++)
21         if(c[grap[i].v1] == c[grap[i].v2])
22             return 0;
23     return 1;
24 }
25
26 int main()
27 {
28 #ifdef    ONLINE_JUDGE
29 #else
30     freopen("Test.txt", "r", stdin);
31 #endif
32
33     scanf("%d%d", &n,&m);
34     for(int i=0; i<m; i++)
35         scanf("%d%d", &grap[i].v1,&grap[i].v2);
36     scanf("%d", &k);
37     for(int i=0; i<k; i++)
38     {
39         color.clear();
40         for(int j=0; j<n; j++)
41         {
42             scanf("%d", &c[j]);
43             color.insert(c[j]);
44         }
45         if(IsColoring())
46             printf("%d-coloring\n", color.size());
47         else
48             printf("No\n");
49     }
50
51     return 0;
52 }

原文地址:https://www.cnblogs.com/blue-lin/p/10847163.html

时间: 2024-07-31 18:54:47

PAT_A1154#Vertex Coloring的相关文章

PAT 甲级 1154 Vertex Coloring

https://pintia.cn/problem-sets/994805342720868352/problems/1071785301894295552 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 

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 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

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 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 gi

【转】编程词汇

很实用的编程英语词库,共收录一千五百余条词汇. 第一部分: application 应用程式 应用.应用程序 application framework 应用程式框架.应用框架 应用程序框架 architecture 架构.系统架构 体系结构 argument 引数(传给函式的值).叁见 parameter 叁数.实质叁数.实叁.自变量 array 阵列 数组 arrow operator arrow(箭头)运算子 箭头操作符 assembly 装配件 assembly language 组合语

Unity Dynamic Batching

1 public class test1 : MonoBehaviour { 2 3 public GameObject prefab; 4 5 void Start() 6 { 7 for (int i = 0; i < 100; i++) 8 { 9 GameObject tmp = Instantiate(prefab) as GameObject; 10 11 //Make sure you don't clone the material at runtime. If that's t

【转】Java 专业词汇

原址:http://blog.csdn.net/xiaojunjuns1/article/details/52729861 abstract (关键字)             抽象 ['.bstr.kt] access                            vt.访问,存取 ['.kses]'(n.入口,使用权) algorithm                     n.算法 ['.lg.riem] annotation                     [Java

计算机英语词汇--算法

算法常用术语中英对照Data Structures 基本数据结构Dictionaries 字典PriorityQueues 堆Graph Data Structures 图Set Data Structures 集合Kd-Trees 线段树Numerical Problems 数值问题Solving LinearEquations 线性方程组Bandwidth Reduction 带宽压缩Matrix Multiplication 矩阵乘法Determinants and Permanents

(转载)MatLab绘图

转载自:http://www.cnblogs.com/hxsyl/archive/2012/10/10/2718380.html 转载自:http://www.cnblogs.com/jeromeblog/p/3396494.html plot选项: 一 基础功能 二维图形 一. plot函数① 函数格式:plot(x,y)  其中x和y为长度相同                     坐标向量     函数功能:以向量x.y为轴,绘制曲线. [例] 在区间0≤X≤2?内,绘制正弦曲线y=si

Java基础常见英语词汇

(转自http://www.jianshu.com/p/2743fe834166) Java基础常见英语词汇(共70个) ['?bd?ekt] ['?:rientid]导向的 ['pr??ɡr?m??]编程OO: object-oriented ,面向对象 OOP: object-oriented programming,面向对象编程 [d?'vel?pm?nt][k?t]工具箱 ['v??tj??l]虚拟的JDK:Java development kit, java开发工具包 JVM:java