結果
問題 | No.1293 2種類の道路 |
ユーザー | tenten |
提出日時 | 2020-11-25 20:05:03 |
言語 | Java19 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,288 ms / 2,000 ms |
コード長 | 2,233 bytes |
コンパイル時間 | 4,878 ms |
コンパイル使用メモリ | 84,672 KB |
実行使用メモリ | 106,840 KB |
最終ジャッジ日時 | 2023-10-01 01:53:04 |
合計ジャッジ時間 | 24,483 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge12 |
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 136 ms
55,464 KB |
testcase_01 | AC | 131 ms
55,652 KB |
testcase_02 | AC | 127 ms
55,464 KB |
testcase_03 | AC | 129 ms
55,796 KB |
testcase_04 | AC | 150 ms
55,756 KB |
testcase_05 | AC | 129 ms
53,924 KB |
testcase_06 | AC | 128 ms
55,460 KB |
testcase_07 | AC | 129 ms
55,656 KB |
testcase_08 | AC | 165 ms
55,868 KB |
testcase_09 | AC | 1,163 ms
71,300 KB |
testcase_10 | AC | 1,153 ms
71,076 KB |
testcase_11 | AC | 1,168 ms
70,964 KB |
testcase_12 | AC | 1,211 ms
72,340 KB |
testcase_13 | AC | 1,152 ms
71,248 KB |
testcase_14 | AC | 1,031 ms
84,108 KB |
testcase_15 | AC | 975 ms
84,076 KB |
testcase_16 | AC | 1,233 ms
83,352 KB |
testcase_17 | AC | 1,288 ms
87,744 KB |
testcase_18 | AC | 1,018 ms
91,228 KB |
testcase_19 | AC | 1,102 ms
106,840 KB |
testcase_20 | AC | 1,119 ms
105,916 KB |
testcase_21 | AC | 905 ms
64,508 KB |
testcase_22 | AC | 880 ms
64,420 KB |
testcase_23 | AC | 850 ms
64,852 KB |
ソースコード
import java.util.*; public class Main { public static void main (String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int d = sc.nextInt(); int w = sc.nextInt(); UnionFindTree dUft = new UnionFindTree(n); UnionFindTree wUft = new UnionFindTree(n); for (int i = 0; i < d; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; dUft.unite(a, b); } for (int i = 0; i < w; i++) { int a = sc.nextInt() - 1; int b = sc.nextInt() - 1; wUft.unite(a, b); } HashMap<Integer, Integer> dCount = new HashMap<>(); HashMap<Integer, Integer> wCount = new HashMap<>(); HashMap<Integer, HashSet<Integer>> bridge = new HashMap<>(); for (int i = 0; i < n; i++) { int x = dUft.find(i); if (!dCount.containsKey(x)) { dCount.put(x, dUft.counts[x]); } int y = wUft.find(i); if (!wCount.containsKey(y)) { wCount.put(y, wUft.counts[y]); } if (!bridge.containsKey(x)) { bridge.put(x, new HashSet<>()); } bridge.get(x).add(y); } long ans = 0; for (Map.Entry<Integer, HashSet<Integer>> entry : bridge.entrySet()) { long count = 0; for (int y : entry.getValue()) { count += wCount.get(y); } ans += (count - 1) * dCount.get(entry.getKey()); } System.out.println(ans); } static class UnionFindTree { int[] parents; int[] counts; public UnionFindTree(int size) { parents = new int[size]; counts = new int[size]; for (int i = 0; i < size; i++) { parents[i] = i; counts[i] = 1; } } 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) { int xx = find(x); int yy = find(y); if (xx == yy) { return; } parents[xx] = yy; counts[yy] += counts[xx]; } } }