[Silver II] Title: 연결 요소의 개수, Time: 688 ms, Memory: 66072 KB -BaekjoonHub

This commit is contained in:
SSUM
2025-01-29 17:56:47 +09:00
parent 1b37b6ea5a
commit 25323e105a
2 changed files with 58 additions and 0 deletions

View File

@@ -0,0 +1,28 @@
# [Silver II] 연결 요소의 개수 - 11724
[문제 링크](https://www.acmicpc.net/problem/11724)
### 성능 요약
메모리: 66072 KB, 시간: 688 ms
### 분류
그래프 이론, 그래프 탐색, 너비 우선 탐색, 깊이 우선 탐색
### 제출 일자
2025년 1월 29일 17:56:33
### 문제 설명
<p>방향 없는 그래프가 주어졌을 때, 연결 요소 (Connected Component)의 개수를 구하는 프로그램을 작성하시오.</p>
### 입력
<p>첫째 줄에 정점의 개수 N과 간선의 개수 M이 주어진다. (1 ≤ N ≤ 1,000, 0 ≤ M ≤ N×(N-1)/2) 둘째 줄부터 M개의 줄에 간선의 양 끝점 u와 v가 주어진다. (1 ≤ u, v ≤ N, u ≠ v) 같은 간선은 한 번만 주어진다.</p>
### 출력
<p>첫째 줄에 연결 요소의 개수를 출력한다.</p>

View File

@@ -0,0 +1,30 @@
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline
N, M = map(int, input().split())
A = list([] for i in range(N+1))
tot=0
visited = [0] * (N+1)
for _ in range(M):
s, e = map(int, input().split())
A[s].append(e)
A[e].append(s)
def DFS(v):
visited[v] = True
for i in A[v]:
if(visited[i]==False):
DFS(i)
for i in range(1,N+1):
if(visited[i]==False):
tot+=1
DFS(i)
print(tot)