結果

問題 No.1370 置換門松列
ユーザー ayaoniayaoni
提出日時 2021-01-30 09:46:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 192 ms / 2,000 ms
コード長 1,548 bytes
コンパイル時間 256 ms
コンパイル使用メモリ 87,004 KB
実行使用メモリ 108,648 KB
最終ジャッジ日時 2023-10-12 21:05:36
合計ジャッジ時間 5,331 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 93 ms
71,200 KB
testcase_01 AC 93 ms
71,440 KB
testcase_02 AC 92 ms
71,540 KB
testcase_03 AC 93 ms
71,624 KB
testcase_04 AC 94 ms
71,424 KB
testcase_05 AC 94 ms
71,312 KB
testcase_06 AC 94 ms
71,576 KB
testcase_07 AC 91 ms
71,620 KB
testcase_08 AC 92 ms
71,532 KB
testcase_09 AC 92 ms
71,548 KB
testcase_10 AC 93 ms
71,508 KB
testcase_11 AC 93 ms
71,468 KB
testcase_12 AC 92 ms
71,248 KB
testcase_13 AC 92 ms
71,496 KB
testcase_14 AC 92 ms
71,508 KB
testcase_15 AC 90 ms
71,504 KB
testcase_16 AC 92 ms
71,204 KB
testcase_17 AC 91 ms
71,208 KB
testcase_18 AC 93 ms
71,372 KB
testcase_19 AC 91 ms
71,392 KB
testcase_20 AC 91 ms
71,452 KB
testcase_21 AC 175 ms
104,336 KB
testcase_22 AC 141 ms
102,016 KB
testcase_23 AC 174 ms
108,648 KB
testcase_24 AC 119 ms
92,344 KB
testcase_25 AC 192 ms
104,248 KB
testcase_26 AC 189 ms
104,360 KB
testcase_27 AC 146 ms
104,880 KB
testcase_28 AC 146 ms
104,836 KB
testcase_29 AC 120 ms
90,532 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque
sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())


N,M = MI()
A = LI()

for i in range(N-2):
    if A[i] == A[i+2] or A[i] == A[i+1] or A[i+1] == A[i+2]:
        exit(print('No'))

Graph = [[] for _ in range(M+1)]
in_deg = [0]*(M+1)  # 入次数
for i in range(N-1):
    if i % 2 == 0:
        Graph[A[i]].append(A[i+1])
        in_deg[A[i+1]] += 1
    else:
        Graph[A[i+1]].append(A[i])
        in_deg[A[i]] += 1


def Topological_Sort(N,Graph,in_deg):
    """
    Nは頂点数,Graphはグラフの隣接リスト表現,in_degは入次数(1-based)
    len(t_sort) < N なら有向サイクルが存在し、トポロジカルソート不可能
    """
    t_sort = []
    deq = deque([i for i in range(1,N+1) if in_deg[i] == 0])
    while deq:
        u = deq.pop()
        t_sort.append(u)
        for v in Graph[u]:
            in_deg[v] -= 1
            if in_deg[v] == 0:
                deq.append(v)
    return t_sort


T = Topological_Sort(M,Graph,in_deg)

if len(T) == M:
    print('Yes')
    ANS = [0]*(M+1)
    for i in range(1,M+1):
        ANS[T[i-1]] = i
    print(*ANS[1:])
else:
    print('No')
0