码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 1154 Vertex Coloring 甲级 xp_xht123


    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 104), 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
    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
    2. #include
    3. #include
    4. #include
    5. using namespace std;
    6. const int N = 1e4 + 10;
    7. int n , m;
    8. vector< vector<int> >v(N);
    9. int a[N];
    10. bool check()
    11. {
    12. for(int i = 0;i < n;i ++)
    13. {
    14. for(int j = 0;j < v[i].size();j ++)
    15. {
    16. if(a[i] == a[v[i][j]]) return false;
    17. }
    18. }
    19. return true;
    20. }
    21. int main()
    22. {
    23. cin >> n >> m;
    24. for(int i = 0;i < m;i ++)
    25. {
    26. int x , b;
    27. cin >> x >> b;
    28. v[x].push_back(b);
    29. v[b].push_back(x);
    30. }
    31. int k;
    32. cin >> k;
    33. while(k --)
    34. {
    35. unordered_map<int , int>mp;
    36. for(int i = 0;i < n;i ++)
    37. {
    38. cin >> a[i];
    39. mp[a[i]] ++;
    40. }
    41. int cnt = mp.size();//可能方案数
    42. bool flag = check();
    43. if(flag) printf("%d-coloring\n" , cnt);
    44. else puts("No");
    45. }
    46. return 0;
    47. }

  • 相关阅读:
    哪个牌子的台灯对孩子的视力好?对孩子视力好的台灯推荐分享
    Auto-GPT测评:自信、努力、不合格
    Java学习笔记3.10.3 异常处理 - 抛出异常
    【图文教程】若依前后端分离版本-菜单怎么设置
    KT6368A蓝牙芯片认证客户FCC定频出现频偏的处理总结
    自动化测试有必要学吗?
    FastReID 从pytorch到caffe (一)制作自己的数据集
    红黑树详解
    大规模 IoT 边缘容器集群管理的几种架构-6-个人体验及推荐
    【LeetCode热题100】【图论】岛屿数量
  • 原文地址:https://blog.csdn.net/xp_xht123/article/details/126334505
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号