Skip to content

Instantly share code, notes, and snippets.

@maehrm
Created January 18, 2026 08:15
Show Gist options
  • Select an option

  • Save maehrm/fa0b87c24d370c959518e987e54f1b6e to your computer and use it in GitHub Desktop.

Select an option

Save maehrm/fa0b87c24d370c959518e987e54f1b6e to your computer and use it in GitHub Desktop.
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