結果

問題 No.1473 おでぶなおばけさん
ユーザー ks2mks2m
提出日時 2021-04-09 21:54:00
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,208 bytes
コンパイル時間 4,349 ms
コンパイル使用メモリ 77,584 KB
実行使用メモリ 77,456 KB
最終ジャッジ日時 2023-09-07 11:01:19
合計ジャッジ時間 29,735 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
51,324 KB
testcase_01 AC 83 ms
52,728 KB
testcase_02 AC 735 ms
72,864 KB
testcase_03 AC 586 ms
68,340 KB
testcase_04 AC 444 ms
64,964 KB
testcase_05 AC 264 ms
57,592 KB
testcase_06 AC 628 ms
69,224 KB
testcase_07 AC 723 ms
76,640 KB
testcase_08 AC 761 ms
76,932 KB
testcase_09 AC 744 ms
77,456 KB
testcase_10 AC 516 ms
62,956 KB
testcase_11 AC 509 ms
62,804 KB
testcase_12 AC 543 ms
60,880 KB
testcase_13 AC 402 ms
59,428 KB
testcase_14 AC 330 ms
58,388 KB
testcase_15 AC 464 ms
60,828 KB
testcase_16 AC 507 ms
61,120 KB
testcase_17 AC 173 ms
56,832 KB
testcase_18 AC 202 ms
57,028 KB
testcase_19 AC 446 ms
61,324 KB
testcase_20 AC 517 ms
63,592 KB
testcase_21 AC 521 ms
63,124 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 635 ms
72,476 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 611 ms
75,872 KB
testcase_32 AC 492 ms
69,136 KB
testcase_33 AC 452 ms
67,528 KB
testcase_34 AC 314 ms
62,808 KB
testcase_35 AC 323 ms
60,580 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 498 ms
61,312 KB
testcase_40 AC 506 ms
60,944 KB
testcase_41 AC 475 ms
60,932 KB
testcase_42 AC 467 ms
61,104 KB
testcase_43 AC 479 ms
67,808 KB
testcase_44 AC 479 ms
66,992 KB
testcase_45 AC 474 ms
67,296 KB
testcase_46 AC 587 ms
67,524 KB
testcase_47 AC 663 ms
70,236 KB
testcase_48 AC 605 ms
69,968 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(0, 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