(PAT 1154) Vertex Coloring (图的广度优先遍历)

小灰灰 2022-03-22 14:22 250阅读 0赞

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

解题思路:

k着色问题,任意相邻的两个顶点之间不能有相同的颜色

利用广度优先遍历图即可,一个顶点和它每个相邻顶点的颜色都不相同。出现相同的情况之间返回false

至于k怎么得到,利用一个set来存储颜色即可

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <queue>
  4. #include <vector>
  5. #include <set>
  6. #include <string.h>
  7. using namespace std;
  8. int colorhashMap[10010];
  9. class gGraph {
  10. public:
  11. int n;
  12. vector<int>* edges;
  13. bool* visited;
  14. public:
  15. gGraph(int _n) {
  16. n = _n;
  17. edges = new vector<int>[n];
  18. visited = new bool[n];
  19. }
  20. void gInsert(int x, int y) {
  21. edges[x].push_back(y);
  22. edges[y].push_back(x);
  23. }
  24. bool BFS(int start_node) {
  25. queue<int> bfs_queue;
  26. visited[start_node] = true;
  27. bfs_queue.push(start_node);
  28. while (!bfs_queue.empty()) {
  29. int tempNode = bfs_queue.front();
  30. bfs_queue.pop();
  31. for (int adj_node : edges[tempNode]) {
  32. if (colorhashMap[tempNode] == colorhashMap[adj_node]) { return false; }
  33. if (!visited[adj_node]) {
  34. visited[adj_node] = true;
  35. bfs_queue.push(adj_node);
  36. }
  37. }
  38. }
  39. return true;
  40. }
  41. };
  42. int main() {
  43. int N, M;
  44. scanf("%d %d", &N, &M);
  45. gGraph colorMap(N);
  46. for (int i = 0; i < M; ++i) {
  47. int nx, ny;
  48. scanf("%d %d", &nx, &ny);
  49. colorMap.gInsert(nx, ny);
  50. }
  51. int K;
  52. scanf("%d", &K);
  53. for (int i = 0; i < K; ++i) {
  54. set<int> colorSet;
  55. for (int j = 0; j < N; ++j) {
  56. int ncolor;
  57. scanf("%d", &ncolor);
  58. colorhashMap[j] = ncolor;
  59. colorSet.insert(ncolor);
  60. }
  61. memset(colorMap.visited, 0, N);
  62. bool res = colorMap.BFS(0);
  63. if (res) {
  64. printf("%d-coloring\n", colorSet.size());
  65. }
  66. else {
  67. printf("No\n");
  68. }
  69. }
  70. system("PAUSE");
  71. return 0;
  72. }

发表评论

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

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

相关阅读

    相关 广度优先

    一 图的广度优先遍历基本思想 1 图的广度优先搜索(Broad First Search) ,简称BFS。 2 该遍历类似于一个分层搜索的过程,广度优先遍历需要使用一个