結果
| 問題 |
No.1639 最小通信路
|
| コンテスト | |
| ユーザー |
ks2m
|
| 提出日時 | 2021-08-06 21:47:10 |
| 言語 | Java (openjdk 23) |
| 結果 |
AC
|
| 実行時間 | 173 ms / 2,000 ms |
| コード長 | 1,470 bytes |
| コンパイル時間 | 2,215 ms |
| コンパイル使用メモリ | 77,560 KB |
| 実行使用メモリ | 57,296 KB |
| 最終ジャッジ日時 | 2024-09-17 01:41:06 |
| 合計ジャッジ時間 | 7,998 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge6 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 43 |
ソースコード
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int m = n * (n - 1) / 2;
int[] a = new int[m];
int[] b = new int[m];
String[] c = new String[m];
for (int i = 0; i < m; i++) {
String[] sa = br.readLine().split(" ");
a[i] = Integer.parseInt(sa[0]) - 1;
b[i] = Integer.parseInt(sa[1]) - 1;
c[i] = sa[2];
}
br.close();
UnionFind uf = new UnionFind(n);
for (int i = 0; i < m; i++) {
uf.union(a[i], b[i]);
if (uf.num == 1) {
System.out.println(c[i]);
return;
}
}
}
static class UnionFind {
int[] parent, size;
int num = 0; // 連結成分の数
UnionFind(int n) {
parent = new int[n];
size = new int[n];
num = n;
for (int i = 0; i < n; i++) {
parent[i] = i;
size[i] = 1;
}
}
void union(int x, int y) {
int px = find(x);
int py = find(y);
if (px != py) {
parent[px] = py;
size[py] += size[px];
num--;
}
}
int find(int x) {
if (parent[x] == x) {
return x;
}
parent[x] = find(parent[x]);
return parent[x];
}
/**
* xとyが同一連結成分か
*/
boolean same(int x, int y) {
return find(x) == find(y);
}
/**
* xを含む連結成分のサイズ
*/
int size(int x) {
return size[find(x)];
}
}
}
ks2m