結果

問題 No.1473 おでぶなおばけさん
ユーザー ks2mks2m
提出日時 2021-04-09 21:50:37
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,202 bytes
コンパイル時間 4,111 ms
コンパイル使用メモリ 77,628 KB
実行使用メモリ 78,976 KB
最終ジャッジ日時 2023-09-07 10:54:32
合計ジャッジ時間 33,209 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
52,548 KB
testcase_01 AC 87 ms
52,356 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 AC 529 ms
60,716 KB
testcase_42 AC 510 ms
60,808 KB
testcase_43 AC 524 ms
68,304 KB
testcase_44 AC 509 ms
68,072 KB
testcase_45 AC 528 ms
65,844 KB
testcase_46 AC 612 ms
67,520 KB
testcase_47 AC 695 ms
70,440 KB
testcase_48 AC 642 ms
70,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Queue;

public class Main {
	public static void main(String[] args) throws Exception {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String[] sa = br.readLine().split(" ");
		int n = Integer.parseInt(sa[0]);
		int m = Integer.parseInt(sa[1]);
		Hen[] arr = new Hen[m];
		for (int i = 0; i < m; i++) {
			sa = br.readLine().split(" ");
			Hen h = new Hen();
			h.s = Integer.parseInt(sa[0]) - 1;
			h.t = Integer.parseInt(sa[1]) - 1;
			h.d = Integer.parseInt(sa[2]);
			arr[i] = h;
		}
		br.close();

		Arrays.sort(arr, (o1, o2) -> o2.d - o1.d);
		UnionFind uf = new UnionFind(n);
		List<List<Integer>> list = new ArrayList<>();
		int w = 0;
		for (int i = 0; i < n; i++) {
			list.add(new ArrayList<>());
		}
		for (int i = 0; i < m; i++) {
			Hen h = arr[i];
			uf.union(h.s, h.t);
			list.get(h.s).add(h.t);
			list.get(h.t).add(h.s);
			if (uf.num == 1) {
				w = h.d;
				break;
			}
		}

		Queue<Integer> que = new ArrayDeque<>();
		que.add(0);
		int[] d = new int[n];
		d[0] = 1;
		while (!que.isEmpty()) {
			int cur = que.poll();
			for (int i : list.get(cur)) {
				if (d[i] == 0) {
					que.add(i);
					d[i] = d[cur] + 1;
				}
			}
		}
		System.out.println(w + " " + (d[n - 1] - 1));
	}

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

	static class Hen {
		int s, t, d;
	}
}
0