結果

問題 No.1473 おでぶなおばけさん
ユーザー ks2mks2m
提出日時 2021-04-09 21:55:12
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,226 bytes
コンパイル時間 2,610 ms
コンパイル使用メモリ 78,248 KB
実行使用メモリ 77,600 KB
最終ジャッジ日時 2023-09-07 11:04:10
合計ジャッジ時間 31,464 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
52,628 KB
testcase_01 AC 82 ms
52,768 KB
testcase_02 AC 776 ms
76,976 KB
testcase_03 AC 628 ms
68,276 KB
testcase_04 AC 476 ms
65,508 KB
testcase_05 AC 308 ms
59,700 KB
testcase_06 AC 677 ms
69,792 KB
testcase_07 AC 775 ms
77,084 KB
testcase_08 AC 774 ms
76,512 KB
testcase_09 AC 799 ms
77,428 KB
testcase_10 AC 515 ms
61,120 KB
testcase_11 AC 520 ms
61,804 KB
testcase_12 AC 538 ms
61,896 KB
testcase_13 AC 390 ms
58,544 KB
testcase_14 AC 331 ms
60,300 KB
testcase_15 AC 458 ms
61,292 KB
testcase_16 AC 522 ms
58,872 KB
testcase_17 AC 178 ms
56,728 KB
testcase_18 AC 201 ms
56,924 KB
testcase_19 AC 442 ms
60,984 KB
testcase_20 AC 529 ms
65,408 KB
testcase_21 AC 540 ms
64,220 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 667 ms
73,696 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 637 ms
77,600 KB
testcase_32 AC 520 ms
68,756 KB
testcase_33 AC 489 ms
65,160 KB
testcase_34 AC 337 ms
62,972 KB
testcase_35 AC 332 ms
60,708 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 525 ms
62,188 KB
testcase_40 AC 510 ms
61,828 KB
testcase_41 AC 487 ms
61,236 KB
testcase_42 AC 493 ms
61,948 KB
testcase_43 AC 529 ms
66,984 KB
testcase_44 AC 521 ms
67,732 KB
testcase_45 AC 525 ms
67,808 KB
testcase_46 AC 657 ms
68,512 KB
testcase_47 AC 692 ms
70,144 KB
testcase_48 AC 630 ms
70,480 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.same(uf.find(0), uf.find(n - 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