Created
February 11, 2026 06:16
-
-
Save maehrm/2d0fdebbc26c59972f9fa20168ce3a1c to your computer and use it in GitHub Desktop.
E - Small d and k https://atcoder.jp/contests/abc254/tasks/abc254_e
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from collections import deque | |
| def bfs(x, k): | |
| que = deque([(x, 0)]) | |
| visits = set([x]) | |
| while que: | |
| cur, dist = que.popleft() | |
| for nxt in G[cur]: | |
| if nxt in visits: | |
| continue | |
| if dist < k: | |
| que.append((nxt, dist + 1)) | |
| visits.add(nxt) | |
| return sum(visits) | |
| N, M = map(int, input().split()) | |
| G = [[] for _ in range(N + 1)] | |
| for _ in range(M): | |
| a, b = map(int, input().split()) | |
| G[a].append(b) | |
| G[b].append(a) | |
| Q = int(input()) | |
| for _ in range(Q): | |
| x, k = map(int, input().split()) | |
| print(bfs(x, k)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment