結果

問題 No.1576 織姫と彦星
ユーザー rlangevin
提出日時 2023-01-01 02:34:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 120 ms / 2,000 ms
コード長 867 bytes
コンパイル時間 360 ms
コンパイル使用メモリ 82,468 KB
実行使用メモリ 76,416 KB
最終ジャッジ日時 2024-11-27 00:00:53
合計ジャッジ時間 6,477 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 5
other AC * 54
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


def bfs(G, s, N):
    Q = deque([])
    dist = [-1] * N
    par = [-1] * N
    dist[s] = 0
    for u in G[s]:
        par[u] = s
        dist[u] = 1
        Q.append(u)

    while Q:
        u = Q.popleft()
        for v in G[u]:
            if dist[v] != -1:
                continue
            dist[v] = dist[u] + 1
            par[v] = u
            Q.append(v)
            
    return dist

def popcount(n):
    cnt = 0
    while n:
        cnt += n & 1
        n //= 2
    return cnt


N = int(input())
S = list(map(int, input().split()))
S.extend(list(map(int, input().split())))
N += 2
G = [[] for i in range(N)]
for i in range(N):
    for j in range(i + 1, N):
        if popcount(S[i]^S[j]) == 1:
            G[i].append(j)
            G[j].append(i)
            
D = bfs(G, 0, N)
print(D[1] - 1) if D[1] != -1 else print(-1)
0