PAT_A1154#Vertex Coloring
Source:
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 properk
-coloring for some positivek
, orNo
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:
/*
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的更多相关文章
- PAT 甲级 1154 Vertex Coloring
https://pintia.cn/problem-sets/994805342720868352/problems/1071785301894295552 A proper vertex color ...
- pat甲级 1154 Vertex Coloring (25 分)
A proper vertex coloring is a labeling of the graph's vertices with colors such that no two vertices ...
- 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 ...
- PTA 1154 Vertex Coloring
题目链接:1154 Vertex Coloring A proper vertex coloring is a labeling of the graph's vertices with colors ...
- 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 ...
- PAT-1154(Vertex Coloring )+map使用+vector建图+set的使用
Vertex Coloring PAT-1154 #include<iostream> #include<cstring> #include<string> #in ...
- 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 ...
- vtkMapper
本文只是整理了该网页的内容:http://www.cnblogs.com/lizhengjin/archive/2009/08/16/1547340.html vtkMapper是一个抽象类,指定了几 ...
- Java基础常见英语词汇
Java基础常见英语词汇(共70个) ['ɔbdʒekt] ['ɔ:rientid]导向的 ['prəʊɡræmɪŋ]编程 OO: object ...
随机推荐
- [bzoj4385][POI2015]Wilcze doły_单调队列
Wilcze doły bzoj-4385 POI-2015 题目大意:给定一个n个数的序列,可以将连续的长度不超过d的区间内所有数变成0,求最长的一段区间,使得区间和不超过p. 注释:$1\le n ...
- memcached集群
借鉴:http://www.cnblogs.com/happyday56/p/3461113.html 首先说明下memcached存在如下问题 本身没有内置分布式功能,无法实现使用多台Memcach ...
- 切换div位置
通过数组来存放div的属性以及属性值,鼠标点击的时候,切换数组中的元素,然后赋值给div <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Tr ...
- [Angular] Why should we using Protal
Origianl article Protal from Angular CDK, is a way to create dynammic component. Consider an example ...
- 中国银联mPOS通用技术安全分析和规范解读
mPOS是近年出现并得到迅速发展的一种新型受理产品,不少机构和生产企业进行了各种形式的试点. 因为mPOS引入了手机.平板电脑等通用智能移动设备.并通过互联网进行信息传输.因此其安全特点与传统银行卡受 ...
- 为什么要阅读——兼分享《首先,打破一切常规》[中译文]:世界顶级管理者的成功秘诀/(美)马库斯·白金汉,(美)柯特·科夫曼 著
<ctrlno=255632">首先,打破一切常规>[中译文]:世界顶级管理者的成功秘诀/(美)马库斯·白金汉,(美)柯特·科夫曼 著:鲍世修 等译 下载地址:http:/ ...
- DirectX11 学习笔记6 - 使用D3DXMATH数学库的一个样例
这个样例是在之前的样例基础上 .把之前d3dx10math数学库换成了最新的d3dxmath.长处就不说了.先上效果图 所有代码.以及效果文件 文件结构 所有代码: 依照上图的文件顺序 #pragma ...
- ExtJs 给grid某一单元格重新赋值
// 司机,搬运工提成 var commissionMoney = (waybillsFtQty * commissionMoneyRatio / personCount).toFixed(2); / ...
- Linux 中的键盘映射【转】
本文转载自:http://hessian.cn/p/144.html [转]Linux 中的键盘映射 原文地址:http://www.linuxidc.com/Linux/2011-04/35197. ...
- Android+Jquery Mobile学习系列(2)-HTML5/Jquery Mobile基础
本章介绍两个关键字[HTML5]和[Jquery Mobile],简单说这两者的关系是:HTML5作为主体,Jquery Mobile在HTML5的基础上对其进行了优化.装饰. HTML5 HTML5 ...