結果
問題 | No.416 旅行会社 |
ユーザー | tenten |
提出日時 | 2020-10-16 17:16:51 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 2,146 ms / 4,000 ms |
コード長 | 3,741 bytes |
コンパイル時間 | 4,560 ms |
コンパイル使用メモリ | 76,296 KB |
実行使用メモリ | 104,520 KB |
最終ジャッジ日時 | 2023-08-21 10:38:41 |
合計ジャッジ時間 | 27,234 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,288 ms
85,812 KB |
testcase_01 | AC | 127 ms
56,336 KB |
testcase_02 | AC | 128 ms
55,748 KB |
testcase_03 | AC | 130 ms
55,712 KB |
testcase_04 | AC | 138 ms
54,036 KB |
testcase_05 | AC | 148 ms
56,000 KB |
testcase_06 | AC | 179 ms
56,000 KB |
testcase_07 | AC | 209 ms
58,556 KB |
testcase_08 | AC | 271 ms
60,676 KB |
testcase_09 | AC | 469 ms
63,812 KB |
testcase_10 | AC | 1,310 ms
86,016 KB |
testcase_11 | AC | 1,208 ms
84,072 KB |
testcase_12 | AC | 1,205 ms
85,432 KB |
testcase_13 | AC | 1,216 ms
94,332 KB |
testcase_14 | AC | 2,146 ms
104,084 KB |
testcase_15 | AC | 2,047 ms
104,520 KB |
testcase_16 | AC | 2,072 ms
100,708 KB |
testcase_17 | AC | 2,075 ms
104,108 KB |
testcase_18 | AC | 2,000 ms
100,576 KB |
testcase_19 | AC | 1,475 ms
88,504 KB |
testcase_20 | AC | 1,490 ms
86,584 KB |
ソースコード
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(); int q = sc.nextInt(); Bridge[] org = new Bridge[m]; for (int i = 0; i < m; i++) { org[i] = new Bridge(sc.nextInt() - 1, sc.nextInt() - 1); } HashSet<Bridge> broken = new HashSet<>(); Bridge[] breaking = new Bridge[q]; for (int i = 0; i < q; i++) { breaking[i] = new Bridge(sc.nextInt() - 1, sc.nextInt() - 1); broken.add(breaking[i]); } ArrayList<ArrayList<Integer>> graph = new ArrayList<>(); for (int i = 0; i < n; i++) { graph.add(new ArrayList<>()); } UnionFindTree uft = new UnionFindTree(n); for (Bridge b : org) { if (!broken.contains(b)) { uft.unite(b); graph.get(b.left).add(b.right); graph.get(b.right).add(b.left); } } boolean[] visited = new boolean[n]; int[] ans = new int[n]; ArrayDeque<Integer> deq = new ArrayDeque<>(); deq.add(0); while (deq.size() > 0) { int x = deq.poll(); if (visited[x]) { continue; } visited[x] = true; ans[x] = -1; deq.addAll(graph.get(x)); } for (int i = q - 1; i >= 0; i--) { if (uft.same(breaking[i])) { continue; } if (uft.same(0, breaking[i].left)) { deq.add(breaking[i].right); } else if (uft.same(0, breaking[i].right)) { deq.add(breaking[i].left); } while (deq.size() > 0) { int x = deq.poll(); if (visited[x]) { continue; } visited[x] = true; ans[x] = i + 1; deq.addAll(graph.get(x)); } uft.unite(breaking[i]); graph.get(breaking[i].left).add(breaking[i].right); graph.get(breaking[i].right).add(breaking[i].left); } StringBuilder sb = new StringBuilder(); for (int i = 1; i < n; i++) { sb.append(ans[i]).append("\n"); } System.out.print(sb); } static class UnionFindTree { int[] parents; public UnionFindTree(int size) { parents = new int[size]; for (int i = 0; i < size; i++) { parents[i] = i; } } public int find(int x) { if (parents[x] == x) { return x; } else { return parents[x] = find(parents[x]); } } public boolean same(int x, int y) { return find(x) == find(y); } public boolean same(Bridge b) { return same(b.left, b.right); } public void unite(int x, int y) { if (!same(x, y)) { parents[find(x)] = find(y); } } public void unite(Bridge b) { unite(b.left, b.right); } } static class Bridge { int left; int right; public Bridge(int left, int right) { this.left = left; this.right = right; } public int hashCode() { return left; } public boolean equals(Object o) { Bridge b = (Bridge)o; return left == b.left && right == b.right; } } }