結果
問題 | No.416 旅行会社 |
ユーザー | htensai |
提出日時 | 2020-01-09 09:11:04 |
言語 | Java21 (openjdk 21) |
結果 |
MLE
|
実行時間 | - |
コード長 | 3,834 bytes |
コンパイル時間 | 4,560 ms |
コンパイル使用メモリ | 79,472 KB |
実行使用メモリ | 813,376 KB |
最終ジャッジ日時 | 2024-11-23 03:39:01 |
合計ジャッジ時間 | 42,321 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 880 ms
108,184 KB |
testcase_01 | MLE | - |
testcase_02 | AC | 58 ms
50,448 KB |
testcase_03 | AC | 60 ms
50,416 KB |
testcase_04 | AC | 62 ms
50,436 KB |
testcase_05 | AC | 63 ms
50,304 KB |
testcase_06 | AC | 65 ms
50,412 KB |
testcase_07 | AC | 110 ms
51,496 KB |
testcase_08 | AC | 179 ms
55,196 KB |
testcase_09 | AC | 269 ms
59,704 KB |
testcase_10 | AC | 1,032 ms
102,104 KB |
testcase_11 | TLE | - |
testcase_12 | TLE | - |
testcase_13 | AC | 1,630 ms
87,644 KB |
testcase_14 | AC | 1,817 ms
106,696 KB |
testcase_15 | AC | 1,981 ms
109,824 KB |
testcase_16 | AC | 1,905 ms
117,860 KB |
testcase_17 | AC | 1,819 ms
117,204 KB |
testcase_18 | AC | 1,622 ms
117,608 KB |
testcase_19 | AC | 1,213 ms
84,968 KB |
testcase_20 | MLE | - |
ソースコード
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; } } 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); } } }