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:

Attenrion:

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

Code:

 /*
Data: 2019-08-02 21:08:29
Problem: PAT_A1154#Vertex Coloring
AC: 17:39 题目大意:
判断图中相连的顶点是否共色
*/
#include<cstdio>
#include<set>
using namespace std;
const int M=1e4+;
struct node
{
int u,v;
}grap[M];
int color[M]; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n,m,k;
scanf("%d%d", &m,&n);
for(int i=; i<n; i++)
scanf("%d%d", &grap[i].u,&grap[i].v);
scanf("%d", &k);
while(k--)
{
set<int> mp;
for(int i=; i<m; i++)
{
scanf("%d", &color[i]);
mp.insert(color[i]);
}
for(int i=; i<n; i++)
{
if(color[grap[i].u] == color[grap[i].v])
{
mp.clear();
break;
}
}
if(mp.size())
printf("%d-coloring\n", mp.size());
else
printf("No\n");
} return ;
}

最新文章

  1. 红米3 TWRP-3.0.3-RECOVERY-7.1.1中文版 20170101更新修复版
  2. WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
  3. SQL Server 常用分页SQL
  4. 深入理解PHP内核(五)函数的内部结构
  5. HTML Agility Pack 搭配 ScrapySharp,彻底解除Html解析的痛苦
  6. 配置SMarty解析
  7. php的header()函数之设置content-type
  8. 初学 Play Framework 以及可能遇到的问题
  9. 【JS】JavaScript中的参数传递
  10. 网站出现service unavailable的解决方法
  11. Python基础学习参考(四):条件与循环
  12. H5移动端项目案例、web手机微商城实战开发
  13. POSIX Timer
  14. 运动控制之一_PID控制理论
  15. C# 栈 、队列的概念
  16. V-rep学习笔记:机器人模型创建1—模型简化
  17. pythonweb框架Flask学习笔记04-模板继承
  18. java实验三实验报告
  19. [leetcode DP]53. Maximum Subarray
  20. Adobe Dynamic Http Streaming的简单配置与实现 (FMS, HLS, HDS)

热门文章

  1. 网络流入门 Drainage Ditches
  2. [poj3070]Fibonacci_矩乘_快速幂
  3. Clojure:两步发送iOS推送通知(apns)
  4. cocos2d-x andriod, Box2D.h: No such file or directory
  5. C#中数据库备份还原 精简
  6. matlab中s函数编写心得-转自水木
  7. windows php文件下载地址
  8. Appium + python - automator定位升级版操作
  9. Django day11(一) ajax 文件上传 提交json格式数据
  10. JavaScript的实参、形参以及变量