結果
問題 | No.416 旅行会社 |
ユーザー | tenten |
提出日時 | 2022-08-23 08:38:32 |
言語 | Java21 (openjdk 21) |
結果 |
AC
|
実行時間 | 1,440 ms / 4,000 ms |
コード長 | 4,184 bytes |
コンパイル時間 | 3,360 ms |
コンパイル使用メモリ | 75,772 KB |
実行使用メモリ | 107,764 KB |
最終ジャッジ日時 | 2023-08-21 10:51:14 |
合計ジャッジ時間 | 18,720 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge11 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 639 ms
90,940 KB |
testcase_01 | AC | 44 ms
49,372 KB |
testcase_02 | AC | 44 ms
49,256 KB |
testcase_03 | AC | 45 ms
49,380 KB |
testcase_04 | AC | 45 ms
49,360 KB |
testcase_05 | AC | 47 ms
49,704 KB |
testcase_06 | AC | 50 ms
49,604 KB |
testcase_07 | AC | 90 ms
51,852 KB |
testcase_08 | AC | 145 ms
55,172 KB |
testcase_09 | AC | 219 ms
59,880 KB |
testcase_10 | AC | 721 ms
99,860 KB |
testcase_11 | AC | 667 ms
99,328 KB |
testcase_12 | AC | 656 ms
100,668 KB |
testcase_13 | AC | 622 ms
88,640 KB |
testcase_14 | AC | 1,378 ms
103,084 KB |
testcase_15 | AC | 1,351 ms
102,896 KB |
testcase_16 | AC | 1,374 ms
107,764 KB |
testcase_17 | AC | 1,340 ms
102,664 KB |
testcase_18 | AC | 1,440 ms
104,460 KB |
testcase_19 | AC | 1,084 ms
99,856 KB |
testcase_20 | AC | 1,049 ms
97,272 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(); } }