結果
問題 | No.416 旅行会社 |
ユーザー | tenten |
提出日時 | 2022-08-23 08:38:32 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,530 ms / 4,000 ms |
コード長 | 4,184 bytes |
コンパイル時間 | 2,424 ms |
コンパイル使用メモリ | 80,096 KB |
実行使用メモリ | 96,232 KB |
最終ジャッジ日時 | 2024-05-08 16:10:20 |
合計ジャッジ時間 | 18,894 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 703 ms
79,300 KB |
testcase_01 | AC | 52 ms
36,672 KB |
testcase_02 | AC | 52 ms
37,228 KB |
testcase_03 | AC | 53 ms
37,212 KB |
testcase_04 | AC | 54 ms
37,092 KB |
testcase_05 | AC | 55 ms
36,900 KB |
testcase_06 | AC | 57 ms
36,924 KB |
testcase_07 | AC | 90 ms
38,540 KB |
testcase_08 | AC | 159 ms
41,288 KB |
testcase_09 | AC | 235 ms
48,980 KB |
testcase_10 | AC | 740 ms
86,312 KB |
testcase_11 | AC | 712 ms
87,168 KB |
testcase_12 | AC | 710 ms
88,004 KB |
testcase_13 | AC | 674 ms
78,716 KB |
testcase_14 | AC | 1,499 ms
94,184 KB |
testcase_15 | AC | 1,487 ms
96,232 KB |
testcase_16 | AC | 1,510 ms
93,132 KB |
testcase_17 | AC | 1,481 ms
95,408 KB |
testcase_18 | AC | 1,530 ms
95,496 KB |
testcase_19 | AC | 1,097 ms
89,588 KB |
testcase_20 | AC | 1,117 ms
86,736 KB |
ソースコード
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); int n = sc.nextInt(); int m = sc.nextInt(); int q = sc.nextInt(); HashSet<Bridge> bridges = new HashSet<>(); for (int i = 0; i < m; i++) { bridges.add(new Bridge(sc.nextInt() - 1, sc.nextInt() - 1)); } Bridge[] brokens = new Bridge[q]; for (int i = 0; i < q; i++) { brokens[i] = new Bridge(sc.nextInt() - 1, sc.nextInt() - 1); bridges.remove(brokens[i]); } UnionFindTree uft = new UnionFindTree(n); for (Bridge x : bridges) { uft.unite(x); } int[] ans = new int[n]; for (int x : uft.getSet(0)) { ans[x] = -1; } for (int i = q - 1; i >= 0; i--) { HashSet<Integer> tmp = null; if (uft.same(0, brokens[i].left) && !uft.same(0, brokens[i].right)) { tmp = uft.getSet(brokens[i].right); } else if (uft.same(0, brokens[i].right) && !uft.same(0, brokens[i].left)) { tmp = uft.getSet(brokens[i].left); } else { uft.unite(brokens[i]); continue; } for (int x : tmp) { ans[x] = i + 1; } uft.unite(brokens[i]); } 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; ArrayList<HashSet<Integer>> group = new ArrayList<>(); public UnionFindTree(int size) { parents = new int[size]; for (int i = 0; i < size; i++) { parents[i] = i; group.add(new HashSet<>()); group.get(i).add(i); } } 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 boolean same(Bridge b) { return same(b.left, b.right); } public void unite(Bridge b) { unite(b.left, b.right); } public void unite(int x, int y) { int xx = find(x); int yy = find(y); if (xx == yy) { return; } if (group.get(xx).size() < group.get(yy).size()) { parents[xx] = yy; group.get(yy).addAll(group.get(xx)); } else { parents[yy] = xx; group.get(xx).addAll(group.get(yy)); } } public HashSet<Integer> getSet(int x) { return group.get(find(x)); } } 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 b.left == left && b.right == right; } } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); 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 String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }