Skip to content

Commit

Permalink
2024-02-12
Browse files Browse the repository at this point in the history
  • Loading branch information
pknujsp committed Feb 12, 2024
1 parent 07e9c2f commit 5b540db
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions pknujsp/BFS/33-์ด๋ถ„ ๊ทธ๋ž˜ํ”„.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from sys import *
from collections import *


def bfs(start):
q = deque([start])
groups[start] = 1

while q:
curr = q.popleft()
visited[curr] = True

for adj in graph[curr]:
if visited[adj]:
continue

if not groups[adj]:
groups[adj] = -groups[curr]
q.append(adj)
elif groups[adj] == groups[curr]:
return False
return True


for _ in range(int(stdin.readline())):
V, E = map(int, stdin.readline().split())
graph = [[] for i in range(V + 1)]

for _ in range(E):
a, b = map(int, stdin.readline().split())
graph[a].append(b)
graph[b].append(a)

groups = [0] * (V + 1)
visited = [False] * (V + 1)
result = None

for i in range(1, V + 1):
if groups[i] == 0 and not bfs(i):
result = 'NO'
break

print('YES' if not result else result)

0 comments on commit 5b540db

Please sign in to comment.