题目链接: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. Atlantis poj1151 线段树扫描线

    Atlantis poj1151 线段树扫描线 题意 题目给了n个矩形,每个矩形给了左下角和右上角的坐标,矩形可能会重叠,求的是矩形最后的面积. 题解思路 这个是我线段树扫描线的第一题,听了学长的讲解 ...

  2. python sys模块导入和模块的使用

    sys故名思意,就是系统模块,系统模块内置很多方法,怎么样去调用方法呢 1,argv 参数 sys.argv() 2,exit() 退出程序 sys.exit() 3,version  获取版本信息 ...

  3. Untiy3D按方向键获取值

    一,如下代码 float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical" ...

  4. VS2015-MFC基础教程-应用程序工程中文件的组成结构

    VS2015应用程序向导生成框架程序后,我们可以在之前设置的Location下看到此文件夹中包含了几个文件和一个以工程名命名的子文件夹,这个子文件夹中又包含了若干个文件和一个res文件夹,创建工程时的 ...

  5. python学习笔记(8):

    一.变量和类型 1.Python基本变量类型: 整数 ,浮点数 ,字符串, 布尔值 ,空值 ,函数, 模块, 类型, 自定义类型 2.变量定义 :变量存储在内存中的值.这就意味着在创建变量时会在内存中 ...

  6. node开发一个接口详细步骤

    最近在做后台系统改版,由于目前接口还没出来,就自己用nodejs写了个简单的接口. 我这里用的是nodejs+mysql的 这里不讲nodejs和mysql的安装.这些基础略过. 首先创建文件夹.cd ...

  7. 网络拓扑_配置hybrid端口

    目的:实现不同VLAN间的PC不可互访, 但不同VLAN的PC均可以访问服务器. 例: VLAN5与VLAN10的PC不可以互通,但它们均可与服务器VLAN50互通. 拓扑图:

  8. 多组件共享-vuex

    1.解决多个组件共享同一状态数据问题1)多个视图共享同一状态2)来自不同视图的触发事件需要变更同一状态文档API:https://vuex.vuejs.org/zh/api/ 2.组件与store连接 ...

  9. 对Promise的研究2

    3.Promise.prototype.then() Promise 实例具有then方法,也就是说,then方法是定义在原型对象Promise.prototype上的.它的作用是为 Promise ...

  10. c++11 默认函数的控制

    1. 类与默认函数: C++中声明自定义的类,编译器会默认生成未定义的成员函数: 构造函数 拷贝构造函数 拷贝赋值函数(operator=) 移动构造函数 移动拷贝函数 析构函数 编译器还会提供全局默 ...