結果

問題 No.1390 Get together
ユーザー ks2mks2m
提出日時 2021-02-12 21:55:16
言語 Java21
(openjdk 21)
結果
AC  
実行時間 861 ms / 2,000 ms
コード長 1,816 bytes
コンパイル時間 2,564 ms
コンパイル使用メモリ 76,620 KB
実行使用メモリ 86,548 KB
最終ジャッジ日時 2023-09-27 03:51:03
合計ジャッジ時間 17,972 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
49,196 KB
testcase_01 AC 45 ms
49,716 KB
testcase_02 AC 45 ms
49,524 KB
testcase_03 AC 130 ms
53,256 KB
testcase_04 AC 127 ms
53,648 KB
testcase_05 AC 134 ms
53,232 KB
testcase_06 AC 126 ms
53,340 KB
testcase_07 AC 127 ms
53,040 KB
testcase_08 AC 129 ms
53,472 KB
testcase_09 AC 134 ms
55,208 KB
testcase_10 AC 45 ms
49,312 KB
testcase_11 AC 45 ms
49,320 KB
testcase_12 AC 44 ms
49,284 KB
testcase_13 AC 44 ms
49,560 KB
testcase_14 AC 44 ms
49,568 KB
testcase_15 AC 44 ms
49,676 KB
testcase_16 AC 499 ms
62,624 KB
testcase_17 AC 851 ms
86,184 KB
testcase_18 AC 493 ms
62,360 KB
testcase_19 AC 829 ms
85,852 KB
testcase_20 AC 861 ms
86,548 KB
testcase_21 AC 835 ms
85,480 KB
testcase_22 AC 679 ms
75,204 KB
testcase_23 AC 677 ms
75,784 KB
testcase_24 AC 695 ms
76,464 KB
testcase_25 AC 841 ms
85,820 KB
testcase_26 AC 831 ms
85,704 KB
testcase_27 AC 840 ms
85,636 KB
testcase_28 AC 843 ms
85,748 KB
testcase_29 AC 834 ms
85,924 KB
testcase_30 AC 840 ms
84,024 KB
testcase_31 AC 842 ms
86,188 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		int m = Integer.parseInt(sa[1]);
		Map<Integer, List<Integer>> map = new HashMap<>();
		for (int i = 0; i < n; i++) {
	 		sa = br.readLine().split(" ");
			int b = Integer.parseInt(sa[0]) - 1;
			int c = Integer.parseInt(sa[1]) - 1;
			List<Integer> list = map.get(c);
			if (list == null) {
				list = new ArrayList<>();
				map.put(c, list);
			}
			list.add(b);
		}
		br.close();

		UnionFind uf = new UnionFind(m);
		for (Integer key : map.keySet()) {
			List<Integer> list = map.get(key);
			if (list.size() > 1) {
				int f = list.get(0);
				for (int i = 1; i < list.size(); i++) {
					uf.union(f, list.get(i));
				}
			}
		}
		System.out.println(m - uf.num);
	}

	static class UnionFind {
		int[] parent, size;
		int num = 0; // 連結成分の数

		UnionFind(int n) {
			parent = new int[n];
			size = new int[n];
			num = n;
			for (int i = 0; i < n; i++) {
				parent[i] = i;
				size[i] = 1;
			}
		}

		void union(int x, int y) {
			int px = find(x);
			int py = find(y);
			if (px != py) {
				parent[px] = py;
				size[py] += size[px];
				num--;
			}
		}

		int find(int x) {
			if (parent[x] == x) {
				return x;
			}
			parent[x] = find(parent[x]);
			return parent[x];
		}

		/**
		 * xとyが同一連結成分か
		 */
		boolean same(int x, int y) {
			return find(x) == find(y);
		}

		/**
		 * xを含む連結成分のサイズ
		 */
		int size(int x) {
			return size[find(x)];
		}
	}
}
0