結果

問題 No.1473 おでぶなおばけさん
ユーザー ks2mks2m
提出日時 2021-04-09 22:02:05
言語 Java19
(openjdk 21)
結果
AC  
実行時間 834 ms / 2,000 ms
コード長 2,242 bytes
コンパイル時間 2,892 ms
コンパイル使用メモリ 78,024 KB
実行使用メモリ 77,444 KB
最終ジャッジ日時 2023-09-07 11:19:21
合計ジャッジ時間 31,653 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
52,640 KB
testcase_01 AC 86 ms
52,424 KB
testcase_02 AC 796 ms
77,444 KB
testcase_03 AC 662 ms
69,032 KB
testcase_04 AC 507 ms
66,148 KB
testcase_05 AC 319 ms
58,604 KB
testcase_06 AC 712 ms
69,600 KB
testcase_07 AC 810 ms
77,436 KB
testcase_08 AC 834 ms
77,376 KB
testcase_09 AC 800 ms
77,096 KB
testcase_10 AC 542 ms
61,144 KB
testcase_11 AC 538 ms
61,460 KB
testcase_12 AC 549 ms
61,356 KB
testcase_13 AC 433 ms
58,796 KB
testcase_14 AC 346 ms
58,924 KB
testcase_15 AC 496 ms
61,216 KB
testcase_16 AC 524 ms
60,976 KB
testcase_17 AC 189 ms
56,624 KB
testcase_18 AC 216 ms
57,292 KB
testcase_19 AC 485 ms
60,896 KB
testcase_20 AC 564 ms
65,736 KB
testcase_21 AC 597 ms
63,144 KB
testcase_22 AC 585 ms
71,632 KB
testcase_23 AC 541 ms
69,512 KB
testcase_24 AC 523 ms
70,356 KB
testcase_25 AC 597 ms
69,492 KB
testcase_26 AC 632 ms
69,792 KB
testcase_27 AC 348 ms
60,820 KB
testcase_28 AC 731 ms
76,280 KB
testcase_29 AC 531 ms
65,680 KB
testcase_30 AC 578 ms
65,756 KB
testcase_31 AC 696 ms
76,368 KB
testcase_32 AC 568 ms
68,812 KB
testcase_33 AC 531 ms
66,984 KB
testcase_34 AC 358 ms
62,784 KB
testcase_35 AC 345 ms
60,712 KB
testcase_36 AC 491 ms
65,364 KB
testcase_37 AC 578 ms
68,364 KB
testcase_38 AC 250 ms
57,248 KB
testcase_39 AC 549 ms
61,108 KB
testcase_40 AC 547 ms
61,116 KB
testcase_41 AC 516 ms
61,124 KB
testcase_42 AC 532 ms
60,924 KB
testcase_43 AC 534 ms
68,360 KB
testcase_44 AC 521 ms
67,272 KB
testcase_45 AC 524 ms
67,700 KB
testcase_46 AC 622 ms
70,064 KB
testcase_47 AC 718 ms
70,380 KB
testcase_48 AC 656 ms
70,444 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];
			if (w != 0 && h.d != w) {
				break;
			}
			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;
			}
		}

		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