結果
問題 | No.1576 織姫と彦星 |
ユーザー | 👑 Kazun |
提出日時 | 2021-06-29 17:32:15 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 666 ms / 2,000 ms |
コード長 | 4,887 bytes |
コンパイル時間 | 169 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 77,056 KB |
最終ジャッジ日時 | 2024-06-25 19:47:59 |
合計ジャッジ時間 | 16,771 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 45 ms
54,912 KB |
testcase_01 | AC | 48 ms
54,144 KB |
testcase_02 | AC | 47 ms
54,528 KB |
testcase_03 | AC | 45 ms
54,656 KB |
testcase_04 | AC | 45 ms
54,528 KB |
testcase_05 | AC | 45 ms
55,168 KB |
testcase_06 | AC | 55 ms
63,744 KB |
testcase_07 | AC | 200 ms
76,288 KB |
testcase_08 | AC | 159 ms
75,776 KB |
testcase_09 | AC | 58 ms
65,664 KB |
testcase_10 | AC | 453 ms
76,288 KB |
testcase_11 | AC | 59 ms
65,792 KB |
testcase_12 | AC | 299 ms
76,160 KB |
testcase_13 | AC | 544 ms
76,160 KB |
testcase_14 | AC | 375 ms
76,544 KB |
testcase_15 | AC | 368 ms
76,032 KB |
testcase_16 | AC | 462 ms
76,160 KB |
testcase_17 | AC | 371 ms
76,288 KB |
testcase_18 | AC | 113 ms
75,904 KB |
testcase_19 | AC | 159 ms
76,032 KB |
testcase_20 | AC | 373 ms
76,032 KB |
testcase_21 | AC | 348 ms
76,032 KB |
testcase_22 | AC | 410 ms
75,904 KB |
testcase_23 | AC | 99 ms
75,776 KB |
testcase_24 | AC | 101 ms
75,904 KB |
testcase_25 | AC | 483 ms
76,160 KB |
testcase_26 | AC | 183 ms
76,416 KB |
testcase_27 | AC | 65 ms
68,736 KB |
testcase_28 | AC | 426 ms
76,032 KB |
testcase_29 | AC | 186 ms
76,032 KB |
testcase_30 | AC | 55 ms
63,616 KB |
testcase_31 | AC | 197 ms
75,904 KB |
testcase_32 | AC | 359 ms
76,288 KB |
testcase_33 | AC | 611 ms
77,056 KB |
testcase_34 | AC | 61 ms
66,304 KB |
testcase_35 | AC | 234 ms
76,032 KB |
testcase_36 | AC | 95 ms
76,032 KB |
testcase_37 | AC | 666 ms
76,800 KB |
testcase_38 | AC | 432 ms
76,160 KB |
testcase_39 | AC | 277 ms
76,160 KB |
testcase_40 | AC | 214 ms
76,416 KB |
testcase_41 | AC | 241 ms
76,288 KB |
testcase_42 | AC | 68 ms
71,424 KB |
testcase_43 | AC | 115 ms
76,032 KB |
testcase_44 | AC | 597 ms
76,416 KB |
testcase_45 | AC | 205 ms
76,032 KB |
testcase_46 | AC | 402 ms
76,672 KB |
testcase_47 | AC | 119 ms
75,776 KB |
testcase_48 | AC | 258 ms
76,416 KB |
testcase_49 | AC | 247 ms
76,416 KB |
testcase_50 | AC | 66 ms
68,352 KB |
testcase_51 | AC | 98 ms
75,648 KB |
testcase_52 | AC | 581 ms
76,288 KB |
testcase_53 | AC | 371 ms
76,544 KB |
testcase_54 | AC | 468 ms
76,672 KB |
testcase_55 | AC | 576 ms
76,416 KB |
testcase_56 | AC | 634 ms
76,416 KB |
testcase_57 | AC | 56 ms
64,896 KB |
testcase_58 | AC | 172 ms
76,672 KB |
ソースコード
class Graph: #入力定義 def __init__(self,vertex=[]): self.vertex=set(vertex) self.edge_number=0 self.adjacent={v:set() for v in vertex} #頂点の追加 def add_vertex(self,*adder): for v in adder: if not self.vertex_exist(v): self.adjacent[v]=set() self.vertex.add(v) #辺の追加 def add_edge(self,u,v): self.add_vertex(u) self.add_vertex(v) if not self.edge_exist(u,v): self.adjacent[u].add(v) self.adjacent[v].add(u) self.edge_number+=1 #辺を除く def remove_edge(self,u,v): self.add_vertex(u) self.add_vertex(v) if self.edge_exist(u,v): self.adjacent[u].discard(v) self.adjacent[v].discard(u) self.edge_number-=1 #頂点を除く def remove_vertex(self,*vertexes): for v in vertexes: if self.vertex_exist(v): U=self.neighbohood(v) for u in U: self.edge_number-=1 self.adjacent[u].discard(v) del self.adjacent[v] self.vertex.discard(v) #Walkの追加 def add_walk(self,*walk): n=len(walk) for i in range(n-1): self.add_edge(walk[i],walk[i+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,u,v): if not(self.vertex_exist(u) and self.vertex_exist(v)): return False return v in self.adjacent[u] #近傍 def neighbohood(self,v): if not self.vertex_exist(v): return [] return list(self.adjacent[v]) #次数 def degree(self,v): if not self.vertex_exist(v): return 0 return len(self.adjacent[v]) #頂点数 def vertex_count(self): return len(self.vertex) #辺数 def edge_count(self): return self.edge_number #頂点vを含む連結成分 def connected_component(self,v): if v not in self.adjacent: return [] from collections import deque T={u:0 for u in self.vertex} T[v]=1 Q=deque([v]) while Q: u=Q.popleft() for w in self.adjacent[u]: if not T[w]: T[w]=1 Q.append(w) return [x for x in self.adjacent if T[x]] #距離 def distance(self,u,v): from collections import deque inf=float("inf") T={v:inf for v in self.vertex} if u==v: return 0 Q=deque([u]) T[u]=0 while Q: w=Q.popleft() for x in self.adjacent[w]: if T[x]==inf: T[x]=T[w]+1 Q.append(x) if x==v: return T[x] return inf #ある1点からの距離 def distance_all(self,u): from collections import deque inf=float("inf") T={v:inf for v in self.vertex} Q=deque([u]) T[u]=0 while Q: w=Q.popleft() for x in self.adjacent[w]: if T[x]==inf: T[x]=T[w]+1 Q.append(x) return T #最短路 def shortest_path(self,u,v): from collections import deque inf=float("inf") T={v:None for v in self.vertex} if u==v: return [u] Q=deque([u]) T[u]=u while Q: w=Q.popleft() for x in self.adjacent[w]: if not T[x]: T[x]=w Q.append(x) if x==v: P=[v] a=v while a!=u: a=T[a] P.append(a) return P[::-1] return None #何かしらの頂点を選ぶ. def poping_vertex(self): v=self.vertex.pop() self.vertex.add(v) return v #================================================== def popcount(x): return bin(x).count("1") def Hamming(x,y): return popcount(x^y) #================================================== N=int(input()) S,G=map(int,input().split()) A=list(map(int,input().split())) V=[S,G]+A F=Graph(V) for a in F.vertex: for b in F.vertex: if Hamming(a,b)==1: F.add_edge(a,b) print(F.distance(S,G)-1 if F.distance(S,G)<float("inf") else -1)