結果
| 問題 |
No.1400 すごろくで世界旅行
|
| コンテスト | |
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 15:54:50 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 317 ms / 3,153 ms |
| コード長 | 2,562 bytes |
| コンパイル時間 | 181 ms |
| コンパイル使用メモリ | 82,780 KB |
| 実行使用メモリ | 113,676 KB |
| 最終ジャッジ日時 | 2025-06-12 15:54:54 |
| 合計ジャッジ時間 | 2,909 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 18 |
ソースコード
import sys
from collections import deque
def main():
V, D = map(int, sys.stdin.readline().split())
E = [sys.stdin.readline().strip() for _ in range(V)]
# Build adjacency list
adj = [[] for _ in range(V)]
for i in range(V):
for j in range(V):
if E[i][j] == '1':
adj[i].append(j)
# Check if the graph is connected
visited = [False] * V
queue = deque([0])
visited[0] = True
while queue:
u = queue.popleft()
for v in adj[u]:
if not visited[v]:
visited[v] = True
queue.append(v)
if not all(visited):
print("No")
return
# Check for self-loop
has_self_loop = any(E[i][i] == '1' for i in range(V))
# Function to perform BFS and return farthest node and max distance
def bfs(u):
dist = [-1] * V
dist[u] = 0
q = deque([u])
while q:
current = q.popleft()
for neighbor in adj[current]:
if dist[neighbor] == -1:
dist[neighbor] = dist[current] + 1
q.append(neighbor)
max_dist = max(dist)
far_node = dist.index(max_dist)
return far_node, max_dist
if has_self_loop:
# Find diameter using two BFS passes
u, _ = bfs(0)
v, diameter = bfs(u)
if D >= diameter:
print("Yes")
else:
print("No")
else:
# Check if the graph is bipartite
is_bipartite = True
color = [-1] * V
for start in range(V):
if color[start] == -1:
queue = deque([start])
color[start] = 0
while queue:
u = queue.popleft()
for v in adj[u]:
if color[v] == -1:
color[v] = color[u] ^ 1
queue.append(v)
elif color[v] == color[u]:
is_bipartite = False
break
if not is_bipartite:
break
if not is_bipartite:
break
if is_bipartite:
print("No")
return
else:
# Find diameter using two BFS passes
u, _ = bfs(0)
v, diameter = bfs(u)
if D >= diameter:
print("Yes")
else:
print("No")
if __name__ == "__main__":
main()
gew1fw