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

PAT_A1154#Vertex Coloring的更多相关文章

  1. PAT 甲级 1154 Vertex Coloring

    https://pintia.cn/problem-sets/994805342720868352/problems/1071785301894295552 A proper vertex color ...

  2. pat甲级 1154 Vertex Coloring (25 分)

    A proper vertex coloring is a labeling of the graph's vertices with colors such that no two vertices ...

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

  4. PTA 1154 Vertex Coloring

    题目链接:1154 Vertex Coloring A proper vertex coloring is a labeling of the graph's vertices with colors ...

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

  6. PAT-1154(Vertex Coloring )+map使用+vector建图+set的使用

    Vertex Coloring PAT-1154 #include<iostream> #include<cstring> #include<string> #in ...

  7. 1154 Vertex Coloring

    题目前半略 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 ...

  8. vtkMapper

    本文只是整理了该网页的内容:http://www.cnblogs.com/lizhengjin/archive/2009/08/16/1547340.html vtkMapper是一个抽象类,指定了几 ...

  9. Java基础常见英语词汇

    Java基础常见英语词汇(共70个) ['ɔbdʒekt] ['ɔ:rientid]导向的                             ['prəʊɡræmɪŋ]编程 OO: object ...

随机推荐

  1. JS推断是否为JSON对象及是否存在某字段

    $.ajax({ type: 'POST', url: url, success(function(data){ //推断是否为JSON对象 if(typeof(data) == "obje ...

  2. 技术总结--android篇(四)--工具类总结

    StringUtil(视个人须要进行加入) public class StringUtil { public static boolean isMail(String string) { if (nu ...

  3. android:怎样用一天时间,写出“飞机大战”这种游戏!(无框架-SurfaceView绘制)

    序言作为一个android开发人员,时常想开发一个小游戏娱乐一下大家,今天就说说,我是怎么样一天写出一个简单的"飞机大战"的. 体验地址:http://www.wandoujia. ...

  4. SQL SEVER 2008中的演示样例数据库

    SQL SEVER 2008数据库是什么我就不说了,我在这里分享一下怎样学习SQL SEVER 2008数据库,假设是对数据库或是SQL SEVER 数据库全然陌生或是不熟悉的人来说,建议看看一些视频 ...

  5. cocos2d-x 3.7 win7 32+Android 环境配置

    之前用的cocos2d-x 2.2.6 版本号,近期换成了3.7.眼下的最新版.整个过程中也碰到了不少问题.如今已经成功移植到手机上了. 分享下整个过程,希望能帮到别人.(所需软件已打包) [下载软件 ...

  6. Openwrt中luci配置页面cbi小记

    先看看network的配置文件: config interface 'loopback' option ifname 'lo' option proto 'static' option ipaddr  ...

  7. jQuery事件整理回想

    一.事件 1.载入DOM $(document).ready() 这个第一节里具体介绍了 2.事件绑定 jQuery定义了bind()方法作为统一的接口.用来为每个匹配元素绑定事件处理程序. 其基本的 ...

  8. 好记性不如烂笔头——WebService与Remoting

    一.WebService总体上分为5个层次: 1)HTTP传输信道 2)XML的数据格式 3)SOAP的封装协议,用于传输 4)WSDL的描述方式,用于引用 5)UDDI,通用描述.发现与集成服务,用 ...

  9. LESS2CSS for sumlime text2

    Windows下的安装 Less2Css插件依赖lessc这个工具,在windows下可以下载或者用git cloneless.js-windows到本地目录.然后把目录地址加入到环境变量PATH的中 ...

  10. SQL 琐碎记录

    1. 查看mysql现在已提供什么存储引擎: SHOW ENGINES ; 2. 查看mysql当前默认的存储引擎: SHOW VARIABLES LIKE '%storage_engine%'; 3 ...