PAT甲级——A1154 VertexColoring【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 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 Klines 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-coloringSolution:
No
6-coloring
No
这道题很水,就是一个简单的判断图的边的两个顶点是不是同一个颜色,把图的精髓都没考出来,唯一有点考点的就是需要使用一个set来统计有几种颜色。
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
int main()
{
int n, m, k;
cin >> n >> m;
vector <vector<int>>path(n);
while (m--)
{
int a, b;
cin >> a >> b;
path[a].push_back(b);
path[b].push_back(a);
}
cin >> k;
while (k--)
{
vector<int>color(n, );
unordered_set<int>nums;
for (int i = ; i < n; ++i)
{
cin >> color[i];
nums.insert(color[i]);
}
bool flag = true;
for (int i = ; i < n && flag; ++i)
{
for (auto j : path[i])
{
if (color[i] == color[j])
{
flag = false;
break;
}
}
}
if (flag)
printf("%d-coloring\n", nums.size());
else
printf("No\n");
}
return ;
}
PAT甲级——A1154 VertexColoring【25】的更多相关文章
- PAT 甲级 1010 Radix (25)(25 分)进制匹配(听说要用二分,历经坎坷,终于AC)
1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...
- PAT 甲级1003 Emergency (25)(25 分)(Dikjstra,也可以自己到自己!)
As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...
- pat 甲级 1010. Radix (25)
1010. Radix (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a pair of ...
- pat 甲级 1078. Hashing (25)
1078. Hashing (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The task of t ...
- PAT 甲级 1003. Emergency (25)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...
- PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)
1032 Sharing (25 分) To store English words, one method is to use linked lists and store a word let ...
- PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
随机推荐
- ASP.NET MVC学习系列(二)-WebAPI请求 转载https://www.cnblogs.com/babycool/p/3922738.html
继续接着上文 ASP.NET MVC学习系列(一)-WebAPI初探 来看看对于一般前台页面发起的get和post请求,我们在Web API中要如何来处理. 这里我使用Jquery 来发起异步请求实现 ...
- nginx 配置代理 session 丢失
最开始一直用的ip访问的项目是没问题的 , 后来改成用了域名访问就发现一直获取不了session ,最开始以为是跨域问题 , 但项目中已经配置了跨域 , 因为第一次用 nginx ,果断入坑了 , ...
- 14. Jmeter-配置元件一
jmeter-配置元件介绍与使用 CSV 数据文件设置 HTTP信息头管理器 HTTP Cookie 管理器 HTTP Cache Manager HTTP请求默认值 计数器 DNS Cache Ma ...
- node_exporter + grafana
监控服务器CPU.内存.磁盘.I/O等信息,首先需要安装node_exporter.node_exporter的作用是用于机器系统数据收集. 下载安装: https://github.com/prom ...
- 我的js
<case value="select"> <div class="form-group item_{$[type]form.name} {$[type ...
- JavaScript DOM编程艺术-第一章
发现基础不是很好,补习一下.37买了2本书(dom编程和高级程序设计). 以前读书总是自己勾勾画画,有点没意思.现在写下来,说不定会成为传世经典.哈哈...........随便扯扯淡. 第一天(201 ...
- 【JavaScript性能优化】------理解Script标签的加载和执行
1.script标签是如何加载的?当浏览器遇到一个 < script>标签时,浏览器会停下来,运行JavaScript代码,然后再继续解析.翻译页面.同样的事情发生在使用 src 属性加载 ...
- python-orm操作数据库
ORM 模型 ORM概念 对象关系映射 全称(Object Relational Mapping) 简称ORM 模式是一种为了解决面向对象与关系型数据库存在的互不匹配的现象的技术. 简单来说, ORM ...
- while语句基本练习(求和思想,统计思想)
A:循环结构while语句的格式: 初始化语句; while(判断条件语句) { 循环体语句; 控制条件语句; } B:执行流程: a:执行初始化语句 b:执行判断条件语句,看其返回值是true还是f ...
- MyBatis中XML 映射配置文件的简单介绍
官网写的比较具体,可以查看以下的网站: http://www.mybatis.org/mybatis-3/zh/configuration.html 另外,实际用到标准的CRUD的操作和查询列表, & ...