結果

問題 No.416 旅行会社
ユーザー 37zigen37zigen
提出日時 2016-08-24 19:08:59
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 2,608 bytes
コンパイル時間 3,686 ms
コンパイル使用メモリ 80,896 KB
実行使用メモリ 147,384 KB
最終ジャッジ日時 2024-04-25 15:07:36
合計ジャッジ時間 35,950 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,768 ms
112,268 KB
testcase_01 AC 126 ms
54,192 KB
testcase_02 AC 126 ms
54,000 KB
testcase_03 AC 130 ms
53,992 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 1,797 ms
110,324 KB
testcase_11 AC 1,800 ms
111,252 KB
testcase_12 AC 1,692 ms
109,912 KB
testcase_13 AC 1,826 ms
111,728 KB
testcase_14 AC 2,570 ms
147,384 KB
testcase_15 AC 2,564 ms
146,280 KB
testcase_16 AC 2,686 ms
144,192 KB
testcase_17 AC 2,615 ms
134,544 KB
testcase_18 AC 2,597 ms
140,416 KB
testcase_19 WA -
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

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], 0));
			g[B[i]].add(new Edge(B[i], A[i], 0));
		}

		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();
			checked[v] = true;
			for (Edge e : g[v]) {
				if (checked[e.dst])
					continue;
				if (e.event_time == 0) {
					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 || e.event_time == 0) {
							que.add(e.dst);
						} else {
							pending[e.event_time].add(e.dst);
						}
					}
				}
			}
		}
		for (int i = 1; i < N; i++) {
			if (d[i] == 0) {
				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