Skip to content

Instantly share code, notes, and snippets.

@maehrm
Created March 24, 2024 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maehrm/4e0bbe27678e97ab11fb5801e3e323fe to your computer and use it in GitHub Desktop.
Save maehrm/4e0bbe27678e97ab11fb5801e3e323fe to your computer and use it in GitHub Desktop.
N, M = map(int, input().split())
G = [[False] * N for _ in range(N)]
for _ in range(M):
a, b = map(lambda x: int(x) - 1, input().split())
G[a][b] = True
G[b][a] = True
ok = [True] * (1 << N) # 各頂点が繋がっているかどうか?
for bit in range(1 << N):
vs = []
for i in range(N):
if bit & (1 << i):
vs.append(i)
for i in range(len(vs)):
if not ok[bit]:
break
for j in range(i + 1, len(vs)):
if not G[vs[i]][vs[j]]:
ok[bit] = False
break
INF = 10**18
dp = [INF] * (1 << N)
dp[0] = 0
for bit in range(1 << N):
if dp[bit] >= INF:
continue
cbit = (1 << N) - 1 - bit
add = cbit
while add > 0:
if ok[add]:
dp[bit | add] = min(dp[bit | add], dp[bit] + 1)
add = (add - 1) & cbit
print(dp[(1 << N) - 1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment