Topic links
The graph coloring problem is a well-known NP-complete problem. Given the g= graph (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 integers 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 non-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 output Yes, otherwise output no, one row per sentence.
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
#include <iostream> #include <vector> #include <set> using namespace std;
int main () {int v,e,k,a,b,num,color[501];
Vector <int> mp[501];
Set <int> s;
scanf ("%d%d%d", &v,&e,&k);
for (int i = 0;i < e;i++) {scanf ("%d%d", &a,&b);
Mp[a].push_back (b);
Mp[b].push_back (a);
} scanf ("%d", &num);
while (num--) {bool flag = true;
S.clear ();
for (int i = 1;i <= v;i++) {scanf ("%d", &color[i]);
S.insert (Color[i]);
} if (S.size ()!=k) flag = false; for (int i = 1;i <= v;i++) {for (int j = 0;j < Mp[i].size (); j + +) {if (Color[i]==color[mp[i] [j]])
{flag = false;
Break
}} if (!flag) break;
} if (flag) printf ("yes\n");
else printf ("no\n"); }
return 0;
}