题目链接: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:

  1. 10 11
  2. 8 7
  3. 6 8
  4. 4 5
  5. 8 4
  6. 8 1
  7. 1 2
  8. 1 4
  9. 9 8
  10. 9 1
  11. 1 0
  12. 2 4
  13. 4
  14. 0 1 0 1 4 1 0 1 3 0
  15. 0 1 0 1 4 1 0 1 0 0
  16. 8 1 0 1 4 1 0 5 3 0
  17. 1 2 3 4 5 6 7 8 8 9

Sample Output:

  1. 4-coloring
  2. No
  3. 6-coloring
  4. No

题意

给定一个图,以及每个顶点的颜色,问是否所有边连接的两个顶点颜色不同。

思路

枚举每一条边即可。

代码

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int maxn = 1e4 + 10;
  4. pair<int, int> edges[maxn];
  5. int colors[maxn];
  6. int main() {
  7. ios::sync_with_stdio(false);
  8. cin.tie(0);
  9. int n, m;
  10. cin >> n >> m;
  11. for(int i = 0; i < m; ++i) {
  12. int x, y;
  13. cin >> x >> y;
  14. edges[i] = {x, y};
  15. }
  16. int k;
  17. cin >> k;
  18. while(k--) {
  19. map<int, int> mp;
  20. for(int i = 0; i < n; ++i) {
  21. cin >> colors[i];
  22. mp[colors[i]]++;
  23. }
  24. int flag = 1;
  25. for(int i = 0; i < m; ++i) {
  26. int x = edges[i].first, y = edges[i].second;
  27. if(colors[x] == colors[y]) {
  28. flag = 0;
  29. break;
  30. }
  31. }
  32. if(flag) {
  33. cout << mp.size() << "-coloring" << endl;
  34. } else {
  35. cout << "No" << endl;
  36. }
  37. }
  38. return 0;
  39. }

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. Oracle数据库的发展历程

    前言 1970年的6月,IBM 公司的研究员埃德加·考特 (Edgar Frank Codd) 在 Communications of ACM 上发表了那篇著名的<大型共享数据库数据的关系模型& ...

  2. vmware 中标麒麟系统安装VM tools

    打开虚拟机系统,选择菜单虚拟机->安装VMware TOOLs,下载后找到文件. 我的文件名称是 VMwareTools-10.2.5-8068393.tar.gz, 在终端里输入:tar xv ...

  3. TAB切换与内容伸展闭合的结合

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 【Matlab技巧】工作区变量如何添加到Simulink中?

    对新手来说,在进行simulink仿真时想把工作区的变量添加到Simulink中,这样在如transfer模块中使用时可以直接输变量即可. 如这样: 那么如何对Simulink仿真文件自动赋值呢? 1 ...

  5. vue防抖节流函数---组件封装,防止按钮多次点击

    1.vue 封装utils.js /** * @param {function} func 执行函数 * @param {number} time 防抖节流时间 * @param {boolean} ...

  6. 行人重识别(ReID) ——技术实现及应用场景

    导读 跨镜追踪(Person Re-Identification,简称 ReID)技术是现在计算机视觉研究的热门方向,主要解决跨摄像头跨场景下行人的识别与检索.该技术能够根据行人的穿着.体态.发型等信 ...

  7. java操作mongodb工具类

    新建maven项目 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht ...

  8. ChainMap简单示例

    ChainMap是dict的子类,拥有dict的所有功能, 感觉用它的地方吧??? from collections import ChainMap """ 相当于joi ...

  9. 监听UDP端口

    功能:监听服务器的UDP端口,输出端口接收的数据 #encoding:utf-8 import socket global udp global ip global port def listen_p ...

  10. PHPExcel 横向循环输出

    在PHPExcel中,我用到了一个循环,代码如下 for($i=4;$i<$num;$i++) { if($i>30) { $j="A".chr($i+34).&quo ...