結果

問題 No.1639 最小通信路
ユーザー ks2mks2m
提出日時 2021-08-06 21:47:10
言語 Java19
(openjdk 21)
結果
AC  
実行時間 159 ms / 2,000 ms
コード長 1,470 bytes
コンパイル時間 2,369 ms
コンパイル使用メモリ 78,104 KB
実行使用メモリ 58,632 KB
最終ジャッジ日時 2023-10-17 03:06:36
合計ジャッジ時間 7,777 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
53,472 KB
testcase_01 AC 55 ms
53,476 KB
testcase_02 AC 128 ms
58,408 KB
testcase_03 AC 158 ms
58,632 KB
testcase_04 AC 159 ms
58,628 KB
testcase_05 AC 79 ms
54,352 KB
testcase_06 AC 103 ms
56,376 KB
testcase_07 AC 53 ms
53,476 KB
testcase_08 AC 123 ms
56,144 KB
testcase_09 AC 58 ms
53,492 KB
testcase_10 AC 69 ms
54,356 KB
testcase_11 AC 114 ms
56,124 KB
testcase_12 AC 88 ms
54,352 KB
testcase_13 AC 130 ms
56,112 KB
testcase_14 AC 53 ms
53,416 KB
testcase_15 AC 74 ms
54,356 KB
testcase_16 AC 115 ms
56,516 KB
testcase_17 AC 124 ms
56,188 KB
testcase_18 AC 57 ms
53,496 KB
testcase_19 AC 101 ms
56,248 KB
testcase_20 AC 119 ms
56,096 KB
testcase_21 AC 54 ms
53,472 KB
testcase_22 AC 78 ms
54,356 KB
testcase_23 AC 110 ms
56,164 KB
testcase_24 AC 75 ms
54,488 KB
testcase_25 AC 83 ms
54,056 KB
testcase_26 AC 60 ms
53,432 KB
testcase_27 AC 56 ms
53,468 KB
testcase_28 AC 55 ms
53,468 KB
testcase_29 AC 118 ms
56,304 KB
testcase_30 AC 131 ms
55,984 KB
testcase_31 AC 73 ms
54,064 KB
testcase_32 AC 125 ms
56,388 KB
testcase_33 AC 60 ms
53,500 KB
testcase_34 AC 118 ms
55,876 KB
testcase_35 AC 149 ms
56,640 KB
testcase_36 AC 115 ms
56,316 KB
testcase_37 AC 126 ms
56,544 KB
testcase_38 AC 55 ms
52,364 KB
testcase_39 AC 80 ms
54,748 KB
testcase_40 AC 56 ms
53,484 KB
testcase_41 AC 61 ms
53,640 KB
testcase_42 AC 128 ms
56,560 KB
testcase_43 AC 76 ms
53,500 KB
testcase_44 AC 57 ms
52,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)];
		}
	}
}
0