18年冬季第三题 PAT甲级 1154 Vertex Coloring (25分) 两种思路

柔情只为你懂 2023-07-13 10:45 102阅读 0赞

题目来源:https://pintia.cn/problem-sets/994805342720868352/problems/1071785301894295552

备考汇总贴:2020年3月PAT甲级满分必备刷题技巧

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

题目大意(本栏、思路一及其代码源自liuchuo,我改得更通顺和好懂)

给出一个图(即给出所有边),然后给出多种图着色的查询,问是否满足 k个颜色的图着色(i.e. 涂上k个颜色后,所有的边的两个点的颜色不相同),如果满足还要指明k是多少。

题目分析

思路一:把所有边存起来,把所有点的颜色存起来(存的过程中放入set统计颜色个数),枚举所有边,检查是否每条边的两点个颜色是否相同;若全不相同,则输出颜色个数k,否则输出No

思路二:用邻接表的方式存图(双重vector),访问每个顶点的邻接顶点,如果存在颜色相同就输出No;如果全部都不同,就输出颜色个数k,否则输出No

满分代码

思路一

  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. using namespace std;
  5. struct node {int t1, t2;};
  6. int main() {
  7. int n, m, k;
  8. cin >> n >> m;
  9. vector<node> v(m);
  10. for (int i = 0; i < m; i++)
  11. scanf("%d %d", &v[i].t1, &v[i].t2);
  12. cin >> k;
  13. while (k--) {
  14. int a[10009] = {0};
  15. bool flag = true;
  16. set<int> se;
  17. for (int i = 0; i < n; i++) {
  18. scanf("%d", &a[i]);
  19. se.insert(a[i]);
  20. }
  21. for (int i = 0; i < m; i++) {
  22. if (a[v[i].t1] == a[v[i].t2]) {
  23. flag = false;
  24. break;
  25. }
  26. }
  27. if (flag)
  28. printf("%d-coloring\n", se.size());
  29. else
  30. printf("No\n");
  31. }
  32. return 0;
  33. }

思路二

  1. #include<iostream>
  2. #include<vector>
  3. #include<set>
  4. using namespace std;
  5. int main(){
  6. int n,m,k,a,b;
  7. scanf("%d %d",&n,&m);
  8. vector<vector<int>> v(n);
  9. for(int i=0;i<m;i++){
  10. scanf("%d %d",&a,&b);
  11. v[a].push_back(b);
  12. v[b].push_back(a);
  13. }
  14. scanf("%d",&k);
  15. for(int i=0;i<k;i++){
  16. vector<int> t(n);
  17. set<int> kc;
  18. for(int j=0;j<n;j++){
  19. scanf("%d",&t[j]);
  20. kc.insert(t[j]);
  21. }
  22. bool flag=true;
  23. for(int j=0;j<n;j++){
  24. for(auto it:v[j]){
  25. if(t[it]==t[j]){
  26. flag=false;
  27. break;
  28. }
  29. }
  30. }
  31. if(flag)printf("%d-coloring\n",kc.size());
  32. else printf("No\n");
  33. }
  34. return 0;
  35. }

备考汇总贴:2020年3月PAT甲级满分必备刷题技巧

如果喜欢我的博客,请为我点个赞,也欢迎多评论、转发,感谢!!

发表评论

表情:
评论列表 (有 0 条评论,102人围观)

还没有评论,来说两句吧...

相关阅读