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
Sample output:
Yes
Yes
no
no
A simulation that needs attention is the color of the coloring if not equal to K is to output no
#include <cstdio>
#include <iostream>
#include <vector>
#include <set>
using namespace std;
int color[600];
vector<int>vec[600];
set<int>s;
int Main () {
int v,e,k;
scanf ("%d%d%d", &v,&e,&k);
for (int i=0;i<e;i++) {
int A, b;
scanf ("%d%d", &a,&b);
Vec[a].push_back (b);
Vec[b].push_back (a);
}
int n;
scanf ("%d", &n);
for (int i=0;i<n;i++) {
int flag=1;
S.clear ();
for (int j=1;j<=v;j++) {
scanf ("%d", &color[j]);
S.insert (Color[j]);
}
if (S.size ()!=k) {
printf ("no\n");
Continue;
}
for (int j=1;j<=v;j++) {for
(int k=0;k<vec[j].size (); k++) {
if (Color[j]==color[vec[j][k]]) {
printf ("no\n");
flag=0;
break;
}
}
if (!flag) break;
}
if (flag) printf ("yes\n");
}
return 0;