結果

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

ソースコード

diff #

from collections import deque

N,M = map(int,input().split()); INF = float("inf")
A = list(map(int,input().split()))
G = [[] for _ in range(M)]
deg1 = [0] * M #出次数
deg2 = [0] * M #入次数
for i in range(N-1):
    a = A[i] - 1 #0index
    b = A[i+1] - 1
    if a == b:
        print("No");exit()
    if i < N-2:
        c = A[i+2] - 1
        if a == c:
            print("No");exit()
    if i%2 == 0:
        G[a].append(b)
        deg1[a] += 1
        deg2[b] += 1
    else:
        G[b].append(a)
        deg1[b] += 1
        deg2[a] += 1
#print(G)

def topological(start,graph):
    topo = []
    while start:
        v = start.pop()
        topo.append(v)
        for u in graph[v]:
            deg2[u] -= 1
            if deg2[u] == 0:
                start.append(u)
    # print(edges, minscore)
 
    return topo

SG = []
used = [0] * M
start = []
for i in range(M):
    if deg2[i] == 0:
        SG.append(i)
#print(SG)
TS = topological(SG,G)
#print(TS)
if len(TS) < M:
    print("No")
else:
    ans = [-1]*M
    for i, v in enumerate(TS):
        ans[v] = i + 1
    print("Yes")
    print(*ans)
0