zoukankan      html  css  js  c++  java
  • 【Union Find】JAVA implementation



    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Scanner;
    
    class UF
    {
    	private int[] id;
    	private int count;
    	
    	public UF(int N)
    	{
    		count = N;
    		id = new int[N];
    		for (int i = 0; i < N; i++) {
    			id[i] = i;
    		}
    	}
    	public int count(){
    		return count;
    	}
    	public int find(int p){
    		while (p != id[p]) {
    			p = id[p];
    		}
    		return p;
    	}
    	public boolean connected(int p, int q){
    		return find(p) == find(q);
    	}
    	public void union(int p, int q){
    		int proot = find(p);
    		int qroot = find(q);
    		
    		if (proot == qroot) {
    			return;
    		}
    		id[proot] = qroot;
    		count--;
    	}
    	public String toString()
    	{
    		return Arrays.toString(id);
    	}
    }
    
    public class Main {
        
        public static void main(String[] args) 
        {
            Scanner jin = new Scanner(System.in);
            int N = jin.nextInt();
            UF uf = new UF(N);
            while (jin.hasNext()) {
    			int p = jin.nextInt();
    			int q = jin.nextInt();
    			if (uf.connected(p, q)) {
    				continue;
    			}
    			uf.union(p, q);
    		}
            System.out.println(uf.count());
            
            System.out.println(uf);
            
        } 
    }

    对两棵树的合并操作做优化。引入变量size表示这棵树的结点个数,合并时。总是使size小的链接到size大的树。这样合并得到的森林。假设有N个结点。那么树的深度

    不会超过lgN。

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.HashMap;
    import java.util.Scanner;
    
    class UF
    {
    	private int[] id;
    	private int[] size;
    	private int count;
    	
    	public UF(int N)
    	{
    		count = N;
    		id = new int[N];
    		size = new int[N];
    		for (int i = 0; i < N; i++) {
    			id[i] = i; size[i] = 1;
    		}
    	}
    	public int count(){
    		return count;
    	}
    	public int find(int p){
    		while (p != id[p]) {
    			p = id[p];
    		}
    		return p;
    	}
    	public boolean connected(int p, int q){
    		return find(p) == find(q);
    	}
    	public void union(int p, int q){
    		int proot = find(p);
    		int qroot = find(q);
    		
    		if (proot == qroot) {
    			return;
    		}
    		if (size[proot] < size[qroot]) { id[proot] = qroot; size[qroot] += size[proot];}
    		else {id[qroot] = proot; size[proot] += size[qroot];}
    		count--;
    	}
    	public String toString()
    	{
    		return Arrays.toString(id);
    	}
    }
    
    public class Main {
        
        public static void main(String[] args) 
        {
            Long n;
            //System.out.println(Long.MAX_VALUE);
            Scanner jin = new Scanner(System.in);
            int N = jin.nextInt();
            UF uf = new UF(N);
            while (jin.hasNext()) {
    			int p = jin.nextInt();
    			int q = jin.nextInt();
    			if (uf.connected(p, q)) {
    				continue;
    			}
    			uf.union(p, q);
    		}
            System.out.println(uf.count());
            
            System.out.println(uf);
            
        } 
    }


  • 相关阅读:
    11.【原创】Object.keys()的一般用法
    5. 【原创】table设置text-overflow: ellipsis;(超出范围显示...)不生效
    12.【转载】vscode默认常用快捷键
    13.【原创】JS读取apk安装包的信息,做应用上传
    11.【原创】chrom文件上传后,手动释放内存
    26.Mysql "truncate"与"delete"的区别
    25.【转载】Mysql timestamp类型字段的CURRENT_TIMESTAMP与ON UPDATE CURRENT_TIMESTAMP属性
    bof
    ctf Wiener tricky
    分解大素数
  • 原文地址:https://www.cnblogs.com/lytwajue/p/7228552.html
Copyright © 2011-2022 走看看