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-coloringif it is a properk-coloring for some positivek, orNoif 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 ...
随机推荐
- 创建客户端项目并读取服务化的配置中心(Consul + Spring Cloud Config)
创建客户端项目并读取服务化的配置中心 将配置中心注册到服务中心(Consul) POM文件添加依赖: <dependency> <groupId>org.springframe ...
- [转载]企业级应用架构(NHibernater+Spring.Net+MVC3)
本人已经从事公司两套这类架构系统的开发工作啦!对于这套架构,我惊叹不已!BPS和CMS系统都是采用这套架构.但本人也同时渐渐发现了这套架构有诸多 不足之处,于是本人利用闲暇时光进一步改进了这套架构.新 ...
- linux搭建tomcat集群+nginx
安装JDK 一.官方下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 下 ...
- 解决javax.servlet.jsp.JspException cannot be resolved to a type
转自:https://blog.csdn.net/fengspg/article/details/41645159
- VUE CLI3.X 创建项目
Node.js环境搭建 Node.js基于V8引擎,可以让js代码脱离浏览器运行 Vue CLI3.0 需要Node.js 8.9或者更高版本. 用nvm或者nvm-windows在同一台电脑中管理多 ...
- nodejs 更新代码自动刷新页面
安装第三方工具: nodemon npm install --global nodemon 安装完毕后使用: 之前使用: node xxx.js 改成 nodemon xxx.js 只要通过nodem ...
- 网络拓扑_配置VLAN虚拟局域网
目的: 1.创建VLAN10,VALN20,VLAN30; 2.将端口加入VLAN 3.查看VLAN信息 拓扑图: 步骤: 1.在三层交换机中创建VLAN10,VLAN20,VLAN30 <Hu ...
- 【转】C/C++产生随机数
转自:https://www.cnblogs.com/vectors07/p/8185215.html C/C++怎样产生随机数:这里要用到的是rand()函数, srand()函数,C语言/C++里 ...
- 08Servlet
1.Servlet概念 1.1 servlet的特点 1)sevlet是一个普通的java类,继承HttpServlet类. 2)其实实现了Servlet接口的java类,才是一个Servlet类. ...
- Windows安装Scrapy(转)
转自: http://www.nnzhp.cn/archives/558 Scrapy是python开发的一个爬虫框架,如果你要学习它的话,可能第一步在安装的时候,就会遇到很多问题,因为Scrapy很 ...