并查集(多级联动)

发布时间:2024年01月19日
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class UnionFind {
    private Map<String, String> parent;

    public UnionFind() {
        parent = new HashMap<>();
    }

    public void makeSet(String element) {
        parent.put(element, element);
    }

    public String find(String element) {
        if (!parent.containsKey(element)) {
            return null;
        }

        String root = element;
        while (parent.get(root) != root) {
            root = parent.get(root);
        }

        // 路径压缩
        while (element != root) {
            String next = parent.get(element);
            parent.put(element, root);
            element = next;
        }

        return root;
    }

    public void union(String x, String y) {
        String rootX = find(x);
        String rootY = find(y);

        if (rootX != null && rootY != null && !rootX.equals(rootY)) {
            parent.put(rootX, rootY);
        }
    }

    public boolean isConnected(String x, String y) {
        String rootX = find(x);
        String rootY = find(y);

        return rootX != null && rootY != null && rootX.equals(rootY);
    }
}

public class Menu {
    public static void main(String[] args) {
        UnionFind uf = new UnionFind();

        // 创建省市区数据并初始化
        String[][] provinces = {
                {"湖南省", "长沙市", "岳麓区", "芙蓉区"},
                {"湖南省", "株洲市", "荷塘区", "天元区"},
                {"湖北省", "武汉市", "江岸区", "江汉区"},
                {"湖北省", "黄石市", "黄石港区", "西塞山区"},
                {"广东省", "广州市", "天河区", "越秀区"},
                {"广东省", "深圳市", "福田区", "罗湖区"},
                {"浙江省", "杭州市", "西湖区", "江干区"},
                {"浙江省", "宁波市", "海曙区", "江北区"},
                {"江苏省", "南京市", "玄武区", "秦淮区"},
                {"江苏省", "苏州市", "姑苏区", "吴中区"}
        };

        // 初始化并查集
        for (String[] areaArray : provinces) {
            for (String area : areaArray) {
                uf.makeSet(area);
            }
        }

        // 建立关系
        for (String[] areaArray : provinces) {
            for (int i = 1; i < areaArray.length; i++) {
                uf.union(areaArray[0], areaArray[i]);
            }
        }

        // 构建省市区的关系映射
        Map<String, Map<String, List<String>>> areaMap = new HashMap<>();
        for (String[] areaArray : provinces) {
            String province = areaArray[0];
            Map<String, List<String>> cityMap = areaMap.getOrDefault(province, new HashMap<>());

            String root = uf.find(province);
            String city = areaArray[1].split("市")[0];
            List<String> districtList = cityMap.getOrDefault(city, new ArrayList<>());
            for (int i = 2; i < areaArray.length; i++) {
                String area = areaArray[i];
                if (uf.isConnected(root, area)) {
                    districtList.add(area);
                }
            }
            cityMap.put(city, districtList);

            areaMap.put(province, cityMap);
        }

        // 打印省市区的关系
        for (Map.Entry<String, Map<String, List<String>>> entry : areaMap.entrySet()) {
            String province = entry.getKey();
            System.out.println(province);

            Map<String, List<String>> cityMap = entry.getValue();
            for (Map.Entry<String, List<String>> cityEntry : cityMap.entrySet()) {
                String city = cityEntry.getKey();
                List<String> districtList = cityEntry.getValue();
                System.out.print("\t" + city + "{");
                for (int i = 0; i < districtList.size(); i++) {
                    System.out.print(districtList.get(i));
                    if (i != districtList.size() - 1) {
                        System.out.print(", ");
                    }
                }
                System.out.println("}");
            }

            System.out.println();
        }
    }
}

文章来源:https://blog.csdn.net/weixin_57128596/article/details/135705113
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。