Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 정렬
- SQL
- 노마드코더
- 프로그래머스
- 이코테
- react-native
- 백준온라인저지
- 이것이코딩테스트다
- ps
- 다이나믹프로그래밍
- 코딩일기
- c++
- dfs
- 구현
- 개발자북클럽
- 최단경로
- TS
- 백준
- 이진탐색
- bfs
- 앱개발
- 빅데이터분석
- 코테
- 그리디
- 알고리즘
- Typescript
- 코딩테스트
- 타입스크립트
- DP
- BOJ
Archives
- Today
- Total
한량처럼 살고 싶다
[SWEA] 창용 마을 무리의 개수 (python) 본문
https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWngfZVa9XwDFAQU
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
dfs로 풀었더니 런타임이 떴다. bfs로 수정했더니 정답이 되었는데 이유를 모르겠다...
from collections import deque
T = int(input())
def bfs(start):
global visit
q = deque()
q.append(start)
visit[start] = True
while q:
now = q.popleft()
for p in graph[now]:
if not visit[p]:
q.append(p)
visit[p] = True
for t in range(T):
people, relationship = map(int, input().split())
graph = [[] for _ in range(people+1)]
visit = [False] * (people+1)
answer = 0
for i in range(relationship):
a, b = map(int, input().split())
graph[a].append(b)
graph[b].append(a)
for i in range(1, people+1):
if not visit[i]:
bfs(i)
answer += 1
print(f"#{t+1} {answer}")
'PS > SWEA' 카테고리의 다른 글
[SWEA] 파리 퇴치 (python) (0) | 2024.02.02 |
---|---|
1206. [S/W 문제해결 기본] 1일차 - View(Python) (0) | 2023.10.19 |
1859. 백만 장자 프로젝트(Python) (0) | 2023.10.19 |
2072. 홀수만 더하기(Python) (0) | 2023.10.19 |