码迷,mamicode.com
首页 > 编程语言 > 详细

【Union Find】JAVA implementation

时间:2014-05-03 21:21:44      阅读:336      评论:0      收藏:0      [点我收藏+]

标签:java   并查集   

bubuko.com,布布扣

bubuko.com,布布扣


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);
        
    } 
}


【Union Find】JAVA implementation,布布扣,bubuko.com

【Union Find】JAVA implementation

标签:java   并查集   

原文地址:http://blog.csdn.net/xiaozhuaixifu/article/details/24924419

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!