题目链接: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 proper k-coloring for some positive k, or No 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的更多相关文章

  1. PAT 甲级 1154 Vertex Coloring

    https://pintia.cn/problem-sets/994805342720868352/problems/1071785301894295552 A proper vertex color ...

  2. pat甲级 1154 Vertex Coloring (25 分)

    A proper vertex coloring is a labeling of the graph's vertices with colors such that no two vertices ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. PAT_A1154#Vertex Coloring

    Source: PAT A 1154 Vertex Coloring (25 分) Description: A proper vertex coloring is a labeling of the ...

  7. PAT-1154(Vertex Coloring )+map使用+vector建图+set的使用

    Vertex Coloring PAT-1154 #include<iostream> #include<cstring> #include<string> #in ...

  8. PAT(甲级)2018年冬季考试

    1152 Google Recruitment 思路:判断素数 #include<bits/stdc++.h> using namespace std; const int maxn = ...

  9. PAT甲级 图 相关题_C++题解

    图 PAT (Advanced Level) Practice 用到图的存储方式,但没有用到图的算法的题目 目录 1122 Hamiltonian Cycle (25) 1126 Eulerian P ...

随机推荐

  1. P1455 搭配购买 (并查集+01背包)

    [题目描述] 明天就是母亲节了,电脑组的小朋友们在忙碌的课业之余挖空心思想着该送什么礼物来表达自己的心意呢?听说在某个网站上有卖云朵的,小朋友们决定一同前往去看看这种神奇的商品,这个店里有n朵云,云朵 ...

  2. ZPL文件打印

    ZPL:全称是Zebra Printer Language,目前能够直接打印zpl文件的打印机只有斑马打印机 如何打印zpl文件呢? 软件类: 一.Print Conductor(桌面打印软件) ht ...

  3. 奇葩问题:Invalid bound statement (not found): cn.zss.zsdemo.mapper.RoleMapper.selectByPrimaryKey

    使用mybatis,遇到Invalid bound statement (not found): cn.zss.zsdemo.mapper.RoleMapper.selectByPrimaryKey ...

  4. 记录面试一位三年经验Web前端开发者的过程

    今天是2019年6月5日,后天就是端午节了,提前祝端午节快乐! 好了,开始这次面试过程的正题部分. 当我从人事手中接下这份三年哥(暂拟名称)的简历的时候,看到三年工作经验,心想 这应该是个大佬了 挺厉 ...

  5. 88-基于FMC接口的2路CameraLink Base输入子卡模块

    基于FMC接口的2路CameraLink Base输入子卡模块 1.板卡概述 FMC连接器是一种高速多pin的互连器件,广泛应用于板卡对接的设备中,特别是在xilinx公司的所有开发板中都使用.该Ca ...

  6. localeCompare按首字母排序汉字

    sort() 方法用于对数组的元素进行排序. 如果想按照其他标准进行排序,就需要提供比较函数,该函数要比较两个值,然后返回一个用于说明这两个值的相对顺序的数字.比较函数应该具有两个参数 a 和 b,其 ...

  7. 脚本_查找 Linux 系统中的僵尸进程

    #!bin/bash#功能:查找Linux系统中的僵尸进程#作者:liusingbon#使用awk判断ps命令输出的第8列为Z时,显示该进程的 PID 和进程命令ps aux |awk '{if($8 ...

  8. [php代码审计] apache 后缀名解析“漏洞”

    不能说是漏洞,只是 apache 特性而已. 下面是apache  httpd.conf中截取的一段: <IfModule mime_module> # # TypesConfig poi ...

  9. java selenium常用API汇总

    (WebElement.iFrame.select.alert.浏览器窗口.事件.js)     一 WebElement相关方法 1.点击操作 WebElement button = driver. ...

  10. thinkphp 响应对象response

    1.可以通过修改配置文件的 default_return_type修改输出类型 // 默认输出类型 'default_return_type' => 'html', 2. 可以通过Config类 ...