結果
| 問題 |
No.416 旅行会社
|
| コンテスト | |
| ユーザー |
tenten
|
| 提出日時 | 2023-04-05 15:57:25 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 1,148 ms / 4,000 ms |
| コード長 | 4,937 bytes |
| コンパイル時間 | 2,897 ms |
| コンパイル使用メモリ | 92,340 KB |
| 実行使用メモリ | 80,280 KB |
| 最終ジャッジ日時 | 2024-10-02 01:58:51 |
| 合計ジャッジ時間 | 17,456 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 21 |
ソースコード
import java.io.*;
import java.util.*;
import java.util.stream.*;
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> remains = new HashSet<>();
for (int i = 0; i < m; i++) {
remains.add(new Bridge(sc.nextInt() - 1, sc.nextInt() - 1));
}
Bridge[] bridges = new Bridge[q];
for (int i = 0; i < q; i++) {
bridges[i] = new Bridge(sc.nextInt() - 1, sc.nextInt() - 1);
remains.remove(bridges[i]);
}
UnionFindTree uft = new UnionFindTree(n);
for (Bridge x : remains) {
uft.unite(x);
}
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
if (uft.same(0, i)) {
ans[i] = -1;
}
}
for (int i = q - 1; i >= 0; i--) {
for (int x : uft.unite(bridges[i])) {
ans[x] = i + 1;
}
}
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<ArrayList<Integer>> groups = new ArrayList<>();
public UnionFindTree(int size) {
parents = new int[size];
for (int i = 0; i < size; i++) {
parents[i] = i;
groups.add(new ArrayList<>());
groups.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 ArrayList<Integer> unite(int x, int y) {
int xx = find(x);
int yy = find(y);
if (xx == yy) {
return new ArrayList<>();
}
int type = 0;
if (same(0, xx)) {
type = 1;
} else if (same(0, yy)) {
type = 2;
}
ArrayList<Integer> ans = new ArrayList<>();
if (groups.get(xx).size() < groups.get(yy).size()) {
if (type == 1) {
ans.addAll(groups.get(yy));
} else if (type == 2) {
ans = groups.get(xx);
}
groups.get(yy).addAll(groups.get(xx));
parents[xx] = yy;
} else {
if (type == 1) {
ans = groups.get(yy);
} else if (type == 2) {
ans.addAll(groups.get(xx));
}
groups.get(xx).addAll(groups.get(yy));
parents[yy] = xx;
}
return ans;
}
public ArrayList<Integer> unite(Bridge x) {
return unite(x.left, x.right);
}
}
static class Bridge {
int left;
int right;
public Bridge(int left, int right) {
this.left = left;
this.right = right;
}
public int hashCode() {
return left + right;
}
public boolean equals(Object o) {
Bridge b = (Bridge)o;
return left == b.left && right == b.right;
}
public String toString() {
return left + ":" + right;
}
}
}
class Utilities {
static String arrayToLineString(Object[] arr) {
return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n"));
}
static String arrayToLineString(int[] arr) {
return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new));
}
}
class Scanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
StringBuilder sb = new StringBuilder();
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 int[] nextIntArray() throws Exception {
return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
}
public String next() throws Exception {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
}
tenten