Requirements:
The graph coloring problem is a well-known NP-complete problem. Given the graph G = (V, E), ask if you can assign a color to each vertex in V with a k color, so that no two adjacent vertices have the same color.
But the question is not to solve this coloring problem, but for a given color assignment, please decide if this is a solution to the graph coloring problem. Input Format:
The input gives 3 integer V (0 < v <= 500), E (>= 0) and K (0 < K <= V) in the first row, respectively, the number of vertices, sides, and colors of the graph. Vertices and colors are numbered from 1 to v. Then the E line, each row gives the number of the two endpoints of an edge. After the information in the graph is given, a positive integer n (<= 20) is given, which is the number of color assignment schemes to be checked. Then n rows, each row is given the color of the V vertex (the number I represents the color of the vertex i), and the numbers are separated by a space. The topic guarantees that the given graph is valid (i.e., there is no self-loop and heavy-side). output Format:
For each color allocation scheme, if it is a solution to the graph coloring problem, the output is "Yes", otherwise the output "No", each sentence occupies a row. Input Sample:
6 8 3
2 1
1 3
4 6
2 5
2 4
5 4
5 6
3 6
4
1 2 3 3 1 2
4 5 6 6 4 5
1 2 3 4 5 6
2 3 4 2 3 4 output Example:
Yes
Yes
No
No Problem Solving ideas:
The
is directly used to traverse the graph to determine whether the color is equal.
Note that the number of different colors required is equal to K. Code:
#include <iostream> #include <cstring> #include <set> using namespace std;
int V, E, K;
int map[501][501] = {0};
int color[501] = {0};
BOOL flag = TRUE;
BOOL vis[501];
void Isyes (int i) {if (Vis[i] | | | flag = = FALSE) {return;
} Vis[i] = true;
for (int j = 0; J < V; j + +) {if (color[i] = = Color[j] && map[i][j] = = 1) {flag = false;
Return
} else if (map[i][j] = = 1 && vis[j] = = False) {Isyes (j);
}}} int main () {cin >> v >> e >> k;
for (int i = 0; i < e; i++) {int x, y;
Cin >> x >> y;
x--;
y--;
Map[x][y] = 1;
MAP[Y][X] = 1;
} int m;
Cin >> m;
for (int i = 0; i < m; i++) {set<int> s;
for (int j = 0; J < V; j + +) {int C;
CIN >> C;
S.insert (c);
COLOR[J] = c; } if (S.size ()! = k) {flag = false;
} else {memset (Vis, false, sizeof (VIS));
Flag = true;
for (int j = 0; J < V; j + +) {Isyes (j);
if (flag = = False) {break;
}}} if (flag) {cout << "Yes" << Endl;
} else {cout << "No" << Endl;
}} return 0; }