L2-3 图着色问题 (25分)

图着色问题是一个著名的NP完全问题。给定无向图G=(V,E)G = (V, E)G=(V,E),问可否用KKK种颜色为VVV中的每一个顶点分配一种颜色,使得不会有两个相邻顶点具有同一种颜色?

但本题并不是要你解决这个着色问题,而是对给定的一种颜色分配,请你判断这是否是图着色问题的一个解。

输入格式:

输入在第一行给出3个整数VVV0<V≤5000 < V \le 5000<V500)、EEE≥0\ge 00)和KKK0<K≤V0 < K \le V0<KV),分别是无向图的顶点数、边数、以及颜色数。顶点和颜色都从1到VVV编号。随后EEE行,每行给出一条边的两个端点的编号。在图的信息给出之后,给出了一个正整数NNN≤20\le 2020),是待检查的颜色分配方案的个数。随后NNN行,每行顺次给出VVV个顶点的颜色(第iii个数字表示第iii个顶点的颜色),数字间以空格分隔。题目保证给定的无向图是合法的(即不存在自回路和重边)。

输出格式:

对每种颜色分配方案,如果是图着色问题的一个解则输出Yes,否则输出No,每句占一行。

输入样例:

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

    
    

输出样例:

Yes
Yes
No
No

    
    

最后一个测试点超时

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

//** Class for buffered reading int and double values *//*
class Reader {
	static BufferedReader reader;
	static StringTokenizer tokenizer;

	// ** call this method to initialize reader for InputStream *//*
	static void init(InputStream input) {
		reader = new BufferedReader(new InputStreamReader(input));
		tokenizer = new StringTokenizer("");
	}

	// ** get next word *//*
	static String next() throws IOException {
		while (!tokenizer.hasMoreTokens()) {
			// TODO add check for eof if necessary
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}
	static boolean hasNext()throws IOException {
		return tokenizer.hasMoreTokens();
	}
	static String nextLine() throws IOException{
		return reader.readLine();
	}
	static char nextChar() throws IOException{
		return next().charAt(0);
	}
	static int nextInt() throws IOException {
		return Integer.parseInt(next());
	}

	static float nextFloat() throws IOException {
		return Float.parseFloat(next());
	}
}
public class Main {
	static class Edge{
		int left;
		int right;
	}
	private static Edge[]edges;
	public static void main(String[] args) throws IOException {
		Reader.init(System.in);
		int v = Reader.nextInt();
		int e = Reader.nextInt();
		int k = Reader.nextInt();
		
		edges = new Edge[e+1];
		for (int i = 1; i < edges.length; i++) {
			edges[i] = new Edge();
			edges[i].left = Reader.nextInt();
			edges[i].right = Reader.nextInt();
		}
		
		int n = Reader.nextInt();
		int []colors;
		Set<Integer>set;
		for (int i = 0; i < n; i++) {
			int flag = 1;
			colors = new int[v+1];
			set = new HashSet<Integer>();
			for (int j = 1; j < colors.length; j++) {
				colors[j] = Reader.nextInt();
				set.add(colors[j]);
			}
			if (set.size()!=k) flag =0;
			if (flag==1) {
				for (int j = 1; j < edges.length; j++) {
					if (colors[edges[j].left]==colors[edges[j].right]) {
						flag=0;
						break;
					}
				}
			}
			if (flag==1) {
				System.out.println("Yes");
			} else {
				System.out.println("No");
			}
		}
	}
	
}



发布了45 篇原创文章 · 获赞 8 · 访问量 1764

猜你喜欢

转载自blog.csdn.net/weixin_43888039/article/details/104115239