1154 Vertex Coloring (25 分)

分手后的思念是犯贱 2022-03-02 15:59 287阅读 0赞

1154 Vertex Coloring (25 分)

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

给定一个图的结点数n和边数m,给出k个查询,问图相邻结点的颜色不能相同。

把m条边存下来,在k个查询中跑一遍,如果有一条边的两个结点是一样,则No。

否则输出给出的查询中有几种颜色。

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<iostream>
  5. #include<string>
  6. #include<sstream>
  7. #include<algorithm>
  8. #include<map>
  9. #include<set>
  10. #include<queue>
  11. #include<stack>
  12. #include<vector>
  13. using namespace std;
  14. #define inf 0x3f3f3f3f
  15. #define LL long long
  16. int op[10005],x[10005],y[10005];
  17. int main()
  18. {
  19. int n,m,k,i,j,f;
  20. scanf("%d%d",&n,&m);
  21. for(i=0;i<m;i++)
  22. {
  23. scanf("%d%d",&x[i],&y[i]);
  24. }
  25. scanf("%d",&k);
  26. while(k--)
  27. {
  28. map<int,int> s;
  29. f=0;
  30. for(i=0;i<n;i++)
  31. {
  32. scanf("%d",&op[i]);
  33. s[op[i]]++;
  34. }
  35. for(i=0;i<m;i++)
  36. {
  37. if(op[x[i]]==op[y[i]])
  38. {
  39. f=1;
  40. break;
  41. }
  42. }
  43. if(f==0)
  44. {
  45. printf("%d-coloring\n",s.size());
  46. }
  47. else
  48. printf("No\n");
  49. }
  50. }

发表评论

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

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

相关阅读

    相关 7-25 朋友圈 (25

    某学校有N个学生,形成M个俱乐部。每个俱乐部里的学生有着一定相似的兴趣爱好,形成一个朋友圈。一个学生可以同时属于若干个不同的俱乐部。根据“我的朋友的朋友也是我的朋友”这个推论可

    相关 PAT A1154

    ![clipboard.png][] 一道弱智的题。。我他妈的却废了这么久 就是个边判别的题我还用dfs和bfs。。。之后又忽律颜色给定的范围,范围根本不得而知,结果在一