栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

顶点着色 Java题解 (图,模拟)【PAT甲级1154】

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

顶点着色 Java题解 (图,模拟)【PAT甲级1154】

输入样例:

10 11
8 7
6 8
4 5
8 4
8 1
1 2
1 4
9 8
9 1
1 0
2 4
4
0 1 0 1 4 1 0 1 3 0
0 1 0 1 4 1 0 1 0 0
8 1 0 1 4 1 0 5 3 0
1 2 3 4 5 6 7 8 8 9

输出样例:

4-coloring
No
6-coloring
No

解题思路:

判断图的任意两点是否染色相同,只需要遍历所有的图的边,判断是否有两端点的值相同。

Java代码:
import java.io.*;
import java.util.HashSet;

public class Main {
	static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
	public static int ini() throws IOException {
		st.nextToken();
		return (int)st.nval;
	}
	
	static class Node{
		int a, b;
		public Node(int a, int b) {
			this.a = a;
			this.b = b;
		}
	}
	
	public static void main(String[] args) throws IOException {
		int n = ini(), m = ini();
		Node []node = new Node[m];
		for(int i = 0; i < m; i++) {
			int a = ini(), b = ini();
			node[i] = new Node(a, b);
		}
		
		int q = ini();
		int []arr = new int[n];
		HashSet set = new HashSet<>();
		while(q-- > 0) {
			for(int i = 0; i < n; i++) {
				arr[i] = ini();
				set.add(arr[i]);
			}
			
			int i = 0;
			for(i = 0; i < m; i++) {  // 遍历的是边数!!
				int a = node[i].a, b = node[i].b;
				if(arr[a] == arr[b]) break;
			}
			if(i == m) System.out.printf("%d-coloringn", set.size());
			else System.out.println("No");
			set.clear();
		}
		
	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/820978.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号