Created
January 18, 2026 08:15
-
-
Save maehrm/fa0b87c24d370c959518e987e54f1b6e to your computer and use it in GitHub Desktop.
C - Path Graph? https://atcoder.jp/contests/abc287/tasks/abc287_c
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
| import sys | |
| sys.setrecursionlimit(10**7) | |
| def solv(): | |
| if N - 1 != M: | |
| return False | |
| for i in range(N): | |
| if len(G[i]) > 2: | |
| return False | |
| visited = [False] * N | |
| dfs(0, visited) | |
| return all(visited) | |
| def dfs(v, visited): | |
| visited[v] = True | |
| for u in G[v]: | |
| if visited[u]: | |
| continue | |
| dfs(u, visited) | |
| return | |
| N, M = map(int, input().split()) | |
| G = [[] for _ in range(N)] | |
| for _ in range(M): | |
| u, v = map(lambda x: int(x) - 1, input().split()) | |
| G[u].append(v) | |
| G[v].append(u) | |
| print("Yes") if solv() else print("No") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment