130 被围绕的区域
2316 统计无向图中无法到达的点数
990 等式方程
128 最长连续序列
public class Node{ Integer value; public Node(Integer v){ value = v; } } public class UnionSet{ //V -> 节点 一一对应关系,值 - 》 值对应的节点 public HashMap > nodes = new HashMap<>(); //一个节点的父亲节点是谁作为一组记录,存在parent表里 public HashMap ,Node > parents = new HashMap<>(); //只有一个点,它是某个集合代表点的情况才有记录,这个点类似头节点 public HashMap ,Integer> sizeMap = new HashMap<>(); public UnionSet(Integer value){ for(Integer i=0;i< value;i++){ Node node = new Node<>(i); nodes.put(i,node); parents.put(node,node); sizeMap.put(node,1); } } //从点current一直往上找,找到不能再往上的代表点 public Node findFather(Node cur){ Stack > path = new Stack<>(); while(cur != parents.get(cur)){ path.push(cur); cur = parents.get(cur); } //cur头节点 while(!path.isEmpty()){ parents.put(path.pop(),cur); } return cur; } public boolean isSameSet(Integer a,Integer b){ if(!nodes.containsKey(a) || nodes.containsKey(b)){ return false; } return findFather(nodes.get(a)) == findFather(nodes.get(b)); } public void union(Integer a,Integer b){ if(!nodes.containsKey(a) || !nodes.containsKey(b)) return; Node aHead = findFather(nodes.get(a)); Node bHead = findFather(nodes.get(b)); if(aHead != bHead){ int aSetSize = sizeMap.get(aHead); int bSetSize = sizeMap.get(bHead); if(aSetSize >= bSetSize){ parents.put(bHead,aHead); sizeMap.put(aHead,aSetSize + bSetSize); sizeMap.remove(bHead); }else { parents.put(aHead,bHead); sizeMap.put(bHead,aSetSize + bSetSize); sizeMap.remove(aHead); } } } }



