結果
問題 | No.1293 2種類の道路 |
ユーザー | tenten |
提出日時 | 2023-04-11 19:41:25 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 549 ms / 2,000 ms |
コード長 | 3,456 bytes |
コンパイル時間 | 3,932 ms |
コンパイル使用メモリ | 96,188 KB |
実行使用メモリ | 76,864 KB |
最終ジャッジ日時 | 2024-04-16 16:30:33 |
合計ジャッジ時間 | 11,950 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 53 ms
36,936 KB |
testcase_01 | AC | 52 ms
37,192 KB |
testcase_02 | AC | 52 ms
36,984 KB |
testcase_03 | AC | 51 ms
37,024 KB |
testcase_04 | AC | 54 ms
37,168 KB |
testcase_05 | AC | 53 ms
37,132 KB |
testcase_06 | AC | 51 ms
37,076 KB |
testcase_07 | AC | 50 ms
37,320 KB |
testcase_08 | AC | 53 ms
37,336 KB |
testcase_09 | AC | 501 ms
52,928 KB |
testcase_10 | AC | 512 ms
54,832 KB |
testcase_11 | AC | 501 ms
53,544 KB |
testcase_12 | AC | 513 ms
53,124 KB |
testcase_13 | AC | 497 ms
53,592 KB |
testcase_14 | AC | 449 ms
57,916 KB |
testcase_15 | AC | 466 ms
57,788 KB |
testcase_16 | AC | 548 ms
58,108 KB |
testcase_17 | AC | 527 ms
58,364 KB |
testcase_18 | AC | 495 ms
70,060 KB |
testcase_19 | AC | 548 ms
76,552 KB |
testcase_20 | AC | 549 ms
76,864 KB |
testcase_21 | AC | 338 ms
47,008 KB |
testcase_22 | AC | 322 ms
47,304 KB |
testcase_23 | AC | 338 ms
46,784 KB |
ソースコード
import java.io.*; import java.util.*; import java.util.stream.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int d = sc.nextInt(); int w = sc.nextInt(); UnionFindTree drive = new UnionFindTree(n); for (int i = 0; i < d; i++) { drive.unite(sc.nextInt() - 1, sc.nextInt() - 1); } UnionFindTree walk = new UnionFindTree(n); for (int i = 0; i < w; i++) { walk.unite(sc.nextInt() - 1, sc.nextInt() - 1); } HashMap<Integer, HashSet<Integer>> bridge = new HashMap<>(); for (int i = 0; i < n; i++) { int x = drive.find(i); if (!bridge.containsKey(x)) { bridge.put(x, new HashSet<>()); } bridge.get(x).add(walk.find(i)); } HashMap<Integer, Integer> counts = new HashMap<>(); long ans = 0; for (int x : bridge.keySet()) { long count = 0; for (int y : bridge.get(x)) { count += walk.getCount(y); } ans += (count - 1) * drive.getCount(x); } 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]; } public int getCount(int x) { return counts[find(x)]; } } } class Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }