結果
問題 | No.1370 置換門松列 |
ユーザー | 👑 Kazun |
提出日時 | 2021-01-29 22:55:58 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 377 ms / 2,000 ms |
コード長 | 4,214 bytes |
コンパイル時間 | 170 ms |
コンパイル使用メモリ | 82,140 KB |
実行使用メモリ | 135,892 KB |
最終ジャッジ日時 | 2024-09-14 19:22:39 |
合計ジャッジ時間 | 4,524 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 44 ms
54,912 KB |
testcase_01 | AC | 40 ms
53,788 KB |
testcase_02 | AC | 44 ms
54,924 KB |
testcase_03 | AC | 44 ms
56,640 KB |
testcase_04 | AC | 41 ms
53,376 KB |
testcase_05 | AC | 44 ms
55,268 KB |
testcase_06 | AC | 40 ms
52,908 KB |
testcase_07 | AC | 44 ms
55,144 KB |
testcase_08 | AC | 45 ms
54,324 KB |
testcase_09 | AC | 40 ms
53,116 KB |
testcase_10 | AC | 43 ms
55,332 KB |
testcase_11 | AC | 41 ms
54,044 KB |
testcase_12 | AC | 40 ms
53,232 KB |
testcase_13 | AC | 40 ms
53,408 KB |
testcase_14 | AC | 38 ms
52,624 KB |
testcase_15 | AC | 43 ms
54,944 KB |
testcase_16 | AC | 43 ms
55,536 KB |
testcase_17 | AC | 44 ms
55,128 KB |
testcase_18 | AC | 40 ms
54,096 KB |
testcase_19 | AC | 40 ms
54,232 KB |
testcase_20 | AC | 47 ms
55,412 KB |
testcase_21 | AC | 260 ms
135,892 KB |
testcase_22 | AC | 214 ms
127,852 KB |
testcase_23 | AC | 217 ms
123,632 KB |
testcase_24 | AC | 96 ms
88,280 KB |
testcase_25 | AC | 377 ms
135,536 KB |
testcase_26 | AC | 374 ms
135,732 KB |
testcase_27 | AC | 220 ms
126,220 KB |
testcase_28 | AC | 221 ms
126,472 KB |
testcase_29 | AC | 134 ms
89,528 KB |
ソースコード
class Digraph: """重み[なし]有向グラフを生成する. """ #入力定義 def __init__(self,vertex=[]): self.vertex=set(vertex) self.edge_number=0 self.vertex_number=len(vertex) self.adjacent_out={v:set() for v in vertex} #出近傍(vが始点) self.adjacent_in={v:set() for v in vertex} #入近傍(vが終点) #頂点の追加 def add_vertex(self,*adder): for v in adder: if v not in self.vertex: self.adjacent_in[v]=set() self.adjacent_out[v]=set() self.vertex_number+=1 self.vertex.add(v) #辺の追加 def add_edge(self,From,To): for v in [From,To]: if v not in self.vertex: self.add_vertex(v) if To not in self.adjacent_in[From]: self.edge_number+=1 self.adjacent_out[From].add(To) self.adjacent_in[To].add(From) #辺を除く def remove_edge(self,From,To): for v in [From,To]: if v not in self.vertex: self.add_vertex(v) if To in self.adjacent_out[From]: self.adjacent_out[From].remove(To) self.adjacent_in[To].remove(From) self.edge_number-=1 #頂点を除く def remove_vertex(self,*vertexes): for v in vertexes: if v in self.vertex: self.vertex_number-=1 for u in self.adjacent_out[v]: self.adjacent_in[u].remove(v) self.edge_number-=1 del self.adjacent_out[v] for u in self.adjacent_in[v]: self.adjacent_out[u].remove(v) self.edge_number-=1 del self.adjacent_in[v] #Walkの追加 def add_walk(self,*walk): N=len(walk) for k in range(N-1): self.add_edge(walk[k],walk[k+1]) #Cycleの追加 def add_cycle(self,*cycle): self.add_walk(*cycle) self.add_edge(cycle[-1],cycle[0]) #頂点の交換 def __vertex_swap(self,p,q): self.vertex.sort() #グラフに頂点が存在するか否か def vertex_exist(self,v): return v in self.vertex #グラフに辺が存在するか否か def edge_exist(self,From,To): if not(self.vertex_exist(From) and self.vertex_exist(To)): return False return To in self.adjacent_out[From] #近傍 def neighbohood(self,v): if not self.vertex_exist(v): return [] return list(self.adjacent[v]) #出次数 def out_degree(self,v): if not self.vertex_exist(v): return 0 return len(self.adjacent_out[v]) #入次数 def in_degree(self,v): if not self.vertex_exist(v): return 0 return len(self.adjacent_in[v]) #次数 def degree(self,v): if not self.vertex_exist(v): return 0 return self.out_degree(v)-self.in_degree(v) #頂点数 def vertex_count(self): return len(self.vertex) #辺数 def edge_count(self): return self.edge_number #頂点vを含む連結成分 def connected_component(self,v): pass def Topological_Sort(D): from collections import deque X={v:D.in_degree(v) for v in D.vertex} Q=deque([v for v in D.vertex if X[v]==0]) S=[] while Q: u=Q.pop() S.append(u) for v in D.adjacent_out[u]: X[v]-=1 if X[v]==0: Q.append(v) return S #================================================ N,M=map(int,input().split()) A=list(map(int,input().split())) for i in range(N-1): if A[i]==A[i+1]: print("No") exit() for i in range(N-2): if A[i]==A[i+2]: print("No") exit() Mode=0 D=Digraph(range(1,M+1)) for i in range(N-1): if Mode: D.add_edge(A[i],A[i+1]) else: D.add_edge(A[i+1],A[i]) Mode^=1 T=Topological_Sort(D) if len(T)==M: print("Yes") X=[0]*(M+1) for i,v in enumerate(T,1): X[v]=i print(" ".join(map(str,X[1:]))) else: print("No")