結果

問題 No.416 旅行会社
ユーザー 37zigen37zigen
提出日時 2016-08-24 19:16:05
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,507 ms / 4,000 ms
コード長 2,620 bytes
コンパイル時間 3,666 ms
コンパイル使用メモリ 76,300 KB
実行使用メモリ 138,996 KB
最終ジャッジ日時 2023-08-21 09:39:14
合計ジャッジ時間 32,719 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,591 ms
108,320 KB
testcase_01 AC 123 ms
55,960 KB
testcase_02 AC 124 ms
56,096 KB
testcase_03 AC 127 ms
55,868 KB
testcase_04 AC 135 ms
55,696 KB
testcase_05 AC 142 ms
56,092 KB
testcase_06 AC 190 ms
56,768 KB
testcase_07 AC 221 ms
59,200 KB
testcase_08 AC 302 ms
60,896 KB
testcase_09 AC 531 ms
63,992 KB
testcase_10 AC 1,630 ms
109,316 KB
testcase_11 AC 1,478 ms
111,092 KB
testcase_12 AC 1,494 ms
104,144 KB
testcase_13 AC 1,474 ms
108,276 KB
testcase_14 AC 2,427 ms
135,828 KB
testcase_15 AC 2,368 ms
135,948 KB
testcase_16 AC 2,507 ms
137,432 KB
testcase_17 AC 2,470 ms
138,996 KB
testcase_18 AC 2,394 ms
136,252 KB
testcase_19 AC 1,947 ms
108,728 KB
testcase_20 AC 1,947 ms
105,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package No400番台;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Q416 {
	public static void main(String[] args) {
		new Q416().solver();
	}

	@SuppressWarnings("unchecked")
	void solver() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();// the number of islands
		int M = sc.nextInt();// the number of bridges
		int Q = sc.nextInt();// the number of events
		ArrayList<Edge>[] g = new ArrayList[N];
		for (int i = 0; i < N; i++) {
			g[i] = new ArrayList<>();
		}
		ArrayList<Integer>[] ref = new ArrayList[N];
		for (int i = 0; i < N; i++) {
			ref[i] = new ArrayList<>();
		}
		int[] A = new int[M];
		int[] B = new int[M];
		for (int i = 0; i < M; i++) {
			A[i] = sc.nextInt() - 1;
			B[i] = sc.nextInt() - 1;
		}
		int[] C = new int[Q];
		int[] D = new int[Q];
		for (int i = 0; i < Q; i++) {
			C[i] = sc.nextInt() - 1;
			D[i] = sc.nextInt() - 1;
			ref[C[i]].add(D[i]);
			ref[D[i]].add(C[i]);
			g[C[i]].add(new Edge(C[i], D[i], i + 1));
			g[D[i]].add(new Edge(D[i], C[i], i + 1));
		}
		for (int i = 0; i < M; i++) {
			if (ref[A[i]].contains(B[i]))
				continue;
			g[A[i]].add(new Edge(A[i], B[i], Q + 1));
			g[B[i]].add(new Edge(B[i], A[i], Q + 1));
		}

		ArrayDeque<Integer> que = new ArrayDeque<>();
		ArrayDeque<Integer>[] pending = new ArrayDeque[Q + 1];
		for (int i = 0; i < Q + 1; i++)
			pending[i] = new ArrayDeque<>();
		que.add(0);
		int[] d = new int[N];
		boolean[] checked = new boolean[N];
		while (!que.isEmpty()) {
			int v = que.poll();
			d[v] = Q + 1;
			checked[v] = true;
			for (Edge e : g[v]) {
				if (checked[e.dst])
					continue;
				if (e.event_time == Q + 1) {
					que.add(e.dst);
				} else {
					pending[e.event_time].add(e.dst);
				}
			}
		}
		for (int i = Q; i >= 1; i--) {
			while (!pending[i].isEmpty()) {
				que.add(pending[i].poll());
				while (!que.isEmpty()) {
					int v = que.poll();
					if (checked[v])
						continue;
					d[v] = i;
					checked[v] = true;
					for (Edge e : g[v]) {
						if (checked[e.dst])
							continue;
						if (e.event_time >= i) {
							que.add(e.dst);
						} else {
							pending[e.event_time].add(e.dst);
						}
					}
				}
			}
		}
		for (int i = 1; i < N; i++) {
			if (d[i] == Q + 1) {
				System.out.println(-1);
			} else {
				System.out.println(d[i]);
			}
		}
	}

	void tr(Object... o) {
		System.out.println(Arrays.deepToString(o));
	}

	class Edge {
		int src;
		int dst;
		int event_time;

		Edge(int src, int dst, int event_time) {
			this.src = src;
			this.dst = dst;
			this.event_time = event_time;
		}
	}
}
0