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 ...
随机推荐
- (appium+python)UI自动化_01_自动化环境搭建【MAC版】
Appium简介 Appium是一个开源的.跨平台的测试框架,主要用来进行app UI自动化,适用于原生应用.混合应用和移动网页应用(H5页面).目前支持Python.JavaScript.Objec ...
- Html5 学习笔记 --》布局
不推荐: 浮动布局: footer 设置 clear : both 清理浮动 | header | |边 | | |内 | 内容 ...
- upc组队赛14 Communication【并查集+floyd /Tarjan】
Communication 题目描述 The Ministry of Communication has an extremely wonderful message system, designed ...
- js中的attributes和Attribute的用法和区别。
一:Attribute的几种用法和含义(attributes和Attribute都是用来操作属性的) getAttribute:获取某一个属性的值: setAttribute:建立一个属性,并同时给属 ...
- 将QTP运行时的错误截图上传到QC
Class QCImageErrorCapture Sub Class_Terminate() 'Check if the current test has failed. If failed the ...
- c#生成html静态文件时出现空白行,怎么去掉utf-8中的bom
public static void UTF8RemoveBOM(string filepath) { UTF8RemoveBOM(filepath, filepath); } public st ...
- Linux 环境下安装rlwrap工具
rlwrap项目是一个“readline包装器”,它使用GNU readline库来编辑任何其他命令的键 盘输入.通过rlwrap可以进行命令的上下切换,类似历史命令. 1.下载rlwrap rpm ...
- chrome浏览器屏蔽广告插件小例子
1.创建一个文件夹,名为"清除页面广告插件" 2.在文件夹内创建"manifest.json"文件, { "name": "第一个 ...
- SVN连接不上仓库,问题之一
如果之前用过SVN,在新的地址上用,发现一直连不上,报错.有可能是因为默认使用了之前的地址,所以没弹出输账号和密码的弹框. 解决方法就是:把之前的链接地址全部清除掉. 右键找到SVN里面的 Sett ...
- oracle exp不生成dumpfile,预估出实际导出文件的大小。
目的:在不创建dumpfile前预估出需要的导出文件大小. 适用于export 实验步骤如下:OS: Linux test20 2.6.18-238.el5 #1 SMP Sun Dec ...