Group Programming Ladder Race l2-023 graph coloring problem

Source: Internet
Author: User
Tags first row time limit

l2-023. Graph coloring problem time limit (ms)
Memory Limit 65536 KB
Code length limit 8000 B
Chen, standard author of the procedure for the award of questions

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

Note: The color must be of type K


#include <iostream> #include <cstdio> #include <cstring> #include <string> #include < algorithm> #include <cmath> #include <map> #include <set> #include <stack> #include <queue

> #include <vector> #include <bitset> using namespace std;

#define LL Long Long const int INF = 0X3F3F3F3F;
int n, m, K;
int u[250090], v[250090];

int visit[505],a[505]; int main () {while (~scanf ("%d%d%d", &n, &m, &k)) {for (int i = 1; I <= m; i++) scanf ("%d%d", &u[i
		], &v[i]);
		int q;
		scanf ("%d", &q);
			while (q--) {memset (visit, 0, sizeof visit);
			int cnt = 0, flag = 1;
				for (int i = 1; I <= n; i++) {scanf ("%d", &a[i]);
				if (Visit[a[i]]) continue;
			Visit[a[i]] = 1, cnt++;
			} if (cnt! = k) {printf ("no\n"); continue;}
			for (int i = 1; I <= m; i++) if (a[u[i] [= a[v[i]]) flag = 0;
			if (flag) printf ("yes\n");
		else printf ("no\n");
}} return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.