結果
| 問題 |
No.416 旅行会社
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2022-08-23 08:38:32 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 1,604 ms / 4,000 ms |
| コード長 | 4,184 bytes |
| コンパイル時間 | 3,145 ms |
| コンパイル使用メモリ | 80,276 KB |
| 実行使用メモリ | 94,848 KB |
| 最終ジャッジ日時 | 2024-12-14 21:16:58 |
| 合計ジャッジ時間 | 20,273 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 21 |
ソースコード
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();
}
}
tenten