結果

問題 No.1400 すごろくで世界旅行
ユーザー lam6er
提出日時 2025-03-20 21:11:48
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,905 bytes
コンパイル時間 191 ms
コンパイル使用メモリ 82,508 KB
実行使用メモリ 170,780 KB
最終ジャッジ日時 2025-03-20 21:12:43
合計ジャッジ時間 6,478 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 10 TLE * 1 -- * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

def main():
    input = sys.stdin.read().split()
    idx = 0
    V, D = int(input[idx]), int(input[idx+1])
    idx += 2
    E = input[idx:idx+V]
    idx += V

    # Build adjacency list
    adj = [[] for _ in range(V)]
    for i in range(V):
        row = E[i]
        for j in range(V):
            if row[j] == '1':
                adj[i].append(j)

    # Check each vertex has at least one outgoing edge
    for neighbors in adj:
        if not neighbors:
            print("No")
            return

    # Check connectivity using BFS from vertex 0
    visited = [False] * V
    q = deque([0])
    visited[0] = True
    count = 1
    while q:
        u = q.popleft()
        for v in adj[u]:
            if not visited[v]:
                visited[v] = True
                count += 1
                q.append(v)
    if count != V:
        print("No")
        return

    # Check if bipartite using BFS
    is_bipartite = True
    color = [-1] * V
    color[0] = 0
    q = deque([0])
    while q and is_bipartite:
        u = q.popleft()
        for v in adj[u]:
            if color[v] == -1:
                color[v] = color[u] ^ 1
                q.append(v)
            elif color[v] == color[u]:
                is_bipartite = False
                break
    if is_bipartite:
        print("No")
        return

    # Compute maximum shortest path using BFS for each vertex
    max_k = 0
    for i in range(V):
        dist = [-1] * V
        q = deque([i])
        dist[i] = 0
        current_max = 0
        while q:
            u = q.popleft()
            current_max = max(current_max, dist[u])
            for v in adj[u]:
                if dist[v] == -1:
                    dist[v] = dist[u] + 1
                    q.append(v)
        max_k = max(max_k, current_max)

    print("Yes" if D >= max_k else "No")

if __name__ == '__main__':
    main()
0