0

样本输入:

1
3 2
1 2
2 3

第一行 = # 个测试用例

第二行第一个人数=人数

第二行第二个数 = 好友数,F

跟随F行=友谊

输出将是最大朋友组的大小。

因此,该输入的样本输出为 3。 ([1, 2, 3])

我的解决方案:

import java.util.Scanner;
import java.util.HashMap;
import java.util.Collections;

public class Main {

    public static void main (String args[]) {
        Scanner reader = new Scanner(System.in);
        int tests = reader.nextInt();
        for (int i = 0; i < tests; i++) {
            int numcitizens = reader.nextInt();
            // the input is 1-indexed so I create the parent array as such
            int[] parent = new int[numcitizens + 1];
            for (int j = 0; j < (numcitizens + 1); j++) {
                parent[j] = j;
            }
            int[] rank = new int[numcitizens + 1];
            int numpairs = reader.nextInt();
            for (int j = 0; j < numpairs; j++) {
                int A = reader.nextInt();
                int B = reader.nextInt();
                union(A, B, parent, rank);
            }
            HashMap<Integer, Integer> histo = new HashMap<Integer, Integer>();
            for (int j = 1; j < parent.length; j++) {
                int root = parent[j];
                if (!histo.containsKey(root)) {
                    histo.put(root, 1);
                }
                else {
                    histo.put(root, histo.get(root) + 1);
                }

            }
            int max = Collections.max(histo.values());
            System.out.println(max);
        }
    }

    // UFDS find
    static int find(int x, int[] parent) {

        if (parent[x]!= x) {
            parent[x] = find(parent[x], parent);
        }

        return parent[x];
    }

    // UFDS union
    static void union(int x, int y, int[] parent, int[] rank) {
        int xRoot = find(x, parent);
        int yRoot = find(y, parent);

        if (xRoot == yRoot) {
            return;
        }
        else {
            if (rank[xRoot] < rank[yRoot]) {
                parent[xRoot] = yRoot;
            }

            else if (rank[yRoot] < rank[xRoot]) {
                parent[yRoot] = xRoot;
            }

            else {
                parent[yRoot] = xRoot;
                for (int i = 0; i < parent.length; i++) {
                    if (parent[i] == yRoot) {
                        parent[i] = xRoot;
                    }
                }
                rank[xRoot] = rank[xRoot] + 1;
            }
        }
    }
}

它适用于样本输入,但是当它通过一个运行数百个测试用例的在线判断系统时,它告诉我错误的输出。不确定我的错误可能在哪里?对我来说似乎是一个简单的 UFDS 问题。

4

2 回答 2

1

您放入的 for 循环会union破坏性能。把它拿出来。

在直方图循环中,您需要int root = find(j,parent);

numcitizens当为零时,您的代码会引发异常。

我想,修复这些东西,你的代码就可以工作了。

这里有一些额外的提示:

  • 我总是按大小联合而不是按等级联合。它具有相同的复杂性,并且大小通常很有用。在这种情况下,您不需要构建直方图,因为您将拥有每个根的大小。

  • 我对数据结构使用单个int[]数组。如果值v >=0,则它是具有该大小的根集(0 表示该索引处没有集或空集)。否则~v是到父集的链接。我最近在这个答案中使用了这个结构:算法:使用联合查找来计算岛屿的数量

于 2019-04-03T04:04:11.713 回答
0

每个人的朋友数量不是parent数组。每个人的排名是该集合表示的树的高度,即该人的朋友数量。所以root=rank[j]+1在 for 循环中使用来计算最大朋友数。

int max=Integer.MAX_VALUE;
for(int r:rank){
    max=Math.max(max,r+1);
}
System.out.println(max);
于 2019-04-02T23:10:57.857 回答