結果

問題 No.3 ビットすごろく
ユーザー toyuzukotoyuzuko
提出日時 2020-07-09 22:39:06
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 70 ms / 5,000 ms
コード長 1,519 bytes
コンパイル時間 91 ms
コンパイル使用メモリ 10,916 KB
実行使用メモリ 14,660 KB
最終ジャッジ日時 2023-09-14 01:47:44
合計ジャッジ時間 2,828 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
8,756 KB
testcase_01 AC 18 ms
8,672 KB
testcase_02 AC 22 ms
8,684 KB
testcase_03 AC 34 ms
10,084 KB
testcase_04 AC 25 ms
9,032 KB
testcase_05 AC 45 ms
11,712 KB
testcase_06 AC 32 ms
10,084 KB
testcase_07 AC 26 ms
9,352 KB
testcase_08 AC 39 ms
11,184 KB
testcase_09 AC 52 ms
12,696 KB
testcase_10 AC 59 ms
13,532 KB
testcase_11 AC 49 ms
12,332 KB
testcase_12 AC 44 ms
11,616 KB
testcase_13 AC 28 ms
9,720 KB
testcase_14 AC 58 ms
13,404 KB
testcase_15 AC 70 ms
14,572 KB
testcase_16 AC 65 ms
14,072 KB
testcase_17 AC 68 ms
14,412 KB
testcase_18 AC 28 ms
9,564 KB
testcase_19 AC 69 ms
14,552 KB
testcase_20 AC 22 ms
8,992 KB
testcase_21 AC 20 ms
8,736 KB
testcase_22 AC 59 ms
13,496 KB
testcase_23 AC 70 ms
14,660 KB
testcase_24 AC 69 ms
14,584 KB
testcase_25 AC 69 ms
14,520 KB
testcase_26 AC 20 ms
8,764 KB
testcase_27 AC 30 ms
9,788 KB
testcase_28 AC 63 ms
13,952 KB
testcase_29 AC 51 ms
12,484 KB
testcase_30 AC 21 ms
8,656 KB
testcase_31 AC 20 ms
8,848 KB
testcase_32 AC 49 ms
11,916 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

class Graph(): #directed
    def __init__(self, n, edge, indexed=1):
        self.n = n
        self.graph = [[] for _ in range(n)]
        self.rev = [[] for _ in range(n)]
        self.deg = [0 for _ in range(n)]
        for e in edge:
            self.graph[e[0] - indexed].append(e[1] - indexed)
            self.rev[e[1] - indexed].append(e[0] - indexed)
            self.deg[e[1] - indexed] += 1

    def BFS(self,s, restore_to=None): #sを始点とした最短経路
        dist = [None for _ in range(self.n)]
        dist[s] = 0
        queue = deque([s])
        prev = [None for _ in range(self.n)]
        while queue:
            node = queue.popleft()
            for adj in self.graph[node]:
                if dist[adj] is not None:
                    continue
                dist[adj] = dist[node] + 1
                queue.append(adj)
        if restore_to is not None:
            path = [restore_to]
            node = restore_to
            while node != s:
                node = prev[node]
                path.append(node)
            return dist, path[::-1]
        return dist

import sys
input = sys.stdin.readline

N = int(input())
E = []

for i in range(1, N + 1):
    count = 0
    for j in range(i.bit_length()):
        if (i >> j) & 1:
            count += 1
    if i - count >= 1:
        E.append((i, i - count))
    if i + count <= N:
        E.append((i, i + count))

g = Graph(N, E)
res = g.BFS(0)[N - 1]

print(res + 1 if res is not None else -1)
0