結果

問題 No.1390 Get together
ユーザー tentententen
提出日時 2021-02-13 16:43:13
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,641 ms / 2,000 ms
コード長 1,598 bytes
コンパイル時間 2,822 ms
コンパイル使用メモリ 76,432 KB
実行使用メモリ 102,176 KB
最終ジャッジ日時 2023-09-28 01:13:13
合計ジャッジ時間 32,157 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 115 ms
55,816 KB
testcase_01 AC 114 ms
56,048 KB
testcase_02 AC 121 ms
56,340 KB
testcase_03 AC 247 ms
60,928 KB
testcase_04 AC 231 ms
60,256 KB
testcase_05 AC 249 ms
60,532 KB
testcase_06 AC 243 ms
60,272 KB
testcase_07 AC 234 ms
60,192 KB
testcase_08 AC 236 ms
60,080 KB
testcase_09 AC 262 ms
60,764 KB
testcase_10 AC 119 ms
55,892 KB
testcase_11 AC 121 ms
55,968 KB
testcase_12 AC 122 ms
56,004 KB
testcase_13 AC 117 ms
56,140 KB
testcase_14 AC 121 ms
55,568 KB
testcase_15 AC 115 ms
56,016 KB
testcase_16 AC 1,222 ms
70,692 KB
testcase_17 AC 1,281 ms
94,964 KB
testcase_18 AC 1,364 ms
79,080 KB
testcase_19 AC 1,616 ms
99,120 KB
testcase_20 AC 1,549 ms
99,568 KB
testcase_21 AC 1,541 ms
98,680 KB
testcase_22 AC 1,515 ms
93,948 KB
testcase_23 AC 1,616 ms
95,144 KB
testcase_24 AC 1,481 ms
92,144 KB
testcase_25 AC 1,519 ms
98,716 KB
testcase_26 AC 1,639 ms
102,176 KB
testcase_27 AC 1,522 ms
98,880 KB
testcase_28 AC 1,641 ms
99,196 KB
testcase_29 AC 1,615 ms
99,032 KB
testcase_30 AC 1,561 ms
98,784 KB
testcase_31 AC 1,595 ms
98,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        HashMap<Integer, TreeSet<Integer>> counts = new HashMap<>();
        for (int i = 0; i < n; i++) {
            int b = sc.nextInt() - 1;
            int c = sc.nextInt();
            if (!counts.containsKey(c)) {
                counts.put(c, new TreeSet<>());
            }
            counts.get(c).add(b);
        }
        int ans = 0;
        UnionFindTree uft = new UnionFindTree(m);
        for (TreeSet<Integer> set : counts.values()) {
            int x = set.pollFirst();
            for (int y : set) {
                if (!uft.same(x, y)) {
                    ans++;
                    uft.unite(x, y);
                }
            }
        }
        System.out.println(ans);
    }
    
    static class UnionFindTree {
        int[] parents;
        
        public UnionFindTree(int x) {
            parents = new int[x];
            for (int i = 0; i < x; i++) {
                parents[i] = i;
            }
        }
        
        public int find(int x) {
            if (x == parents[x]) {
                return x;
            } else {
                return parents[x] = find(parents[x]);
            }
        }
        
        public boolean same(int x, int y) {
            return find(x) == find(y);
        }
        
        public void unite(int x, int y) {
            if (!same(x, y)) {
                parents[find(x)] = find(y);
            }
        }
    }
}
0