結果

問題 No.1370 置換門松列
ユーザー さかぽんさかぽん
提出日時 2021-02-18 16:35:28
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 1,441 bytes
コンパイル時間 935 ms
コンパイル使用メモリ 115,340 KB
実行使用メモリ 58,228 KB
最終ジャッジ日時 2024-04-25 17:07:13
合計ジャッジ時間 4,874 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 25 ms
23,092 KB
testcase_02 AC 31 ms
26,996 KB
testcase_03 AC 33 ms
25,132 KB
testcase_04 AC 27 ms
24,368 KB
testcase_05 AC 34 ms
27,432 KB
testcase_06 AC 26 ms
24,496 KB
testcase_07 AC 34 ms
25,384 KB
testcase_08 AC 35 ms
27,428 KB
testcase_09 AC 26 ms
24,748 KB
testcase_10 WA -
testcase_11 AC 26 ms
24,624 KB
testcase_12 AC 27 ms
26,848 KB
testcase_13 AC 26 ms
26,724 KB
testcase_14 AC 26 ms
24,756 KB
testcase_15 WA -
testcase_16 AC 33 ms
25,008 KB
testcase_17 AC 32 ms
27,216 KB
testcase_18 AC 27 ms
26,716 KB
testcase_19 AC 27 ms
26,728 KB
testcase_20 AC 32 ms
25,260 KB
testcase_21 WA -
testcase_22 AC 121 ms
53,512 KB
testcase_23 WA -
testcase_24 AC 76 ms
43,700 KB
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 120 ms
53,080 KB
testcase_28 AC 120 ms
52,952 KB
testcase_29 AC 87 ms
43,368 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Linq;

class E
{
	static int[] Read() => Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
	static (int, int) Read2() { var a = Read(); return (a[0], a[1]); }
	static void Main() => Console.WriteLine(Solve());
	static object Solve()
	{
		var (n, m) = Read2();
		var a = Read();

		for (int i = 1; i < n; i++)
			if (a[i - 1] == a[i]) return "No";
		for (int i = 2; i < n; i++)
			if (a[i - 2] == a[i]) return "No";

		var es = new List<int[]>();

		for (int i = 1; i < n; i += 2)
			es.Add(new[] { a[i - 1], a[i] });
		for (int i = 2; i < n; i += 2)
			es.Add(new[] { a[i], a[i - 1] });

		var ts = TopologicalSort(m + 1, es.ToArray());
		if (ts == null) return "No";

		return "Yes\n" + string.Join(" ", ts.Skip(1));
	}

	static int[] TopologicalSort(int n, int[][] des)
	{
		var map = Array.ConvertAll(new bool[n], _ => new List<int[]>());
		var indeg = new int[n];
		foreach (var e in des)
		{
			map[e[0]].Add(e);
			++indeg[e[1]];
		}

		var r = new List<int>();
		var q = new Queue<int>();
		var svs = Enumerable.Range(0, n).Where(v => indeg[v] == 0).ToArray();

		foreach (var sv in svs)
		{
			r.Add(sv);
			q.Enqueue(sv);

			while (q.Count > 0)
			{
				var v = q.Dequeue();
				foreach (var e in map[v])
				{
					if (--indeg[e[1]] > 0) continue;
					r.Add(e[1]);
					q.Enqueue(e[1]);
				}
			}
		}
		if (r.Count < n) return null;
		return r.ToArray();
	}
}
0