結果

問題 No.679 不思議マーケット
ユーザー GrenacheGrenache
提出日時 2018-04-27 23:35:04
言語 Java19
(openjdk 21)
結果
AC  
実行時間 293 ms / 2,000 ms
コード長 2,569 bytes
コンパイル時間 3,754 ms
コンパイル使用メモリ 76,540 KB
実行使用メモリ 60,100 KB
最終ジャッジ日時 2023-09-10 06:55:32
合計ジャッジ時間 8,292 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
55,808 KB
testcase_01 AC 128 ms
55,496 KB
testcase_02 AC 130 ms
56,632 KB
testcase_03 AC 293 ms
60,100 KB
testcase_04 AC 228 ms
58,900 KB
testcase_05 AC 239 ms
58,984 KB
testcase_06 AC 213 ms
58,056 KB
testcase_07 AC 200 ms
57,912 KB
testcase_08 AC 241 ms
59,916 KB
testcase_09 AC 129 ms
55,916 KB
testcase_10 AC 204 ms
59,000 KB
testcase_11 AC 200 ms
58,124 KB
testcase_12 AC 208 ms
58,964 KB
testcase_13 AC 211 ms
58,560 KB
testcase_14 AC 211 ms
59,040 KB
testcase_15 AC 204 ms
58,292 KB
testcase_16 AC 133 ms
55,496 KB
testcase_17 AC 132 ms
55,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


public class Main_yukicoder679 {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
		int n = sc.nextInt();
		int m = sc.nextInt();

		List<List<Integer>> edges = new ArrayList<>(n);
		int[] cnt = new int[n];
		for (int i = 0; i < n; i++) {
			edges.add(new ArrayList<>());
		}
		for (int i = 0; i < m; i++) {
			int g = sc.nextInt() - 1;
			int r = sc.nextInt();

			for (int j = 0; j < r; j++) {
				int h = sc.nextInt() - 1;

				edges.get(h).add(g);
				cnt[g]++;
			}
		}

		int[] d = new int[n];
		int[] f = new int[n];
		int clock = 1;

		List<Integer> bt = new ArrayList<>();
		int[] used = new int[n];  // 0:White, 1:Gray, 2:Black
		for (int i = 0; i < n; i++) {
			if (used[i] != 0) {
				continue;
			}

			Deque<Integer> st = new ArrayDeque<>();
			st.push(i);
			while (!st.isEmpty()) {
				int e = st.peek();

				if (used[e] == 2) {
					st.pop();
				} else if (used[e] == 1) {
					// postorder
					f[e] = clock++;
					used[e] = 2;
					st.pop();
				} else {
					// preorder
					d[e] = clock++;
					used[e] = 1;
					for (int next : edges.get(e)) {
						if (used[next] == 2) {
							// 前進辺または横断辺
							continue;
						} else if (used[next] == 1) {
							// 後退辺
							bt.add(next);
							continue;
						}

						st.push(next);
					}
				}
			}
		}

		int[] used2 = new int[n];
		for (int i : bt) {
			if (used2[i] != 0) {
				continue;
			}

			Deque<Integer> st = new ArrayDeque<>();
			st.push(i);
			while (!st.isEmpty()) {
				int e = st.peek();

				if (used2[e] == 2) {
					st.pop();
				} else if (used2[e] == 1) {
					// postorder
					f[e] = clock++;
					used2[e] = 2;
					st.pop();
				} else {
					// preorder
					d[e] = clock++;
					used2[e] = 1;
					for (int next : edges.get(e)) {
						if (used2[next] == 2) {
							// 前進辺または横断辺
							continue;
						} else if (used2[next] == 1) {
							// 後退辺
							continue;
						}

						st.push(next);
					}
				}
			}
		}

		int ans = n;
		for (int ff : used2) {
			if (ff > 0) {
				ans--;
			}
		}

		pr.println(ans);
	}

	// ---------------------------------------------------
	public static void main(String[] args) {
		sc = new Scanner(INPUT == null ? System.in : new ByteArrayInputStream(INPUT.getBytes()));
		pr = new Printer(System.out);

		solve();

//		pr.close();
		pr.flush();
//		sc.close();
	}

	static String INPUT = null;

	private static class Printer extends PrintWriter {
		Printer(OutputStream out) {
			super(out);
		}
	}
}
0