結果

問題 No.1370 置換門松列
ユーザー ayaoni
提出日時 2021-01-30 09:46:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 157 ms / 2,000 ms
コード長 1,548 bytes
コンパイル時間 347 ms
コンパイル使用メモリ 82,452 KB
実行使用メモリ 101,376 KB
最終ジャッジ日時 2024-09-14 19:24:30
合計ジャッジ時間 3,704 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

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