Graph coloring problem is a well-known NP-complete problem. Given an undirected graph G = ( V , E ), by asking whether K colors as vertices assigned a color of each of a V, so that no two adjacent vertices with the same color?
But this problem is not to solve the problem of coloring you, but given a color assignment, you determine whether this is a solution graph coloring problem.
Input formats:
Input is given in the first row three integers V ( 0 < V ≤ . 5 0 0), E ( ≥ 0) and K ( 0 < K ≤ V), respectively, is no, the number of edge points toward the top graph, and the color number. Vertex and colors from 1 to No. V. Then E lines of a given number of sides of the two end points. After the information is given in FIG gives a positive integer N ( ≤ 2 0), is the number of color assignment scheme to be examined. Then N rows, each row is sequentially given V vertex colors (the i-th digit represents the i-th vertex color), separated by spaces between digits. Title ensure that a given undirected graph is a legitimate (i.e., the absence of self-weight and edge loop).
Output formats:
For each color allocation, graph coloring problem is if a solution is output Yes
, otherwise the output No
, each sentence per line.
Sample input:
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
Sample output:
Yes
Yes
No
No
#include<iostream> #include<string> #include<vector> #include<set> using namespace std; vector<vector<int>>G(501); int color[501] = { 0 }; bool check(int V) { for (int i = 1; i <= V; i++) for (int j = 0; j < G[i].size(); j++) if (color[i] == color[G[i][j]]) return false; return true; } int main() { int V, E, K, N; cin >> V >> E >> K; for (int i = 0; i < E; i++) { int start, end; cin >> start >> end; G[start].push_back(end); G[end].push_back(start); } cin >> N; for (int i = 0; i < N; i++) { set<int>color_kind; for (int j = 1; j <=V; j++) { cin >> color[j]; color_kind.insert(color[j]); } if (check(V) && color_kind.size() == K) cout << "Yes" << endl; else cout << "No" << endl; } return 0; }