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 ...
随机推荐
- Oracle数据库的发展历程
前言 1970年的6月,IBM 公司的研究员埃德加·考特 (Edgar Frank Codd) 在 Communications of ACM 上发表了那篇著名的<大型共享数据库数据的关系模型& ...
- vmware 中标麒麟系统安装VM tools
打开虚拟机系统,选择菜单虚拟机->安装VMware TOOLs,下载后找到文件. 我的文件名称是 VMwareTools-10.2.5-8068393.tar.gz, 在终端里输入:tar xv ...
- TAB切换与内容伸展闭合的结合
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【Matlab技巧】工作区变量如何添加到Simulink中?
对新手来说,在进行simulink仿真时想把工作区的变量添加到Simulink中,这样在如transfer模块中使用时可以直接输变量即可. 如这样: 那么如何对Simulink仿真文件自动赋值呢? 1 ...
- vue防抖节流函数---组件封装,防止按钮多次点击
1.vue 封装utils.js /** * @param {function} func 执行函数 * @param {number} time 防抖节流时间 * @param {boolean} ...
- 行人重识别(ReID) ——技术实现及应用场景
导读 跨镜追踪(Person Re-Identification,简称 ReID)技术是现在计算机视觉研究的热门方向,主要解决跨摄像头跨场景下行人的识别与检索.该技术能够根据行人的穿着.体态.发型等信 ...
- java操作mongodb工具类
新建maven项目 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...
- ChainMap简单示例
ChainMap是dict的子类,拥有dict的所有功能, 感觉用它的地方吧??? from collections import ChainMap """ 相当于joi ...
- 监听UDP端口
功能:监听服务器的UDP端口,输出端口接收的数据 #encoding:utf-8 import socket global udp global ip global port def listen_p ...
- PHPExcel 横向循环输出
在PHPExcel中,我用到了一个循环,代码如下 for($i=4;$i<$num;$i++) { if($i>30) { $j="A".chr($i+34).&quo ...