PTA 1154 Vertex Coloring
题目链接:1154 Vertex Coloring
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 \(10^4\)), 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 1:
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
题意
给定一个图,以及每个顶点的颜色,问是否所有边连接的两个顶点颜色不同。
思路
枚举每一条边即可。
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 10;
pair<int, int> edges[maxn];
int colors[maxn];
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
for(int i = 0; i < m; ++i) {
int x, y;
cin >> x >> y;
edges[i] = {x, y};
}
int k;
cin >> k;
while(k--) {
map<int, int> mp;
for(int i = 0; i < n; ++i) {
cin >> colors[i];
mp[colors[i]]++;
}
int flag = 1;
for(int i = 0; i < m; ++i) {
int x = edges[i].first, y = edges[i].second;
if(colors[x] == colors[y]) {
flag = 0;
break;
}
}
if(flag) {
cout << mp.size() << "-coloring" << endl;
} else {
cout << "No" << endl;
}
}
return 0;
}
PTA 1154 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 ...
- 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 ...
- 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 ...
- PAT_A1154#Vertex Coloring
Source: PAT A 1154 Vertex Coloring (25 分) Description: A proper vertex coloring is a labeling of the ...
- PAT-1154(Vertex Coloring )+map使用+vector建图+set的使用
Vertex Coloring PAT-1154 #include<iostream> #include<cstring> #include<string> #in ...
- PAT(甲级)2018年冬季考试
1152 Google Recruitment 思路:判断素数 #include<bits/stdc++.h> using namespace std; const int maxn = ...
- PAT甲级 图 相关题_C++题解
图 PAT (Advanced Level) Practice 用到图的存储方式,但没有用到图的算法的题目 目录 1122 Hamiltonian Cycle (25) 1126 Eulerian P ...
随机推荐
- python为什么人们喜欢学习呢?
软件的质和量. 既有量的积累也有质的区别.继承一定的前人研究基础. 基本上来说,python更加的注重可读性,一致性,可移植性,其中软件的质量也是比较的讲究的. python支持开发的高级重用机制,例 ...
- 从ES6重新认识JavaScript设计模式: 装饰器模式
1 什么是装饰器模式 向一个现有的对象添加新的功能,同时又不改变其结构的设计模式被称为装饰器模式(Decorator Pattern),它是作为现有的类的一个包装(Wrapper). 可以将装饰器理解 ...
- 剑指offer学习读书笔记--二维数组中的查找
在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都是按照从上到下递增的顺序排序.请设计一个函数,输入这样的一个二维数组和一个整数,判断数组是否含有这个整数. 1 2 8 9 2 4 9 1 ...
- MiniUI学习笔记1-新手必读
1.mini的全局方法 2.Ajax jQuery 拥有完整的 Ajax 兼容套件.其中的函数和方法允许我们在不刷新浏览器的情况下从服务器加载数据. 详细jQuery Ajax教程,可参考这里. 3. ...
- 1145. Hashing - Average Search Time (25)
The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...
- 随笔1 interface Map<K,V>
第一次写笔记就从map开始吧,如上图所示,绿色的是interface,黄色的是abstract class,蓝色的是class,可以看出所有和图相关的接口,抽象类和类的起源都是interface ma ...
- 前端面试题(4)JavaScript
前端面试题JavaScript(一) JavaScript的组成 JavaScript 由以下三部分组成: ECMAScript(核心):JavaScript 语言基础 DOM(文档对象模型):规定了 ...
- web编程jsp小tips
jsp文件头 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEn ...
- django 使用ORM插入数据,提示Cannot assign "1": "B" must be a "Projectconfig" instance.
这是因为使用了外键导致的, 如果使用了外键,先实例化外键查询,然后再插入的表里面放入实例化后的外键连接
- selenium 自动化的坑(1)
UI自动化,一天一坑系列(1) 不要试图自作聪明添加一些不必要的东西. 个人遇到的问题关于下拉框的,本来一个下拉框连续点击两次就好了,结果自己多余将谷歌的提示‘正在受到自动化控制’去掉了,导致原本很简 ...