結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
56,000 KB
testcase_01 AC 119 ms
56,348 KB
testcase_02 AC 122 ms
56,020 KB
testcase_03 AC 264 ms
61,236 KB
testcase_04 AC 239 ms
59,944 KB
testcase_05 AC 260 ms
60,436 KB
testcase_06 AC 241 ms
60,312 KB
testcase_07 AC 235 ms
60,184 KB
testcase_08 AC 244 ms
60,448 KB
testcase_09 AC 254 ms
60,940 KB
testcase_10 AC 118 ms
56,024 KB
testcase_11 AC 120 ms
55,948 KB
testcase_12 AC 125 ms
55,856 KB
testcase_13 AC 122 ms
56,100 KB
testcase_14 AC 127 ms
56,268 KB
testcase_15 AC 121 ms
55,800 KB
testcase_16 AC 1,239 ms
70,816 KB
testcase_17 AC 1,312 ms
96,264 KB
testcase_18 AC 1,356 ms
78,832 KB
testcase_19 AC 1,537 ms
98,728 KB
testcase_20 AC 1,661 ms
98,912 KB
testcase_21 AC 1,529 ms
99,044 KB
testcase_22 AC 1,473 ms
93,888 KB
testcase_23 AC 1,544 ms
92,156 KB
testcase_24 AC 1,578 ms
93,316 KB
testcase_25 AC 1,563 ms
98,812 KB
testcase_26 AC 1,537 ms
99,384 KB
testcase_27 AC 1,654 ms
99,776 KB
testcase_28 AC 1,695 ms
100,240 KB
testcase_29 AC 1,558 ms
99,080 KB
testcase_30 AC 1,538 ms
99,228 KB
testcase_31 AC 1,652 ms
99,972 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(y) == find(x);
        }
        
        public void unite(int x, int y) {
            if (!same(x, y)) {
                parents[find(x)] = find(y);
            }
        }
    }
}
0