結果
問題 | No.1400 すごろくで世界旅行 |
ユーザー |
![]() |
提出日時 | 2025-06-12 21:04:37 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 370 ms / 3,153 ms |
コード長 | 2,562 bytes |
コンパイル時間 | 293 ms |
コンパイル使用メモリ | 82,528 KB |
実行使用メモリ | 113,408 KB |
最終ジャッジ日時 | 2025-06-12 21:05:55 |
合計ジャッジ時間 | 3,375 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
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()