l2-023. Graph coloring problem time limit MS
Memory Limit 65536 KB
Code length limit 8000 B
Standard author Chen Yue The procedure of the sentence
The graph coloring problem is a famous NP complete problem. Given the non-direction graph G = (V, E), ask if you can assign a color to each of the vertices in V, so that no two adjacent vertices will have the same color.
But the point is not to solve this coloring problem, but to a given color distribution, please determine whether this is a graph coloring problem of a solution.
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, edges, and colors of the graph without direction. The vertices and colors are numbered from 1 to v. Then line e, each row gives the number of two endpoints on one edge. After the information of the graph is given, a positive integer n (<= 20) is given, which is the number of color allocation schemes to be examined. Then n rows, each row gives the color of the V vertex (the first number denotes the color of the vertex i), and the number is separated by a space. The topic guarantees that the given graph is valid (i.e. no self loop and heavy edge).
output Format:
For each color allocation scheme, if it is a solution to the graph coloring problem, the output "Yes", otherwise the output "No", each sentence occupies one 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 Sample:
Yes
Yes
no
no
Problem-Solving ideas: The topic data is very small, so direct all violence to find once, see there is no adjacent point color the same ...
The code is as follows:
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;
vector<int>g[555];
int main ()
{
int v,e,k,n,a,b,s[555];
scanf ("%d%d%d", &v,&e,&k);
for (int i=0;i<e;i++)
{
scanf ("%d%d", &a,&b);
G[a].push_back (b);
G[b].push_back (a);
}
scanf ("%d", &n);
while (n--)
{
int vis[555]={0},ans=0;
for (int i=1;i<=v;i++)
{
scanf ("%d", &s[i]);
if (!vis[s[i]]) {vis[s[i]]=1; ans++;}
}
if (ans!=k) {printf ("no\n"); continue;}
int f=0;
for (int i=1;i<=v;i++)
{for
(int j=0;j<g[i].size (), j + +)
{
if (s[i]==s[g[i][j)]) {f=1; break;}
}
if (f==1) break;
printf (f? "no\n": "yes\n");
return 0;
}