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-coloring
Solution:
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 ...
随机推荐
- LeetCode 实现 Trie (前缀树)
题目链接:https://leetcode-cn.com/problems/implement-trie-prefix-tree/ 题目大意: 略. 分析: 字典树模板. 代码如下: class Tr ...
- 百度网盘不限速-Motrix
目录: Windows 下使用 Motrix Mac 下使用 BaiduNetdiskPlugin-macOS Mac 下使用 Motrix Windows 系统 Motrix 1. Google ...
- C++中类的静态成员变量
1,成员变量的回顾: 1,通过对象名能够访问 public 成员变量: 2,每个对象的成员变量都是专属的: 3,成员变量不能在对象之间共享: 1,在做程序设计中,成员变量一般是私有的.至少不是公有的: ...
- HDFS文件的基本操作
HDFS文件的基本操作: package wjn; import java.io.BufferedInputStream; import java.io.BufferedReader; import ...
- JavaScript学习笔记(基础部分)
一.JavaScript简介: 概念:JavaScript是一种解释性的.跨平台的.基于对象的脚本语言,一般用于客户端来给HTML页面增加动态的功能. 组成: 1.ECMAScript,描述了该语言的 ...
- ARM指令adr adrl ldr mov
ADR是一条小范围的地址读取伪指令,它将基于PC的相对偏移的地址值读到目标寄存器中.格式:ADR register,exper. 编译源程序时,汇编器首先计算当前PC值(当前指令位置)到exper的距 ...
- Matplotlib_key_point
Matplotlib官方入门教程: http://www.labri.fr/perso/nrougier/teaching/matplotlib/ 本文参考教程: http://codingpy.co ...
- http请求访问响应慢问题解决的基本思路
第一步,检查网络 ping命令检查网络域名解析是否正常,ping服务器的延迟是否过大,如果过大可以检查Ip是否冲突,或者交换机网线是否正常插好,通过nmon还可以查看网络流量,一般用的千兆交换机理论速 ...
- H2数据库做单测数据库时踩到的坑
H2数据库用来做单测数据库,可以自定义初始化数据,不用担心数据库内容更改造成单测跑不过问题,不过H2数据库跟实际使用的Mysql还是有一定区别. 1. H2数据库不支持Mysql的批量更新功能,支持批 ...
- python3.x 扯扯【切片】这玩意儿
在此之前先了解一下list这个玩意儿: list对应cpp这的数组,一维数组,二维数组,或者是嵌套都行: L=[] #空列表 L=[1,2,3,4,5,6] #六项 L=['a',['b','c']] ...