結果
問題 | No.416 旅行会社 |
ユーザー | htensai |
提出日時 | 2020-01-09 09:16:53 |
言語 | Java21 (openjdk 21) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,860 bytes |
コンパイル時間 | 2,578 ms |
コンパイル使用メモリ | 80,116 KB |
実行使用メモリ | 92,368 KB |
最終ジャッジ日時 | 2024-05-02 13:34:12 |
合計ジャッジ時間 | 12,768 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 753 ms
89,948 KB |
testcase_01 | AC | 45 ms
37,072 KB |
testcase_02 | AC | 46 ms
36,784 KB |
testcase_03 | AC | 44 ms
36,864 KB |
testcase_04 | AC | 44 ms
36,864 KB |
testcase_05 | AC | 47 ms
37,080 KB |
testcase_06 | AC | 48 ms
37,264 KB |
testcase_07 | AC | 88 ms
38,664 KB |
testcase_08 | AC | 137 ms
42,068 KB |
testcase_09 | AC | 222 ms
47,480 KB |
testcase_10 | AC | 780 ms
92,368 KB |
testcase_11 | TLE | - |
testcase_12 | -- | - |
testcase_13 | -- | - |
testcase_14 | -- | - |
testcase_15 | -- | - |
testcase_16 | -- | - |
testcase_17 | -- | - |
testcase_18 | -- | - |
testcase_19 | -- | - |
testcase_20 | -- | - |
ソースコード
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] first = br.readLine().split(" ", 3); int n = Integer.parseInt(first[0]); int m = Integer.parseInt(first[1]); int q = Integer.parseInt(first[2]); HashSet<Bridge> bridges = new HashSet<>(); for (int i = 0; i < m; i++) { bridges.add(new Bridge(br.readLine())); } Bridge[] events = new Bridge[q]; for (int i = 0; i < q; i++) { events[i] = new Bridge(br.readLine()); bridges.remove(events[i]); } UnionFindTree uft = new UnionFindTree(n); for (Bridge b : bridges) { uft.unite(b); } int[] results = new int[n]; HashSet<Integer> now = uft.prepare(); for (int x : now) { results[x] = -1; } for (int i = q - 1; i >= 0; i--) { HashSet<Integer> next = uft.uniteNext(events[i]); for (int x : next) { results[x] = i + 1; } next.clear(); } StringBuilder sb = new StringBuilder(); for (int i = 1; i < n; i++) { sb.append(results[i]).append("\n"); } System.out.print(sb); } static class Bridge { int left; int right; public Bridge (String arg) { String[] arr = arg.split(" ", 2); left = Integer.parseInt(arr[0]) - 1; right = Integer.parseInt(arr[1]) - 1; } public int hashCode() { return left + right; } public boolean equals(Object o) { Bridge b = (Bridge)o; return b.left == left && b.right == right; } } static class UnionFindTree { int[] parents; HashMap<Integer, HashSet<Integer>> map = new HashMap<>(); HashSet<Integer> nothing = new HashSet<>(); 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 void unite(int x, int y) { int xx = find(x); int yy = find(y); if (xx == yy) { return; } int small = Math.min(xx, yy); int large = Math.max(xx, yy); parents[large] = small; } public void unite(Bridge b) { unite(b.left, b.right); } public HashSet<Integer> uniteNext(int x, int y) { int xx = find(x); int yy = find(y); if (xx == yy) { return nothing; } int small = Math.min(xx, yy); int large = Math.max(xx, yy); parents[large] = small; map.get(small).addAll(map.get(large)); if (small == 0) { return map.get(large); } else { return nothing; } } public HashSet<Integer> uniteNext(Bridge b) { return uniteNext(b.left, b.right); } public HashSet<Integer> prepare() { for (int i = 0; i < parents.length; i++) { int x = find(i); if (!map.containsKey(x)) { map.put(x, new HashSet<Integer>()); } map.get(x).add(i); } return map.get(0); } } }