京东完全多部图
import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Scanner; public class Solution3 { private static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int count = scanner.nextInt(); for (int i = 0; i < count; i++){ if (solute()){ System.out.println("Yes"); }else { System.out.println("No"); } } } public static boolean solute(){ int N = scanner.nextInt(); int M = scanner.nextInt(); HashMap<Integer, HashSet<Integer>> mapXKey = new HashMap<>(); HashMap<HashSet<Integer>, HashSet<Integer>> mapSetKey= new HashMap<>(); //保存每一个节点所连通的节点,存在自己的set中 for (int i = 0; i < M ; i ){ int X = scanner.nextInt(); int Y = scanner.nextInt(); mapXKey.putIfAbsent(X, new HashSet()); mapXKey.putIfAbsent(Y, new HashSet()); HashSet set = mapXKey.get(X); set.add(Y); set = mapXKey.get(Y); set.add(X); } //将“连通节点集合(上面的set)”相同的节点,组合到一起,代表一个集合, //该集合内的节点互相不连通。该集合作为value,key是该集合内节点都能连通到的节点集合 for (Map.Entry> entry : mapXKey.entrySet()){ mapSetKey.putIfAbsent(entry.getValue(), new HashSet()); mapSetKey.get(entry.getValue()).add(entry.getKey()); } //如果上面的每一个key和value的并集都包含了所有的节点,那么就是yes,否则false for (Map.Entry, HashSet> entry : mapSetKey.entrySet()){ HashSet targets = entry.getKey(); HashSet items = entry.getValue(); for (int i = 1; i <= N; i ) { if (!(targets.contains(i) || items.contains(i))){ return false; } } } return true; } }
#笔试题目##京东#